Szyfrowanie i deszyfrowanie nie daje tego samego zwykłego tekstu przy użyciu AES / ECB / NoPadding
String plain1= "Test";
byte[] cipher = SplashSecure.getInstance().encrypt2(plain1);
String plain2 = SplashSecure.getInstance().decrypt2(cipher);
plain = Test
Po odszyfrowaniuplainText2
powinien być równyplaintext
.Ale nie jest.
Szyfruj / Odszyfruj metody.
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;
}
Edytować:
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;
}
Edytować:
Próbuję uzyskać szyfrowanie-deszyfrowanie działające w oknach (inny klient szyfrujący i serwer) oraz android. Klient Windows to aplikacja VC ++, która używa klasy Rijndael (http://svn.openfoundry.org/pcman/2007.06.03/Lite/Rijndael.h) i Android używahttp://www.cs.ucdavis.edu/~rogaway/ocb/ocb-java/Rijndael.java Klient Windows zaszyfrował dane i zapisał je na serwerze. Muszę zbudować klienta dla Androida, który pobiera zaszyfrowane dane, odszyfrowuje je i wyświetla użytkownikowi.
Jestem pewien, że używam właściwego klucza do odszyfrowania.