String plain1= "Test";
byte[] cipher = SplashSecure.getInstance().encrypt2(plain1);
String plain2 = SplashSecure.getInstance().decrypt2(cipher);
平原=测试
解密后plainText2
应等于plaintext
。但它不是。
加密/解密方法。
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;
}
编辑:
我试着去得到加密,解密跨Windows工作(其它客户端,加密和服务器)和Android。 Windows客户端是一个VC的应用程序,使用的Rijndael类( 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使用正确的密钥来解密。