|
@ -1,182 +0,0 @@
|
|
|
package com.yihu.jw.patient.util;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.security.*;
|
|
|
import java.security.interfaces.RSAPrivateKey;
|
|
|
import java.security.interfaces.RSAPublicKey;
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
|
import com.yihu.jw.entity.base.security.RSA;
|
|
|
import com.yihu.jw.patient.dao.security.RSADao;
|
|
|
import com.yihu.jw.util.common.RSAUtils;
|
|
|
import org.apache.commons.codec.binary.Hex;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
@Service
|
|
|
public class RSAService {
|
|
|
|
|
|
@Autowired
|
|
|
private RSADao rsaDao;
|
|
|
/**
|
|
|
* 缓存的密钥对。
|
|
|
*/
|
|
|
private KeyPair oneKeyPair = null;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 读取RSA加密信息
|
|
|
* @return
|
|
|
*/
|
|
|
public RSA loadRSA() {
|
|
|
Iterable<RSA> iterable = rsaDao.findAll();
|
|
|
if (iterable != null) {
|
|
|
Iterator<RSA> it = iterable.iterator();
|
|
|
if (it != null && it.hasNext()) {
|
|
|
return it.next();
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
public KeyPair getKeyPair() {
|
|
|
if (oneKeyPair == null) {
|
|
|
// 从数据库加载
|
|
|
RSA rsa = loadRSA();
|
|
|
if (rsa == null) {
|
|
|
// 生成密钥
|
|
|
generateKeyPair();
|
|
|
} else {
|
|
|
// 由数据库取出来
|
|
|
Object obj = toObject(rsa.getData());
|
|
|
oneKeyPair = (KeyPair) obj;
|
|
|
}
|
|
|
}
|
|
|
return oneKeyPair;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 生成并返回RSA密钥对。
|
|
|
*/
|
|
|
private synchronized KeyPair generateKeyPair() {
|
|
|
try {
|
|
|
oneKeyPair = RSAUtils.initKey();
|
|
|
// 保存到数据库
|
|
|
saveRSA(oneKeyPair);
|
|
|
return oneKeyPair;
|
|
|
} catch (InvalidParameterException ex) {
|
|
|
ex.printStackTrace();
|
|
|
} catch (NullPointerException ex) {
|
|
|
ex.printStackTrace();
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 保存RSA加密信息
|
|
|
* @return
|
|
|
*/
|
|
|
public RSA saveRSA(KeyPair keyPair) {
|
|
|
RSA rsa = new RSA();
|
|
|
rsa.setData(toByteArray(keyPair));
|
|
|
// 先清空
|
|
|
rsaDao.deleteAll();
|
|
|
// 再添加
|
|
|
return rsaDao.save(rsa);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 返回已初始化的默认的公钥。
|
|
|
*/
|
|
|
public RSAPublicKey getDefaultPublicKey() {
|
|
|
KeyPair keyPair = getKeyPair();
|
|
|
if (keyPair != null) {
|
|
|
return (RSAPublicKey) keyPair.getPublic();
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
public String getModulus() {
|
|
|
return new String(Hex.encodeHex(getDefaultPublicKey().getModulus().toByteArray()));
|
|
|
}
|
|
|
|
|
|
public String getExponent() {
|
|
|
return new String(Hex.encodeHex(getDefaultPublicKey().getPublicExponent().toByteArray()));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 对象转数组
|
|
|
*
|
|
|
* @param obj
|
|
|
* @return
|
|
|
*/
|
|
|
public byte[] toByteArray(Object obj) {
|
|
|
byte[] bytes = null;
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
try {
|
|
|
ObjectOutputStream oos = new ObjectOutputStream(bos);
|
|
|
oos.writeObject(obj);
|
|
|
oos.flush();
|
|
|
bytes = bos.toByteArray();
|
|
|
oos.close();
|
|
|
bos.close();
|
|
|
} catch (IOException ex) {
|
|
|
ex.printStackTrace();
|
|
|
}
|
|
|
return bytes;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 数组转对象
|
|
|
*
|
|
|
* @param bytes
|
|
|
* @return
|
|
|
*/
|
|
|
public Object toObject(byte[] bytes) {
|
|
|
Object obj = null;
|
|
|
try {
|
|
|
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
|
|
ObjectInputStream ois = new ObjectInputStream(bis);
|
|
|
obj = ois.readObject();
|
|
|
ois.close();
|
|
|
bis.close();
|
|
|
} catch (IOException ex) {
|
|
|
ex.printStackTrace();
|
|
|
} catch (ClassNotFoundException ex) {
|
|
|
ex.printStackTrace();
|
|
|
}
|
|
|
return obj;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 使用默认的私钥解密给定的字符串。
|
|
|
* <p>
|
|
|
* 若{@code encrypttext} 为 {@code null}或空字符串则返回 {@code null}。
|
|
|
* 私钥不匹配时,返回 {@code null}。
|
|
|
*
|
|
|
* @param encrypttext 密文。
|
|
|
* @return 原文字符串。
|
|
|
*/
|
|
|
public String decryptString(String encrypttext) {
|
|
|
if (StringUtils.isBlank(encrypttext)) {
|
|
|
return null;
|
|
|
}
|
|
|
try {
|
|
|
byte[] en_data = Hex.decodeHex(encrypttext.toCharArray());
|
|
|
byte[] data = RSAUtils.decrypt((RSAPrivateKey) getKeyPair().getPrivate(), en_data);
|
|
|
return new String(data);
|
|
|
} catch (NullPointerException ex) {
|
|
|
ex.printStackTrace();
|
|
|
} catch (Exception ex) {
|
|
|
ex.printStackTrace();
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|