gpt4 book ai didi

java - 解密不正确

转载 作者:行者123 更新时间:2023-12-01 15:18:35 25 4
gpt4 key购买 nike

我将 XML 文件作为字节数组导入到项目中

RandomAccessFile rnd = new RandomAccessFile(filePath, "r");
byte[] fileData = new byte[(int) rnd.length()];
rnd.read(fileData);

我使用 java.crypto 加密了数组

Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");

byte[] encypted = new byte[cipher.getOutputSize(fileData.length)];
int len = cipher.update(fileData, 0, fileData.length, encypted, 0);
len += cipher.doFinal(encypted, len);

当我解密字节数组并使用它打印时

System.out.println(new String(decrypted, "UTF-8"));

我得到了 XML 文件,但末尾有一些未知字符(它们仅位于末尾)。有什么办法可以删除这个吗?

提前致谢

最佳答案

看到类似的问题here ,但答案可能与您的情况非常相关:

1.If you don't know what padding was used to encrypt, then decrypt with 'no padding' set. That will decrypt everything, including the padding, and won't throw an error because of mismatched padding.

2.When you have decrypted the cyphertext, have a look at the last block of the output and see what padding was used. Different paddings leave different byte patterns, so it is usually easy enough to tell.

3.Set your decryption method to expect the correct type of padding, and it will be automatically removed for you.

如果这与您相关,这里有一个关于填充模式等的链接:Padding Wikipedia

这里也是一个非常好的使用 DES 加密的教程:http://www.exampledepot.com/egs/javax.crypto/desstring.html这是 DES 中的 PBE: PBE with HMAC(SHA1) and DES(EDE)

关于java - 解密不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11292413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com