能够在Java中解密经过更改的加密文本

huangapple go评论67阅读模式
英文:

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();
    }
}
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA1");
SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

EDIT:

I am using StringEscapeUtils from apache commons lang library.


<details>
<summary>英文:</summary>

I have the following code:

    public static void main(final String[] args) throws Exception {
            try {
                String textToEncrypt = &quot;{asdfsad asdf;ls kasdf asdlfjaslfjalksdfjlkadsjflkasfjl;kasj alkdfjaslkfj \r\n&quot; +
                        &quot;asdfjl;asdfjlasdjfdasfjfdosafjadsf \r\n&quot; +
                        &quot;as;ldfjal;ksfjlkdasfjadsf&quot; +
                        &quot;a;ldfjal;ksfjds&quot; +
                        &quot;}&quot;;
                String secret = &quot;SOME_SECRET&quot;;
    
            String escapedTextToEncrypt = StringEscapeUtils.escapeJava(textToEncrypt);
            System.out.println(escapedTextToEncrypt);
    
            String encryptedText = SomeEncryption.encrypt(escapedTextToEncrypt, secret);
    
            encryptedText = encryptedText.concat(&quot;3asdasfd&quot;);  // CHANGING THE ENCRYPTED TEXT!
    
                System.out.println(&quot;Encrypted Text :&quot; + encryptedText);
    
                System.out.println(&quot;Decrypted Text :&quot; + SomeEncryption.decrypt(encryptedText, secret));  // THIS WORKS!!
            }catch(Throwable t){
                t.printStackTrace();
            }
        }


I am using AES encryption algorithm and apache commons codec.  Curiously, when I append a string to the encrypted text, it is still able to decrypt it with no problems.  Am I missing something here?

More details:

    SecretKeyFactory factory = SecretKeyFactory.getInstance(&quot;PBKDF2withHmacSHA1&quot;);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), &quot;AES&quot;);
    Cipher cipher = Cipher.getInstance(&quot;AES/CBC/PKCS5Padding&quot;);



EDIT:

I am using StringEscapeUtils from apache commons lang library.

</details>


# 答案1
**得分**: 0

我们找到了这个问题的答案。如果我在加密字符串末尾添加任何字符串,那么它就能正常工作。但如果插入到中间位置,那么就会失败。

```java
encryptedText = new StringBuilder(encryptedText).insert(0, "xyz").toString();

上述操作会失败。

英文:

We found the answer to this question. If I add any string at the end of the encrypted string then it works. But if it is inserted in the middle then it fails.

encryptedText = new StringBuilder(encryptedText).insert(0, &quot;xyz&quot;).toString();

The above fails.

huangapple
  • 本文由 发表于 2020年4月10日 12:46:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/61134184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定