Java can';无法在解密时删除0
我用Java编写了两个函数来加密和解密消息。我选择AES/CBC/PKCS7Padding作为参数。加密时,我的函数无法添加正确的填充(如PKCS7Padding,它不应添加0来填充我的消息),因此在解密时,我无法删除消息末尾的0。 有了代码,我的问题会更严重Java can';无法在解密时删除0,java,aes,Java,Aes,我用Java编写了两个函数来加密和解密消息。我选择AES/CBC/PKCS7Padding作为参数。加密时,我的函数无法添加正确的填充(如PKCS7Padding,它不应添加0来填充我的消息),因此在解密时,我无法删除消息末尾的0。 有了代码,我的问题会更严重 public static byte[] encryptStringToData(String message, String key){ // transform the message from string
public static byte[] encryptStringToData(String message, String key){
// transform the message from string to bytes
byte[] array_to_encrypt, bkey;
try {
array_to_encrypt = message.getBytes("UTF8");
bkey = key.getBytes("UTF8");
} catch (UnsupportedEncodingException e1) {
array_to_encrypt = null;
bkey = null;
return null;
}
bkey = Arrays.copyOf(bkey, 32);
BlockCipher engine = new AESEngine();
engine.init(true, new KeyParameter(bkey, 0, 32));
PKCS7Padding pad = new PKCS7Padding() ;
BufferedBlockCipher c = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine),pad);
c.init(true, new ParametersWithIV(new KeyParameter(bkey), new byte[16]));
byte[] encrypted_array = new byte[c.getOutputSize(array_to_encrypt.length)];
int outputLen = c.processBytes(array_to_encrypt, 0, array_to_encrypt.length, encrypted_array, 0);
try
{
c.doFinal(encrypted_array, outputLen);
}
catch (CryptoException ce)
{
System.err.println(ce);
System.exit(1);
}
return encrypted_array;
}
public static String decryptDataToString(byte[] message, String key){
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
String decrypted ="";
try {
byte[] keyBytes = key.getBytes("UTF8");
BlockCipher engine = new AESEngine();
BufferedBlockCipher c = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
keyBytes = Arrays.copyOf(keyBytes, 32); // use only first 256 bit
c.init(false, new ParametersWithIV(new KeyParameter(keyBytes), new byte[16]));
byte[] cipherText = new byte[c.getOutputSize(message.length)];
int outputLen = c.processBytes(message, 0, message.length, cipherText, 0);
c.doFinal(cipherText, outputLen);
decrypted = new String(cipherText,"UTF8");
}
catch (CryptoException ce)
{
System.err.println(ce);
System.exit(1);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return decrypted;
}
结果(十六进制输出)
clair信息:3132333435大小:5
加密邮件:561dd9f43ec183fe351776a46276991c大小:16
解密消息:31323334335000000000000000000000大小:16
删除额外字节的代码为:
if (outputLength == output.length) {
return output;
} else {
byte[] truncatedOutput = new byte[outputLength];
System.arraycopy(
output, 0,
truncatedOutput, 0,
outputLength
);
return truncatedOutput;
}
在您的代码列表中:
outputLen += c.doFinal(cipherText, outputLen);
if (outputLen == cipherText.length) {
decrypted = new String(cipherText,"UTF8");
} else {
byte[] truncatedOutput = new byte[outputLen];
System.arraycopy(
cipherText, 0,
truncatedOutput, 0,
outputLen
);
decrypted = new String(truncatedOutput,"UTF8");
}
你应该检查一下
删除额外字节的代码为:
if (outputLength == output.length) {
return output;
} else {
byte[] truncatedOutput = new byte[outputLength];
System.arraycopy(
output, 0,
truncatedOutput, 0,
outputLength
);
return truncatedOutput;
}
在您的代码列表中:
outputLen += c.doFinal(cipherText, outputLen);
if (outputLen == cipherText.length) {
decrypted = new String(cipherText,"UTF8");
} else {
byte[] truncatedOutput = new byte[outputLen];
System.arraycopy(
cipherText, 0,
truncatedOutput, 0,
outputLen
);
decrypted = new String(truncatedOutput,"UTF8");
}
如果你不想,为什么要填充消息?这是加密API的要求吗?在这种情况下,在消息的开头包含长度,以便知道要删除多少个零。因为我使用CBC,所以在加密/解密传递之前,似乎需要对消息进行填充。如果不想填充,为什么要填充消息?这是加密API的要求吗?在这种情况下,在消息的开头包含长度,这样您就知道要删除多少个零。因为我使用CBC,所以在加密/解密过程之前,似乎需要对消息进行很好的填充。它可以工作!我以为PKCS7Padding会自动取消加载,但是。。好吧,我自己把填充物去掉没关系。非常感谢^^它很有效!我以为PKCS7Padding会自动取消加载,但是。。好吧,我自己把填充物去掉没关系。多谢各位^^