无法在Android上解密AES?

时间:2011-07-21 23:31:17

标签: android base64 hex aes

我是android dev的新手,现在我在Android上实现了AES,它可以使用用户输入密码加密字符串。加密似乎很好,它可以省略Base64 / Hex编码的字符串。

但是当我尝试解密它时,问题出现了:通过解密,遗漏总是让我看到一堆乱七八糟的字符。

为了摆脱它,我尝试通过定义一个字符集(如UTF-8)来调试它,同时从字符串转换为byte [],但没有命中,并且还尝试使用base 64编码省略或Hex,但两者都失败了。

我还尝试使用cipher.getInstance方法定义AES / CBC / PKCS5Padding或AES,但仍然没有。

这是非常恼人的,你能帮帮我吗?

忘记提及我曾经问过类似的问题https://stackoverflow.com/questions/6727255/aes-decryption-on-android-not-correct,那里的语法问题已得到纠正。

以下是代码:

加密

public String AESEncrypt(String sKey, String PlainMsg)
                throws Exception {
            //Try use some Android based alert dialog to catch this exception.
            if (sKey == null) {
                Log.e("SecureChat", "IllegalArgumentException Catched");
                throw new IllegalArgumentException ("NULL Secret NOT ALLOWED!");
            }           
            /*Old Method
            //byte[] rawKey = getRawKey(sKey.getBytes("UTF-8"));
            byte[] rawKey = getRawKey(sKey.getBytes());
            //Encrypt start
            SecretKeySpec keySpec = new SecretKeySpec(rawKey, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, keySpec);
            //byte[] cipherText = cipher.doFinal(PlainMsg.getBytes("UTF-8"));
            byte[] cipherText = cipher.doFinal(PlainMsg.getBytes());
            return Base64Encoded(cipherText);
            */
            //New Method
            byte[] salt = getSalt();
            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHSHA256AND256BITAES-CBC-BC");
            KeySpec spec = new PBEKeySpec(sKey.toCharArray(), salt, 1024, 256); 
            SecretKey tmp = factory.generateSecret(spec);
            SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
            //Encryption Process
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secret);
            byte[] cipherText = cipher.doFinal(PlainMsg.getBytes());
            //return Base64Encoded(cipherText);
            //Hex
            return toHex(cipherText);
        }

解密

public String AESDecrypt(String sKey, String EncryptMsg)
                throws Exception {          
            /*Old Method
            //byte[] rawKey = getRawKey(sKey.getBytes("UTF-8"));
            byte[] rawKey = getRawKey(sKey.getBytes());
            SecretKeySpec keySpec = new SecretKeySpec(rawKey, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
            //byte[] plainText = Base64Decoded(EncryptMsg.getBytes("UTF-8"));
            byte[] plainText = Base64Decoded(EncryptMsg);           
            cipher.doFinal(plainText);
            return new String(plainText, "UTF-8");
            */
            //New Method
            byte[] salt = getSalt();
            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHSHA256AND256BITAES-CBC-BC");
            KeySpec spec = new PBEKeySpec(sKey.toCharArray(), salt, 1024, 256); 
            SecretKey tmp = factory.generateSecret(spec);
            SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
            //byte[] bCipherText = Base64Decoded(EncryptMsg);
            //Hex
            byte[] bCipherText = toByte(EncryptMsg);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secret);
            cipher.doFinal(bCipherText);
            return new String(bCipherText);
        }

        private byte[] getSalt() throws NoSuchAlgorithmException {
            /*Mark for old key method
            //Initialize the KeyGenerator
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
            sr.setSeed(seed);
            //Init for 256bit AES key
            kgen.init(Constants.AES_KEY_SIZE, sr);;
            SecretKey secret = kgen.generateKey();
            //Get secret raw key
            byte[] rawKey = secret.getEncoded();
            return rawKey;
            */

            //New key method with some salt
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            byte[] ransalt = new byte[20];
            random.nextBytes(ransalt);
            return ransalt;
        }

        @Override
        public byte[] getRawKey(byte[] seed) throws Exception {
            /*Old Method
            //Initialize the KeyGenerator
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
            sr.setSeed(seed);
            //Init for 256bit AES key
            kgen.init(Constants.AES_KEY_SIZE, sr);
            SecretKey secret = kgen.generateKey();
            //Get secret raw key
            byte[] rawKey = secret.getEncoded();
            return rawKey;
            */
            return null;
        }
/**
 * 
 * @param toBeDecoded
 * @return
 */
        public byte[] Base64Decoded(String toBeDecoded) {
            byte[] decoded = Base64.decode(toBeDecoded, 0);
            return decoded;
        }

        //Hex Mode
        public String toHex(String txt) {
            return toHex(txt.getBytes());
        }
        public String fromHex(String hex) {
            return new String(toByte(hex));
        }

        public byte[] toByte(String hexString) {
            int len = hexString.length()/2;
            byte[] result = new byte[len];
            for (int i = 0; i < len; i++)
                result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
            return result;
        }

        public String toHex(byte[] buf) {
            if (buf == null)
                return "";
            StringBuffer result = new StringBuffer(2*buf.length);
            for (int i = 0; i < buf.length; i++) {
                appendHex(result, buf[i]);
            }
            return result.toString();
        }
        private final String HEX = "0123456789ABCDEF";
        private void appendHex(StringBuffer sb, byte b) {
            sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
        }


    }

我在Stackoverflow上引用/比较了这些代码: Android aes encryption pad block corruptedincorrect decryption using AES/CBC/PKCS5Padding in Android

似乎我的问题在于charset编码,但我找不到问题所在。

非常感谢任何评论/答案! 谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

我写的这段代码完美无瑕。请看下面这个链接:

http://pocket-for-android.1047292.n5.nabble.com/Encryption-method-and-reading-the-Dropbox-backup-td4344194.html#a4454327

如果不仔细查看您的代码,我建议您在此处指定编码,但我不确定这是否是导致问题的原因:

byte[] cipherText = cipher.doFinal(PlainMsg.getBytes());

在这里:

return new String(bCipherText);