在Android中使用AES / CBC / PKCS5Padding不正确解密 [英] incorrect decryption using AES/CBC/PKCS5Padding in Android

查看:246
本文介绍了在Android中使用AES / CBC / PKCS5Padding不正确解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid(V2.2 API 8),其中,一个纯文本输入,并使用用户的密码和随机盐,它在code加密写了下面的code,然后对它进行解密。运行code之后,我只得到的纯文本正确的部分。例如,用户输入消息1.5加密,并从解密code中的结果是Msg15toencrypg ==

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=="

下面是code:     

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?

推荐答案

如果你把加密,解密,这应该是一个身份的转变,剩下的就是:

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

Base64.encodeToString(Base64.decode(msg))

消息1.5加密不是Base64的恩codeD字符串,没有必要试图去code吧。如果你这样做,因为你这样做,非Base64的角色获得剥夺,你会得到一些字节,当EN codeD回来了,看起来就像你得到的结果。

"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.

这篇关于在Android中使用AES / CBC / PKCS5Padding不正确解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆