我尝试加密和解密电子邮件和密码文本。我使用了javax.crypto库。它可以加密数据,并且没有错误。但是当它解密数据时,会抛出如下错误。我错过了什么?
private final String key = "12345678901234567890123456789012";
private void saveUserInfo() {
String email = emailET.getText().toString();
String password = passwordET.getText().toString();
SecretKeySpec sks = generateKey();
byte[] encodedEmail = encrypt(email, sks);
byte[] encodedPassword = encrypt(password, sks);
String decodedEmail = decrypt(encodedEmail, sks);
String decodedPassword = decrypt(encodedPassword, sks);
Log.d("Encoded Email", Base64.encodeToString(encodedEmail, Base64.DEFAULT));
Log.d("Encoded Password", Base64.encodeToString(encodedPassword, Base64.DEFAULT));
Log.d("Decoded Email", decodedEmail);
Log.d("Decoded Password", decodedPassword);
}
public SecretKeySpec generateKey(){
try {
SecureRandom secureRandom = new SecureRandom();
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(256, secureRandom);
SecretKeySpec sks = new SecretKeySpec(key.getBytes(), "AES");
return sks;
}
catch (NoSuchAlgorithmException error){
return null;
}
}
public byte[] encrypt(String text, Key key){
try {
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encodedBytes = c.doFinal(text.getBytes());
return encodedBytes;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return null;
}
public String decrypt(byte[] text, Key key){
try {
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
c.init(Cipher.DECRYPT_MODE, key);
byte[] decodedBytes = c.doFinal(text);
return new String(decodedBytes);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return null;
}
StackTrace:
javax.crypto.BadPaddingException: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
at com.android.org.conscrypt.OpenSSLCipher.doFinalInternal(OpenSSLCipher.java:430)
at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:466)
at javax.crypto.Cipher.doFinal(Cipher.java:1340)
at com.example.myapplication.MainActivity.decrypt(MainActivity.java:100)
at com.example.myapplication.MainActivity.saveUserInfo(MainActivity.java:53)
at com.example.myapplication.MainActivity.onSubmitButtonClicked(MainActivity.java:40)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
javax.crypto.BadPaddingException: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
at com.android.org.conscrypt.OpenSSLCipher.doFinalInternal(OpenSSLCipher.java:430)
at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:466)
at javax.crypto.Cipher.doFinal(Cipher.java:1340)
at com.example.myapplication.MainActivity.decrypt(MainActivity.java:100)
at com.example.myapplication.MainActivity.saveUserInfo(MainActivity.java:54)
at com.example.myapplication.MainActivity.onSubmitButtonClicked(MainActivity.java:40)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)