package com.genersoft.pub.security;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.cert.Certificate;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Enumeration;
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 PUBLIC_KEY = "publicKey";
public static final String PRIVATE_KEY = "privateKey";
public static final String SB_PUBLIC_KEY = "sb_publicKey";
public static final String SB_PRIVATE_KEY = "sb_privateKey";
private static Map<String,byte[]> keyMap = new HashMap<String,byte[]>();
/**
* base64编码
* @param data
* @return
*/
public static String base64Encode(byte[] data) {
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
/**
* base64解码
* @param data
* @return
* @throws IOException
*/
public static byte[] base64Decode(String data) throws IOException {
BASE64Decoder encoder = new BASE64Decoder();
return encoder.decodeBuffer(data);
}
/**
* 生成密钥对
* @return
*/
public static KeyPair getRsaKP() throws Exception {
KeyPairGenerator kpg = null;
try {
kpg = KeyPairGenerator.getInstance("RSA"); // 创建‘密钥对’生成器
kpg.initialize(1024, new SecureRandom()); // 指定密钥长度
KeyPair kp = kpg.genKeyPair(); // 生成‘密钥对
return kp;
} catch (Exception e) {
throw new Exception("失败");
}
}
/**
* 还原公钥,X509EncodedKeySpec 用于构建公钥的规范
* @param keyBytes
* @return
*/
public static PublicKey restorePublicKey(byte[] keyBytes) {
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);
try {
KeyFactory factory = KeyFactory.getInstance("RSA");
PublicKey publicKey = factory.generatePublic(x509EncodedKeySpec);
return publicKey;
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
return null;
}
/**
* 还原私钥,PKCS8EncodedKeySpec 用于构建私钥的规范
* @param keyBytes
* @return
*/
public static PrivateKey restorePrivateKey(byte[] keyBytes) {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);
try {
KeyFactory factory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = factory.generatePrivate(pkcs8EncodedKeySpec);
return privateKey;
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
/**
* RSA加密
* @param pubkey 公钥
* @param data 明文数据
* @return
*/
public static byte[] rsaEncrypt(PublicKey key, byte[] data)throws Exception {
try {
Cipher desCipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
desCipher.init(Cipher.ENCRYPT_MODE, key);
return desCipher.doFinal(data);
} catch (Exception e) {
throw new Exception("RSA加密失败");
}
}
/**
* RSA解密
* @param pubkey 公钥
* @param data 明文数据
* @return
*/
public static String rsaDecrypt(PrivateKey key, byte[] data)
throws Exception {
try {
Cipher desCipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
desCipher.init(Cipher.DECRYPT_MODE, key);
return new String(desCipher.doFinal(data),"UTF-8");
} catch (Exception e) {
throw new Exception("RSA解密失败");
}
}
/**
* RSA解密
* @param pubkey 公钥
* @param data 明文数据
* @return
*/
public static byte[] rsaDecrypt_Js(PrivateKey key, byte[] data)
throws Exception {
try {
Cipher desCipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
desCipher.init(Cipher.DECRYPT_MODE, key);
return desCipher.doFinal(data);
} catch (Exception e) {
throw new Exception("RSA解密失败");
}
}
/**
* 签名
* @param key 公钥
* @param data 数据
* @return
*/
public static byte[] sign(PrivateKey key, byte[] data) throws Exception {
Signature signature = null;
try {
signature = Signature.getInstance("SHA1WithRSA");
signature.initSign(key);
signature.update(data);
return signature.sign();
} catch (Exception e) {
throw new Exception("签名失败");
}
}
/**
* 签名
* @param key 公钥
* @param data 数据
* @return
*/
public static byte[] signSunRsaSign(PrivateKey key, byte[] data) throws Exception {
Signature signature = null;
try {
signature = Signature.getInstance("SHA1withRSA", "SunRsaSign");
signature.initSign(key);
signature.update(data);
return signature.sign();
} catch (Exception e) {
throw new Exception("签名失败");
}
}
/**
* 签名验证
* @param key 公钥
* @param data 待验证数据
* @param signData 签名数据
* @return
* @throws Exception
*/
public static boolean verify(PublicKey key, byte[] data,byte[] signData) throws Exception {
Signature signature = null;
try {
signature = Signature.getInstance("SHA1WithRSA");
signature.initVerify(key);
signature.update(data);
return signature.verify(signData);
} catch (Exception e) {
throw new Exception("签名失败");
}
}
/**
* 签名验证
* @param oCert 证书
* @param data 待验证数据
* @param signData 签名数据
* @return
* @throws Exception
*/
public static boolean verify(Certificate oCert, byte[] data,byte[] signData) throws Exception {
Signature signature = null;
try {
signature = Signature.getInstance("SHA1withRSA", "SunRsaSign");
signature.initVerify(oCert);
signature.update(data);
return signature.verify(signData);
} catch (Exception e) {
throw new Exception("签名失败");
}
}
/**
* 根据私钥证书文件获取PrivateKey对象
* @param pfxFile 证书路径
* @param pfxPwd 证书密码
* @return PrivateKey 私钥对象
*/
public static PrivateKey GetPvkformPfx(String pfxFile, String pfxPwd) throws Exception{
FileInputStream fis = null;
try {
KeyStore ks = KeyStore.getInstance("PKCS12"); //PKCS12
fis = new FileInputStream(pfxFile);
char[] nPassword = null;
if ((pfxPwd == null) || pfxPwd.trim().equals("")){
nPassword = null;
}else{
nPassword = pfxPwd.toCharArray();
}
ks.load(fis, nPassword);
fis.close();
Enumeration enumas = ks.aliases();
String keyAlias = null;
if (enumas.hasMoreElements()){
keyAlias = (String)enumas.nextElement();
}
PrivateKey prikey = (PrivateKey) ks.getKey(keyAlias, nPassword);
return prikey;
}catch (Exception e){
e.printStackTrace();
throw new Exception("获取私钥对象失败");
}finally {
try {
if(fis != null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 签名验证
* @param key 公钥
* @param data 待验证数据
* @param signData 签名数据
* @return
* @throws Exception
*/
public static boolean verify1(byte[] key, byte[] data,byte[] signData) throws Exception {
Signature signature = null;
try {
KeyFactory factory = KeyFactory.getInstance("RSA");
signature = Signature.
评论2