非对称RAS加密解密

package com.cnhis.cloudhealth.open.rest.user;

import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class RSAUtils {
    public static final String KEY_ALGORITHM = "RSA";
    private static final int KEY_SIZE = 1024;
    private static final String PUBLIC_KEY = "RSAPublicKey";
    private static final String PRIVATE_KEY = "RSAPrivateKey";

    public static String bytesToString(byte[] encrytpByte) {
        String result = "";
        byte b;
        int i;
        byte[] arrayOfByte;
        for (i = (arrayOfByte = encrytpByte).length, b = 0; b < i;) {
            Byte bytes = Byte.valueOf(arrayOfByte[b]);
            result = String.valueOf(result) + bytes.toString() + " ";
            b++;
        }

        return result;
    }

    public static String encryptByPrivateKey(String data, String key) throws Exception {
        byte[] keyBytes = decryptBASE64(key);

        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

        Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
        cipher.init(1, privateKey);
        String encryptString = "";
        byte[] dataBytes = data.getBytes();
        int length = dataBytes.length;
        byte[] b1 = cipher.doFinal(dataBytes);
        BASE64Encoder encoder = new BASE64Encoder();
        encryptString = encoder.encode(b1);

        return encryptString;
    }

    public static String encryptByPublicKey(String data, String key) throws Exception {
        byte[] keyBytes = decryptBASE64(key);

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);

        PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);

        Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
        cipher.init(1, pubKey);

        String encryptString = "";
        byte[] dataBytes = data.getBytes();
        int length = dataBytes.length;
        byte[] b1 = cipher.doFinal(dataBytes);
        BASE64Encoder encoder = new BASE64Encoder();
        encryptString = encoder.encode(b1);

        return encryptString;
    }

    public static String decryptByPrivateKey(String data, String key) throws Exception {
        byte[] keyBytes = decryptBASE64(key);

        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

        Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
        cipher.init(2, privateKey);

        BASE64Decoder decoder = new BASE64Decoder();
        StringBuilder sb = new StringBuilder();

        byte[] b1 = decoder.decodeBuffer(data);
        byte[] doFinal = cipher.doFinal(b1);
        sb.append(new String(doFinal));

        return sb.toString().trim();
    }

    public static String decryptByPublicKey(String data, String key) throws Exception {
        byte[] keyBytes = decryptBASE64(key);

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);

        PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);

        Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
        cipher.init(2, pubKey);

        BASE64Decoder decoder = new BASE64Decoder();
        StringBuilder sb = new StringBuilder();

        byte[] b1 = decoder.decodeBuffer(data);
        byte[] doFinal = cipher.doFinal(b1);
        sb.append(new String(doFinal));

        return sb.toString().trim();
    }

    public static Map<String, Object> initKey() throws Exception {
        SecureRandom sr = new SecureRandom();

        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");

        keyPairGenerator.initialize(1024, sr);

        KeyPair keyPair = keyPairGenerator.generateKeyPair();

        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

        Map<String, Object> keyMap = new HashMap<String, Object>();
        keyMap.put("RSAPublicKey", publicKey);
        keyMap.put("RSAPrivateKey", privateKey);
        return keyMap;
    }

    public static String getPrivateKey(Map<String, Object> keyMap) throws Exception {
        Key key = (Key) keyMap.get("RSAPrivateKey");
        return encryptBASE64(key.getEncoded());
    }

    public static String getPublicKey(Map<String, Object> keyMap) throws Exception {
        Key key = (Key) keyMap.get("RSAPublicKey");
        return encryptBASE64(key.getEncoded());
    }

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

    public static String encryptBASE64(byte[] key) throws Exception {
        return (new BASE64Encoder()).encodeBuffer(key);
    }

    public static void main(String[] args) throws Exception {
    }
}
 

 

调用

RSAUtils.encryptByPublicKey(data,publicKey).replaceAll("\r\n|\r|\n","");

data是参数

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值