Android Base64编码和Apache编解码器解码

时间:2015-05-22 16:58:48

标签: java android encryption base64 rsa

我们使用以下方法使用ANDROID Base64.NO_CLOSE

对字符串进行编码
    public static String encrypt(String inputString, byte[] keyBytes) {
    Calendar cal = Calendar.getInstance();
    int mDay = cal.get(Calendar.DAY_OF_MONTH);
    // System.out.println("Day of month :::" + mDay);
    String encryptedString = "";
    Key publicKey = null;
    try {
        Random generator = new Random(mDay);
        int num = (generator.nextInt()) % 100;
        String salt = "WEER563784" + num;
        inputString += salt;
        X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        publicKey = keyFactory.generatePublic(publicKeySpec);
    } catch (Exception e) {
        System.out.println("Exception rsaEncrypt:::::::::::::::::  "
                + e.getMessage());
        e.printStackTrace();
    }
    // Encode the original data with RSA public key
    byte[] encodedBytes = null;
    try {
        Cipher c = Cipher.getInstance("RSA");
        c.init(Cipher.ENCRYPT_MODE, publicKey);
        encodedBytes = c.doFinal(inputString.getBytes());
        encryptedString = Base64.encodeToString(encodedBytes,
                Base64.NO_CLOSE);
        System.out.println(encryptedString);
    } catch (Exception e) {
        System.out.println("Exception rsaEncrypt:::::::::::::::::  "
                + e.getMessage());
        e.printStackTrace();
    }

    return encryptedString;
}

使用以下方法

在Android应用外部解密生成的加密字符串
public static String decrypt(String inputString, byte[] keyBytes) {
        String resultStr = null;
        Calendar cal = Calendar.getInstance();
        int mDay = cal.get(Calendar.DAY_OF_MONTH);
        Random generator = new Random(mDay);
        int num = (generator.nextInt()) % 100;
        String salt = "qqq" + num;
        PrivateKey privateKey = null;
        try {
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(keyBytes);
            privateKey = keyFactory.generatePrivate(privateKeySpec);
        } catch (Exception e) {
            System.out.println("Exception privateKey:::::::::::::::::  "
                    + e.getMessage());
        }
        byte[] decodedBytes = null;
        try {
            Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            // Cipher c = Cipher.getInstance("RSA");
            c.init(Cipher.DECRYPT_MODE, privateKey);
            // decodedBytes = c.doFinal(Base64.decodeBase64(inputString));
            decodedBytes = c.doFinal(Base64InputStream());

        } catch (Exception e) {
            System.out.println("Exception privateKey1:::::::::::::::::  "
                    + e.getMessage());
            e.printStackTrace();
        }
        if (decodedBytes != null) {
            resultStr = new String(decodedBytes);
            System.out.println("resultStr:::" + resultStr + ":::::");
            resultStr = resultStr.replace(salt, "");
        }
        return resultStr;

    }

我们正在获得以下例外

javax.crypto.BadPaddingException: Decryption error
    at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380)
    at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)
    at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:365)
    at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:391)
    at javax.crypto.Cipher.doFinal(Cipher.java:2087)
    at RSAEncryption.decrypt(RSAEncryption.java:41)
    at RSAEncryption.main(RSAEncryption.java:108)

所以问题是

1)是否可以在Android之外使用ANDROID Base64.NO_CLOSE解密加密字符串,我的意思是直接在IDE中?

2)在其中一篇帖子中我发现使用ANDROID Base64.NO_WRAP加密的字符串可以在Android环境之外解密,这是正确的理解吗?

非常感谢你的帮助。

此致 阿米特

1 个答案:

答案 0 :(得分:0)

问题已修复......

解密时我们需要使用RSA / ECB / NoPadding

基本上,当我们在Android设备中使用RSA加密值时,在单独的独立Java环境中解密应该在密码中使用RSA / ECB / NoPadding。

Android加密代码:

public static String encrypt(String inputString, byte[] keyBytes) {
        Calendar cal = Calendar.getInstance();
        int mDay = cal.get(Calendar.DAY_OF_MONTH);
        // System.out.println("Day of month :::" + mDay);
        String encryptedString = "";
        Key publicKey = null;
        try {
            Random generator = new Random(mDay);
            int num = (generator.nextInt()) % 100;
            String salt = "MNSadm563784" + num;
            //inputString += salt;
            X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(keyBytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            publicKey = keyFactory.generatePublic(publicKeySpec);
        } catch (Exception e) {
            System.out.println("Exception rsaEncrypt:::::::::::::::::  "
                    + e.getMessage());
            e.printStackTrace();
        }
        // Encode the original data with RSA public key
        byte[] encodedBytes = null;
        try {
            Cipher c = Cipher.getInstance("RSA");
            c.init(Cipher.ENCRYPT_MODE, publicKey);
            encodedBytes = c.doFinal(inputString.getBytes());
            encryptedString = Base64.encodeToString(encodedBytes,
                    Base64.NO_CLOSE);
            System.out.print("-----??"+encryptedString+"??-------");
        } catch (Exception e) {
            System.out.println("Exception rsaEncrypt:::::::::::::::::  "
                    + e.getMessage());
            e.printStackTrace();
        }

        return encryptedString;
    }

服务器解密代码:

public static String decrypt(String inputString, byte[] keyBytes) {
        String resultStr = null;
//      Calendar cal = Calendar.getInstance();
//      int mDay = cal.get(Calendar.DAY_OF_MONTH);
//      Random generator = new Random(mDay);
//      int num = (generator.nextInt()) % 100;
//      String salt = "MNSadm563784" + num;
        PrivateKey privateKey = null;
        try {
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(
                    keyBytes);
            privateKey = keyFactory.generatePrivate(privateKeySpec);
        } catch (Exception e) {
            System.out.println("Exception privateKey:::::::::::::::::  "
                    + e.getMessage());
            e.printStackTrace();
        }
        byte[] decodedBytes = null;
        try {
            Cipher c = Cipher.getInstance("RSA/ECB/NoPadding");
            // Cipher c = Cipher.getInstance("RSA");
            c.init(Cipher.DECRYPT_MODE, privateKey);
            decodedBytes = c.doFinal(Base64.decodeBase64(inputString));
            // decodedBytes = c.doFinal(Base64InputStream());

        } catch (Exception e) {
            System.out.println("Exception privateKey1:::::::::::::::::  "
                    + e.getMessage());
            e.printStackTrace();
        }
        if (decodedBytes != null) {
            resultStr = new String(decodedBytes);
            System.out.println("resultStr:::" + resultStr + ":::::");
//          resultStr = resultStr.replace(salt, "");
        }
        return resultStr;

    }