在Android中使用AES / CBC进行解密的问题

时间:2018-12-26 18:51:18

标签: java android cryptography aes

我正在制作一个简单的应用程序,该应用程序使用带有CBC的AES算法对字符串进行加密和解密,加密有效,而解密完全无效,并且当我将文本设置为编辑文本时给出空结果在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;
}

}

2 个答案:

答案 0 :(得分:2)

您使用

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

在加密过程之后,但是忘记在解密之前对其进行解码。

Base64.getDecoder().decode( )

记住,请务必将您所做的全部撤回。

答案 1 :(得分:1)

byte[] cipher = ciphertext.getBytes();

是问题所在。您应该在解密中使用Base64.decodeBase64(encryptedValue),因为在加密时使用Base64.getEncoder()。encodeToString。但是,您必须先尝试执行此操作,然后再尝试解密。您必须按与加密方法相反的顺序撤消操作。