【问题标题】:Decryption problem using AES/CBC in android在android中使用AES/CBC的解密问题
【发布时间】:2018-12-26 18:51:18
【问题描述】:

我正在制作一个简单的应用程序,该应用程序使用带有 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;
}

}

【问题讨论】:

  • 你在解密前做了Base64.getDecoder().decode吗?
  • 谢谢它有效,这是我错过的!
  • 现在你可以用我的回答来结束这个问题了。玩得开心。

标签: java android cryptography aes


【解决方案1】:

你用过

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

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

Base64.getDecoder().decode( )

记住,永远把你做过的事,扭转它。

【讨论】:

    【解决方案2】:

    线

    byte[] cipher = ciphertext.getBytes();
    

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

    【讨论】:

    • 谢谢你这是我错过的,我用了这一行:byte[] b = Base64.getDecoder().decode(ciphertext);它有效。但是这种语法 Base64.decodeBase64(encryptedValue) 可能在android中不起作用。
    猜你喜欢
    • 2013-08-11
    • 2017-09-28
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 2015-09-16
    • 2021-04-22
    • 1970-01-01
    • 2021-10-23
    相关资源
    最近更新 更多