我基本上使用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";
}
Run Code Online (Sandbox Code Playgroud)
主要看起来像这样:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
正如我在标题中所说的那样它可以与ECB一起使用但是在其他模式下我只能加密.
您缺少解密的IV值.您需要在Cipher.init通话中包含此内容:
... Cipher.init(Cipher.DECRYPT, someKey, new IvParameterSpec(eightByteValue));
Run Code Online (Sandbox Code Playgroud)
如果从加密代码中省略它,将生成随机IV值.您将需要存储此(检索到的Cipher.getIV())以在您的解密代码中使用.
| 归档时间: |
|
| 查看次数: |
7251 次 |
| 最近记录: |