gpt4 book ai didi

java - java AES解密函数有什么问题?

转载 作者:行者123 更新时间:2023-12-01 17:39:32 25 4
gpt4 key购买 nike

我修改了可用的代码 http://java.sun.com/developer/technicalArticles/Security/AES/AES_v1.html

并在程序中制定了加密和解密方法。但我收到 BadpaddingException..该函数也返回 null..为什么会这样?出什么问题了?请帮助我..

这些是我正在使用的变量:


kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
raw = new byte[]{(byte)0x00,(byte)0x11,(byte)0x22,(byte)0x33,(byte)0x44,(byte)0x55,(byte)0x66,(byte)0x77,(byte)0x88,(byte)0x99,(byte)0xaa,(byte)0xbb,(byte)0xcc,(byte)0xdd,(byte)0xee,(byte)0xff};
skeySpec = new SecretKeySpec(raw, "AES");
cipher = Cipher.getInstance("AES");

plainText=null;
cipherText=null;

以下是解密函数..


public String decrypt(String cipherText)
{
try
{
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] original = cipher.doFinal(cipherText.getBytes());
plainText = new String(original);
}


catch(BadPaddingException e)
{

}

return plainText;
}

最佳答案

来自Java-Security archives

One common mistakes that people made is to put the encrypted bytes inside astring and upon decryption they use String.getBytes() to retrieve it.Since String does its own character encoding, the byte[] that you used toconstruct the String object and the byte[] that you get from its getBytes()are not necessarily equal.

关于java - java AES解密函数有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2732808/

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