Java android中AES/CBC的解密问题
我正在制作一个简单的应用程序,使用AES算法和CBC对字符串进行加密和解密,加密正在工作,而解密根本不工作,当我用来将文本设置为xml中的编辑文本时,会给出空结果,所以有人能帮我吗?下面是我的代码:Java android中AES/CBC的解密问题,java,android,cryptography,aes,Java,Android,Cryptography,Aes,我正在制作一个简单的应用程序,使用AES算法和CBC对字符串进行加密和解密,加密正在工作,而解密根本不工作,当我用来将文本设置为xml中的编辑文本时,会给出空结果,所以有人能帮我吗?下面是我的代码: public class MainActivity extends AppCompatActivity { EditText ptext,ctext,dtext; Button eButton,dButton; @Override protected void onCreate(Bundle s
public class MainActivity extends AppCompatActivity {
EditText ptext,ctext,dtext;
Button eButton,dButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ptext =(EditText)findViewById(R.id.editText);
ctext =(EditText)findViewById(R.id.editText2);
dtext =(EditText)findViewById(R.id.editText3);
eButton = (Button)findViewById(R.id.button);
dButton = (Button)findViewById(R.id.button2);
KeyGenerator keyGenerator = null;
try {
keyGenerator = KeyGenerator.getInstance("AES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
keyGenerator.init(128);
// Generate Key
final SecretKey key = keyGenerator.generateKey();
// Generating IV.
final byte[] IV = new byte[16];
SecureRandom random = new SecureRandom();
random.nextBytes(IV);
eButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String plaintext = ptext.getText().toString();
byte[] cipherText = new byte[0];
cipherText = encrypt(plaintext.getBytes(),key, IV);
ctext.setText(Base64.getEncoder().encodeToString(cipherText));
}
});
dButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String ciphertext=ctext.getText().toString();
byte[] cipher = ciphertext.getBytes();
String decryptedText = decrypt(cipher,key, IV);
dtext.setText(decryptedText);
}
});
}
public static byte[] encrypt (byte[] plaintext,SecretKey key,byte[] IV )
{
try {
//Get Cipher Instance
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//Create SecretKeySpec
SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");
//Create IvParameterSpec
IvParameterSpec ivSpec = new IvParameterSpec(IV);
//Initialize Cipher for ENCRYPT_MODE
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
//Perform Encryption
byte[] cipherText = cipher.doFinal(plaintext);
return cipherText;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public static String decrypt (byte[] cipherText, SecretKey key,byte[] IV)
{
//Get Cipher Instance
Cipher cipher = null;
try {
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//Create SecretKeySpec
SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");
//Create IvParameterSpec
IvParameterSpec ivSpec = new IvParameterSpec(IV);
//Initialize Cipher for DECRYPT_MODE
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
//Perform Decryption
byte[] decryptedText = cipher.doFinal(cipherText);
return new String(decryptedText);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}这条线
byte[] cipher = ciphertext.getBytes();
这就是问题所在。在解密中应该使用Base64.decodeBase64(encryptedValue),因为在加密时使用Base64.getEncoder().encodeToString。但在尝试解密之前,必须先执行此操作。必须按与加密方法相反的顺序撤消操作。您使用的
Base64.getEncoder().encodeToString(cipherText));
在加密过程之后,但在解密之前忘记解码
Base64.getDecoder().decode( )
记住,你一直在做的事,把它颠倒过来。你是不是在64.getDecoder()之前解码?谢谢你,它能工作,这就是我错过的!现在你可以用我的答案来结束这个问题了。祝你玩得开心。谢谢你这是我错过做的,我用了这行:byte[]b=Base64.getDecoder().decode(密文);它是有效的。但是这种语法Base64.decodeBase64(encryptedValue)在android中可能不起作用。