Android---解密服务器RSA内容

重点:
1.加密内容/key转byte数组,用Base64工具;
2.获取私钥key,规则是 KeyFactory.getInstance("RSA", "BC");// 入参“BC”不可以省略;
3.解密的时候,规则是Cipher.getInstance("RSA/ECB/PKCS1Padding");
4.返回的byte数组,用new String(byte[]);打印

    public void doSomething(String enContent) {
                    byte[] encryptContent = Base64.decode(enContent, Base64.DEFAULT);// enContent,加密内容。。。。务必用Base64解密
                    byte[] privateKeyContent = Base64.decode(privateKey, Base64.DEFAULT);//privateKey 私钥

                    byte[] bytes1 = decryptByPrivateKey(encryptContent, privateKeyContent);
                    String resultString = new String(bytes1);
                    int result = StringUtils.tranString2Int(resultString);
                    LogUtil.d(TAG,"Enter getVerifiyPercentage resultString : " + resultString);
                    LogUtil.d(TAG,"Enter getVerifiyPercentage result : " + result);
                }

public static byte[] decryptByPrivateKey(byte[] encrypted, byte[] privateKey) {
    // 得到私钥对象
    byte[] arr = null;
    try {
        // 得到私钥
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey);
        KeyFactory kf = KeyFactory.getInstance("RSA", "BC");// 入参“BC”不可以省略
        PrivateKey privateK = kf.generatePrivate(keySpec);

        // 解密数据
        Cipher cp = Cipher.getInstance("RSA/ECB/PKCS1Padding");// 与Java解密不一样
        cp.init(Cipher.DECRYPT_MODE, privateK);
        arr = cp.doFinal(encrypted);
    } catch (Exception e) {
        LogUtil.d(LoginPresenter.class, "decryptByPrivateKey Exception ");
        e.printStackTrace();
    }
    return arr;
}

你可能感兴趣的:(Android---解密服务器RSA内容)