gpt4 book ai didi

java - 在android中使用AES/CBC的解密问题

转载 作者:行者123 更新时间:2023-11-29 04:13:00 25 4
gpt4 key购买 nike

我正在制作一个简单的应用程序,使用 AES 算法和 CBC 加密和解密字符串,加密有效,而解密根本不起作用,当我将文本设置为编辑文本时给出空结果在 xml 中,所以有人可以帮助我吗?下面是我的代码:

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;
}

最佳答案

你用过

Base64.getEncoder().encodeToString(cipherText));

在加密过程之后,但在解密之前忘记解码。

Base64.getDecoder().decode( )

记住,永远把你做过的事逆转过来。

关于java - 在android中使用AES/CBC的解密问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53935938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com