私はキーストアを使用してAES暗号化の鍵を生成し、それを使用してプレーンテキストを暗号化します。コード。私は別のアクティビティのonCreate()メソッドでcreateKey()メソッドを1回だけ呼び出し、同じkeyAliasと同じ平文でprintCipherText()メソッドを何度も呼び出します。奇妙なことは:私がprintCipherText()メソッドを呼び出すたびに、私は別の結果を得る。私は同じキーエイリアスと同じ平文を使用していますが、毎回異なる暗号テキストを得るのはなぜですか?Android AES(キーストア付き)は、同じプレーンテキストで異なる暗号テキストを生成します
public class KeyCreatorClass {
KeyStore keyStore;
KeyGenerator keyGenerator;
Cipher cipher;
public void createKey(String keyAlias) { //I call this method only once in the onCreate() method of another activity, with keyAlias "A"
try {
keyStore = KeyStore.getInstance("AndroidKeyStore");
keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
keyStore.load(null);
keyGenerator.init(
new KeyGenParameterSpec.Builder(keyAlias, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setUserAuthenticationRequired(false)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
.setRandomizedEncryptionRequired(false)
.build());
keyGenerator.generateKey();
} catch (Exception e) {
e.printStackTrace();
}
}
public String printCipherText(String keyAlias, String plainText){ //I call this method many times with the same keyAlias "A" and same plaintext in the same activity
try {
keyStore.load(null);
SecretKey key = (SecretKey) keyStore.getKey(keyAlias, null);
cipher.init(Cipher.ENCRYPT_MODE, key);
return byteToHex(cipher.doFinal(plainText.getBytes()));
}catch(Exception e){
e.printStackTrace();
}
return "BUG";
}
private String byteToHex(byte[] byteArray){
StringBuilder buf = new StringBuilder();
for (byte b : byteArray)
buf.append(String.format("%02X", b));
String hexStr = buf.toString();
return hexStr;
}
}
これらのすべての機会に、元のテキストを元に戻しますか? – MikeC
私は@Artjom B.は私の問題を解決したと思うありがとうございます –