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<String,Object> values = new HashMap<String, Object>();
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加密:

加密:


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

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解密


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;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值