尽管更改了加密文本,仍能够解密 Java 中的加密文本
Able to decrypt an encrypted text in Java despite changing the encrypted text
我有以下代码:
public static void main(final String[] args) throws Exception {
try {
String textToEncrypt = "{asdfsad asdf;ls kasdf asdlfjaslfjalksdfjlkadsjflkasfjl;kasj alkdfjaslkfj \r\n" +
"asdfjl;asdfjlasdjfdasfjfdosafjadsf \r\n" +
"as;ldfjal;ksfjlkdasfjadsf" +
"a;ldfjal;ksfjds" +
"}";
String secret = "SOME_SECRET";
String escapedTextToEncrypt = StringEscapeUtils.escapeJava(textToEncrypt);
System.out.println(escapedTextToEncrypt);
String encryptedText = SomeEncryption.encrypt(escapedTextToEncrypt, secret);
encryptedText = encryptedText.concat("3asdasfd"); // CHANGING THE ENCRYPTED TEXT!
System.out.println("Encrypted Text :" + encryptedText);
System.out.println("Decrypted Text :" + SomeEncryption.decrypt(encryptedText, secret)); // THIS WORKS!!
}catch(Throwable t){
t.printStackTrace();
}
}
我正在使用 AES 加密算法和 apache commons 编解码器。奇怪的是,当我将一个字符串附加到加密文本时,它仍然能够毫无问题地解密它。我在这里遗漏了什么吗?
更多详情:
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA1");
SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
编辑:
我正在使用来自 apache commons lang 库的 StringEscapeUtils。
我们找到了这个问题的答案。如果我在加密字符串的末尾添加任何字符串,那么它就可以工作。但是如果插在中间就失败了
encryptedText = new StringBuilder(encryptedText).insert(0, "xyz").toString();
以上失败。
我有以下代码:
public static void main(final String[] args) throws Exception {
try {
String textToEncrypt = "{asdfsad asdf;ls kasdf asdlfjaslfjalksdfjlkadsjflkasfjl;kasj alkdfjaslkfj \r\n" +
"asdfjl;asdfjlasdjfdasfjfdosafjadsf \r\n" +
"as;ldfjal;ksfjlkdasfjadsf" +
"a;ldfjal;ksfjds" +
"}";
String secret = "SOME_SECRET";
String escapedTextToEncrypt = StringEscapeUtils.escapeJava(textToEncrypt);
System.out.println(escapedTextToEncrypt);
String encryptedText = SomeEncryption.encrypt(escapedTextToEncrypt, secret);
encryptedText = encryptedText.concat("3asdasfd"); // CHANGING THE ENCRYPTED TEXT!
System.out.println("Encrypted Text :" + encryptedText);
System.out.println("Decrypted Text :" + SomeEncryption.decrypt(encryptedText, secret)); // THIS WORKS!!
}catch(Throwable t){
t.printStackTrace();
}
}
我正在使用 AES 加密算法和 apache commons 编解码器。奇怪的是,当我将一个字符串附加到加密文本时,它仍然能够毫无问题地解密它。我在这里遗漏了什么吗?
更多详情:
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA1");
SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
编辑:
我正在使用来自 apache commons lang 库的 StringEscapeUtils。
我们找到了这个问题的答案。如果我在加密字符串的末尾添加任何字符串,那么它就可以工作。但是如果插在中间就失败了
encryptedText = new StringBuilder(encryptedText).insert(0, "xyz").toString();
以上失败。