加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

使用除ECB之外的其他模式,使用DES进行解密会抛出“InvalidKeyExc

发布时间:2020-12-15 04:11:34 所属栏目:Java 来源:网络整理
导读:我基本上使用 http://www.avajava.com/tutorials/lessons/how-do-i-encrypt-and-decrypt-files-using-des.html的代码加密应用程序,但我希望能够选择操作模式,所以我添加了这个: private String key;private static String algorithmMode; public DESCrypt(S
我基本上使用 http://www.avajava.com/tutorials/lessons/how-do-i-encrypt-and-decrypt-files-using-des.html的代码加密应用程序,但我希望能够选择操作模式,所以我添加了这个:

private String key;
private static String algorithmMode;

    public DESCrypt(String password,String algorithmMode) {
        this.key = password;
        this.algorithmMode = "DES/" + algorithmMode + "/PKCS5Padding";
    }

主要看起来像这样:

public static void main(String[] args) {
        try {
            DESCrypt des = new DESCrypt("12345678",algorithmMode);

            // FileInputStream fis1 = new FileInputStream("d:_test.txt");
            // FileOutputStream fos1 = new FileOutputStream("d:_test.txt.des");
            // des.encrypt(fis1,fos1);

            FileInputStream fis = new FileInputStream("d:_test.txt.des");
            FileOutputStream fos = new FileOutputStream("d:_test.txt");
            des.decrypt(fis,fos);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

正如我在标题中所说的那样它可以与ECB一起使用但是在其他模式下我只能加密.

解决方法

您缺少解密的IV值.您需要在Cipher.init调用中包含此内容:

... Cipher.init(Cipher.DECRYPT,someKey,new IvParameterSpec(eightByteValue));

如果从加密代码中省略它,将生成随机IV值.您需要存储它(通过Cipher.getIV()检索)以在您的解密代码中使用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读