Possible Duplicate:
How do I use 3des encryption/decryption in Java?
如何在Java中使用3DES加密/解密文本字符串?
我找到了答案。当我问这个问题时,没有出现重复的问题。
How do I use 3des encryption/decryption in Java?
最佳答案
从旧代码:
public void testSymCypher(SecretKey k, String str)
throws BadPaddingException, IllegalBlockSizeException,
InvalidAlgorithmParameterException, InvalidKeyException,
NoSuchAlgorithmException, NoSuchPaddingException
{
Cipher cip = Cipher.getInstance("DESede/CBC/PKCS5Padding");
cip.init(Cipher.ENCRYPT_MODE,k);
byte[] ciphered = cip.doFinal(str.getBytes());
byte iv[] = cip.getIV();
// printing the ciphered string
printHexadecimal(ciphered);
IvParameterSpec dps = new IvParameterSpec(iv);
cip.init(Cipher.DECRYPT_MODE,k,dps);
byte[] deciphered = cip.doFinal(ciphered);
// printing the deciphered string
printHexadecimal(deciphered);
}
注意,Java JDK 6中提供了DESede的其他用法:
DESede / CBC / NoPadding(168)
DESede / CBC / PKCS5填充(168)
还提供ECB模式(但要小心,不要两次使用它!),在这种情况下,您无需使用iv part:
DESede / ECB /无填充(168)
DESede / ECB / PKCS5填充(168)
生成DESede的密钥:
KeyGenerator generatorDes = KeyGenerator.getInstance("DESede");
SecretKey skaes = generatorDes.generateKey();
最后,如果您需要使用Java和密码学,我建议您从SUN阅读this document