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是参数