Java中使用OpenSSL RSA公私钥进行数据加解密保存

使用openSSL 生成公私钥可以参考作者 JackCousins 的文章 @http://blog.csdn.net/chaijunkun/article/details/7275632 写的非常好,鼓掌
但在使用的过程中发现 ,例如 我在手机中可以使用公钥签名,服务器daunting使用私钥解密,在这个过程中需要传递数据,那么就是string类型的,故我们可以稍微改一下 代码如下  ,做个笔记标记一下
    private PrivateKey getPrivateKey() throws Exception {
        InputStream fis = new FileInputStream(new File("pkcs8_rsa_private_key.pem"));
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
        String key = br.readLine();
        br.close();
        fis.close();
        BASE64Decoder base64Decoder = new BASE64Decoder();
        byte[] buffer = base64Decoder.decodeBuffer(key);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(keySpec);

    }

    private PublicKey getPublicKey() throws Exception {
        InputStream fis = new FileInputStream(new File("rsa_public_key.pem"));
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
        String key = br.readLine();
        br.close();
        fis.close();
        BASE64Decoder base64Decoder = new BASE64Decoder();
        byte[] buffer = base64Decoder.decodeBuffer(key);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
        return keyFactory.generatePublic(keySpec);
    }

    private String encryptByPublicKey(PublicKey publicKey, byte[] data) throws Exception {
        //        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        BASE64Encoder base64Encoder = new BASE64Encoder();
        return base64Encoder.encode(cipher.doFinal(data, 0, data.length));
    }

    public String decrypt(String cipherData) throws Exception {
        Cipher cipher = null;
        cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, getPrivateKey());
        BASE64Decoder base64Decoder = new BASE64Decoder();
        byte[] output = cipher.doFinal(base64Decoder.decodeBuffer(cipherData));
        return new String(output);

    }

    public String encode(String text) {
        byte[] data = text.getBytes();
        String value = null;
        try {
            value = new String(encryptByPublicKey(getPublicKey(), data));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }

    public static void main(String[] args) throws Exception {
        HttpRSAUtil httpRSAUtil = new HttpRSAUtil();
        String encoder = httpRSAUtil.encryptByPublicKey(httpRSAUtil.getPublicKey(),
                "123456".getBytes());
        System.out.println(encoder);
        System.err.println(httpRSAUtil.decrypt(encoder));
        //        encryptByPublicKey
    }


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值