Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

incorrect decryption using AES/CBC/PKCS5Padding in Android

I wrote the following code in Android (v2.2 API 8), where a plain text is entered and the code encrypts it using a user password and a random salt and then decrypts it. After running the code I only get part of the plain text correct. For example the user enters "Msg 1.5 to encrypt" and the result from the decryption code is "Msg15toencrypg=="

Here is the code:

 private EditText plain_msg;
    private EditText pwd;
    private TextView result;
    byte[] iv;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    plain_msg = (EditText)findViewById(R.id.msg2encypt);
    pwd = (EditText)findViewById(R.id.password);
    result = (TextView)findViewById(R.id.decrypttxt);
}

public void mybuttonHandler(View view){
    String S_plain_msg = plain_msg.getText().toString();
    String S_pwd = pwd.getText().toString();
    setAES(S_plain_msg, S_pwd);
}


private byte[] generateSalt() throws NoSuchAlgorithmException{
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
    byte[] ransalt = new byte[20];
    random.nextBytes(ransalt);
    return ransalt;
}


private void setAES(String msg, String pwd){
    try {
        //Generation of Key
        byte[] salt = generateSalt();
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHSHA256AND256BITAES-CBC-BC");
        KeySpec spec = new PBEKeySpec(pwd.toCharArray(),salt,1024, 256);
        SecretKey tmp = factory.generateSecret(spec);
        SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

        //Encryption process
        byte[] btxt = Base64.decode(msg, 0);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secret); 
        AlgorithmParameters params = cipher.getParameters(); 
        iv = params.getParameterSpec(IvParameterSpec.class).getIV(); 
        byte[] ciphertext = cipher.doFinal(btxt);
        String encryptedtext = Base64.encodeToString(ciphertext, 0);

        //Decryption process
        byte[] bencryptxt = Base64.decode(encryptedtext, 0);
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv)); 
        ciphertext = cipher.doFinal(bencryptxt);
        String cipherS = Base64.encodeToString(ciphertext, 0); 

        result.setText(cipherS);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (GeneralSecurityException e) {
        e.printStackTrace();
    } 

}

}

Can someone knows why is this happening or any advice to be able to get the correct decrypted message?

like image 350
dvv Avatar asked Nov 14 '22 01:11

dvv


1 Answers

If you take out the encrypt-decrypt, which should be an identity transformation, what remains is:

Base64.encodeToString(Base64.decode(msg))

"Msg 1.5 to encrypt" isn't a Base64-encoded string, there's no need to try to decode it. If you do, as you do, non-Base64 characters get stripped and you get some bytes which, when encoded back, look like the result you get.

like image 88
aaz Avatar answered Dec 05 '22 00:12

aaz