我有以下代码:
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();
以上失败。
关于java - 尽管更改了加密文本,仍能够解密 Java 中的加密文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61134184/