RSA加密解密

AES+RSA加密:

String url = config.getHostIp() + path;

EncryptMessageModel model = new EncryptMessageModel(config.getIdentity(), config.getPassword(), content);
String encryCode = EncryptHelper.encrypt(model, config.getRSAPublicKey(), config.getAesPassword());
Map values = new HashMap();
values.put("content", encryCode);

String result = CeaHttpClientHelper.postObject(url, values); ---->url是可读的,values里的内容加密不可读

例如:{"aa":1250","bb":"TICKET","cc":"20180710XX1239713","dd":"B2C2018070XXX58504576","ee":"987XX3873636"}

一般RSA不做上例文本整个内容加密,只对核心数据的交换进行加密,长文本用普通的对称加密AES,再对其中核心数据进行RSA校验,这样就可以保证数据安全

但有时候为了简单就是用了RSA加密整个长文本,那么RSA加解密的过程如下:

RSA加密:

加密:

RSA加密解密_第1张图片

---------------------------------------------------------------------------------------------------

private static byte[] decryptBASE64(String key) throws Exception {
      return (new BASE64Decoder()).decodeBuffer(key);
}

----------------------------------------------------------------------------------------------

 public static String encryptByPublicKey(String data, RSAPublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        // 模长
        int key_len = publicKey.getModulus().bitLength() / 8;
        // 加密数据长度 <= 模长-11
        String[] datas = splitString(data, key_len - 11);
        String mi = "";
        // 如果明文长度大于模长-11则要分组加密
        for (String s : datas) {
            mi += bcd2Str(cipher.doFinal(s.getBytes()));
        }
        return mi;

    }

------------------------------------------------------------------------------------------------------------


RSA解密

RSA加密解密_第2张图片

public static String decryptByPrivateKey(String data, RSAPrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        // 模长
        int key_len = privateKey.getModulus().bitLength() / 8;
        byte[] bytes = data.getBytes();
        byte[] bcd = ASCII_To_BCD(bytes, bytes.length);
        // 如果密文长度大于模长则要分组解密
        String ming = "";
        byte[][] arrays = splitArray(bcd, key_len);
        for (byte[] arr : arrays) {
            ming += new String(cipher.doFinal(arr));
        }
        return ming;
    }

你可能感兴趣的:(运行过程类)