加密和解密不会使用AES / ECB / NoPadding提供相同的纯文本 [英] Encrypt and decrypt doesnt give the same plain text using AES/ECB/NoPadding
问题描述
String plain1= "Test";
byte[] cipher = SplashSecure.getInstance().encrypt2(plain1);
String plain2 = SplashSecure.getInstance().decrypt2(cipher);
plain =Test
plain = Test������������������������
解密后 plainText2
应该等于 plaintext
。但不是。
After decryption plainText2
should be equal to plaintext
.But its not.
加密/解密方法。
public void initKey(String key) {
String paddedKey = Utils.padString(key);
mKeyspec = new SecretKeySpec(Utils.getBytes(paddedKey), "AES/ECB/NoPadding");
// Utils.getBytes returns "paddedKey.getBytes("CP1252")"
}
public byte[] encrypt2(String data) {
try {
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, mKeyspec);
String paddedData = Utils.padString(data);
return cipher.doFinal(Utils.getBytes(paddedData));
} catch(InvalidKeyException e) {
e.printStackTrace();
// Series of catch blocks
}
return null;
}
public String decrypt2(byte[] cypherText) {
try {
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, mKeyspec);
byte[] plainTextBytes = cipher.doFinal(cypherText);
return Utils.getString(plainTextBytes);
// Utils.getString returns "new String(bytes, "CP1252");"
} catch(InvalidKeyException e) {
// Series of catch blocks.
}
return null;
}
编辑:
public static String padString(String source) {
char paddingChar = '\0';
int size = 16;
int padLength = size - source.length() % size;
for (int i = 0; i < padLength; i++) {
source += paddingChar;
}
return source;
}
编辑:
http:// svn.openfoundry.org/pcman/2007.06.03/Lite/Rijndael.h ),android使用 http://www.cs.ucdavis.edu/~rogaway/ocb/ocb-java/Rijndael.java Windows客户端已加密数据并存储在服务器。我需要构建一个android的客户端,获取加密的数据,解密并显示给用户。
Im trying to get the encryption-decryption working across windows(other client that encrypts, and server) and android. The windows client is a VC++ app that uses a Rijndael class(http://svn.openfoundry.org/pcman/2007.06.03/Lite/Rijndael.h) and android uses http://www.cs.ucdavis.edu/~rogaway/ocb/ocb-java/Rijndael.java The Windows client has encrypted the data and stored it on the server. I need to build a client for android that fetches the encrypted data, decrypt it and display to the user.
我确定使用正确的密钥进行解密。
Im sure im using the correct key to decrypt.
推荐答案
AES的块大小为128位(即16字节)。它可以仅以这种大小的方式处理数据,因此即使您已经告诉它使用 NoPadding
,它无法遵守。
AES has a block size of 128 bits (i.e 16 bytes). It can only process data in blocks of this size, so even though you have told it to use NoPadding
it is unable to comply.
这里发生的最可能的事情是,您正在使用的AES实现是内部填充4个字节的输入,最多16个字节,并加密结果。当您解密时,您将得到相同的16个字节,即T,e,s,t和12个垃圾字节。
The most likely thing that is happening here is that the AES implementation you are using is internally padding your four bytes of input up to 16 bytes and encrypting the result. When you decrypt, you get the same 16 bytes back out, i.e. 'T', 'e', 's', 't' and 12 garbage bytes.
输出你看到支持这样:测试,然后是24 ?
符号。我不知道为什么它为每个垃圾字节打印两个?
符号,但我猜这是解释unicode中的垃圾字节。您可以通过打印解密的blob的原始字节值来看到发生了什么。
The output you see supports this: "Test" followed by 24 ?
symbols. I don't know why it's printing two ?
symbols for each garbage byte, but I'm guessing it's something to do with interpreting the garbage bytes in unicode. You could see what is going on by printing out the raw byte values of the decrypted blob.
简短的答案是NoPadding对于一个块没有意义密码(或者,如果你要使用NoPadding,那么你必须自己填充和解压缩)。
The short answer is that 'NoPadding' doesn't make sense for a block cipher (or, rather, if you are going to use NoPadding then you have to pad and unpad things yourself).
这篇关于加密和解密不会使用AES / ECB / NoPadding提供相同的纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!