[英]Java 3DES implementation (academic)
My code so far is:到目前为止,我的代码是:
public class TripleDES {
/**
* @param args the command line arguments
* @throws java.security.NoSuchAlgorithmException
* @throws javax.crypto.NoSuchPaddingException
* @throws java.security.InvalidKeyException
* @throws javax.crypto.IllegalBlockSizeException
* @throws javax.crypto.BadPaddingException
*/
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
//Encrypt: C = EK3(DK2(EK1(P)))
//Decrypt: P = DK3(EK2(DK1(C)))
Scanner sc = new Scanner(System.in);
//Generate key for DES
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
SecretKey secretKey = keyGenerator.generateKey();
SecretKey secretKey2 = keyGenerator.generateKey();
SecretKey secretKey3 = keyGenerator.generateKey();
//Text Enc & Dec
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
//enter msg
System.out.print("Enter a string: ");
String x= sc.nextLine();
//enc
cipher.init(Cipher.ENCRYPT_MODE,secretKey);
byte[] message = x.getBytes();//text
byte[] messageEnc = cipher.doFinal(message);//encryption with key1
cipher.init(Cipher.DECRYPT_MODE,secretKey2);
byte[] deck2 = cipher.doFinal(messageEnc);//decryption with key2
cipher.init(Cipher.ENCRYPT_MODE,secretKey3);
byte[] messageEnc1 = cipher.doFinal(deck2);//encryption with key3
System.out.println("Cipher Text: " + new String(messageEnc1));
//dec
cipher.init(Cipher.DECRYPT_MODE,secretKey3);
byte[] dec = cipher.doFinal(messageEnc1);//decryption with key1
cipher.init(Cipher.ENCRYPT_MODE,secretKey2);
byte[] messageEnc2 = cipher.doFinal(dec);//encryption with key2
cipher.init(Cipher.DECRYPT_MODE,secretKey);
byte[] deck3 = cipher.doFinal(messageEnc2);//decryption with key3
System.out.println("Plain Text: " + new String(deck3));
}
}
I get the error:我得到错误:
Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:991)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DESCipher.java:314)
at javax.crypto.Cipher.doFinal(Cipher.java:2164)
at tripledes.TripleDES.main(TripleDES.java:45)
My guess is that when I try decrypting with a different key at line 45 and it gives the error above, it's because the encrypted text is larger than the key generated, but I am not quite sure.我的猜测是,当我尝试在第 45 行使用不同的密钥解密并给出上述错误时,这是因为加密的文本大于生成的密钥,但我不太确定。
Can someone please help as I can't figure out the problem.有人可以帮忙,因为我无法弄清楚问题所在。
I am referring to the originally posted code with the cipher
, cipher2
and cipher3
instances: The problem is the padding regarding cipher2
and cipher3
.我指的是最初发布的带有
cipher
、 cipher2
和cipher3
实例的代码:问题是关于cipher2
和cipher3
的填充。 Only cipher
may use PKCS5Padding
, cipher2
and cipher3
must apply NoPadding
.只有
cipher
可以使用PKCS5Padding
, cipher2
和cipher3
必须应用NoPadding
。
The generated ciphertext is then indeed identical to the one that would be generated by 3DES, provided that the concatenated bytes of secretKey
, secretKey2
and secretKey3
are used as 3DES key.生成的密文确实与 3DES 生成的密文相同,前提是
secretKey
、 secretKey2
和secretKey3
的连接字节用作 3DES 密钥。
ECB is an insecure mode, by the way, s.顺便说一下,欧洲央行是一种不安全的模式。 here .
在这里。
Regarding the comments:关于评论:
For the decoding of a ciphertext with a charset encoding, see eg Problems when converting from byte array to string and back to byte array .对于使用字符集编码的密文的解码,请参见从字节数组转换为字符串并返回字节数组时出现的问题。 Concerning the missing encoding specification, seg String.getBytes() in different default charsets .
关于缺少的编码规范,seg String.getBytes() in different default charsets 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.