【问题标题】:Able to decrypt an encrypted text in Java despite changing the encrypted text尽管更改了加密文本,但能够在 Java 中解密加密文本
【发布时间】:2020-04-10 14:05:59
【问题描述】:

我有以下代码:

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。

【问题讨论】:

  • 如果你删除一个字符或者替换一个字符,它会如你所愿地失败。它可以解密它的事实仅意味着附加字符被忽略
  • 您未能提供许多关键细节。当然,至少必须提供SomeEncryption.encrypt() 的代码才能有机会回答您的问题。我只能说,通常情况下,当您将任何内容附加到以这种模式加密的密文时,如果您附加的内容不是 16 字节的倍数,则会得到一个无效的长度异常,并且如果出现错误的填充异常(很有可能)它是 16 个字节的倍数。

标签: java encryption aes apache-commons


【解决方案1】:

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

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

上述失败。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多