我正在尝试加密&使用AES算法和字符串解密字符串GCM模式.
我的代码能够加密字符串,但是我无法解密编码的数据.
遵循的步骤:
>创建密钥()
>加密(字符串)
>解密(encded_data);
我的代码:
建立金钥
public void createKey() {
try {
if (!ks.containsAlias(keyName)) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
Log.e("MAinAcvtivity", "Current version is 23(MashMello)");
//Api level 23
KeyGenerator generator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
generator.init(
new KeyGenParameterSpec.Builder(keyName,
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.build());
SecretKey key = generator.generateKey();
} else {
Log.e("MAinAcvtivity", "Current version is < 23(MashMello)");
}
}else{
Log.e("MAinAcvtivity", "Key exist");
}
} catch (Exception e) {
Log.e("MAinAcvtivity", "Key didn't generated");
Log.e("MAinAcvtivity", Log.getStackTraceString(e));
}
}
加密方式:
public String doEncryption(String data) {
try {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
ce = Cipher.getInstance("AES/GCM/NoPadding");
sKey = (SecretKey) ks.getKey(keyName, null);
ce.init(Cipher.ENCRYPT_MODE, sKey);
} else {
}
encodedData = ce.doFinal(data.getBytes());
mEncodedData = Base64.encodeToString(encodedData, Base64.DEFAULT);
} catch (Exception e) {
Log.e("Main", "RSA Encription Error.!");
Log.e("MainActivity", "RSA Decryption Error.!", e);
}
Log.e("Main", "encripted DATA =" + mEncodedData);
return mEncodedData;
}
解密:
public String doDecryption(String eData) {
String decryptedText = null;
encodedData = Base64.decode(eData, Base64.DEFAULT);
try {
Cipher c;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
Log.i("MainActivity","in Decryption version m");
c = Cipher.getInstance("AES/GCM/NoPadding");
sKey = (SecretKey) ks.getKey(keyName, null);
Log.e("MainActivity", "After getting key : " );
c.init(Cipher.DECRYPT_MODE, sKey);
} else {
}
decodedData = c.doFinal(encodedData);
decryptedText = new String(decodedData, "UTF-8");
Log.e("MainActivity", "After decryption : "+ decryptedText);
} catch (Exception e) {
Log.e("MainActivity", "RSA Decryption Error.!", e);
}
return decryptedText;
}
错误日志:
MAinAcvtivity: Key exist
Main: encripted DATA =KrHmMXhcytb0owDzLaMY2wsQmwY=
MainActivity: in decryption : encoded data =KrHmMXhcytb0owDzLaMY2wsQmwY=
MainActivity: After getting key :
MainActivity: RSA Decryption Error.!
MainActivity: java.security.InvalidKeyException: IV required when decrypting. Use IvParameterSpec or AlgorithmParameters to provide it.
MainActivity: at android.security.keystore.AndroidKeyStoreAuthenticatedAESCipherSpi$GCM.initAlgorithmSpecificParameters(AndroidKeyStoreAuthenticatedAESCipherSpi.java:79)
MainActivity: at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineInit(AndroidKeyStoreCipherSpiBase.java:106)
解决方法:
宾果
我找到了解决方案.
在解密中,我进行了以下更改:
GCMParameterSpec spec = new GCMParameterSpec(128,ce.getIV());
c.init(Cipher.DECRYPT_MODE, sKey,spec);