/**
* 根据模和指数生成公key
*
* @param publicKeyStr
* @return
*/
public static RSAPublicKey getRSAPublicKey(String publicKeyStr) {
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(HexUtil.hex2Bytes(publicKeyStr));
RSAPublicKey publicKey = null;
try {
publicKey = (RSAPublicKey) KeyFactory.getInstance(AlgorithmConstant.ALGORITHM).generatePublic(x509EncodedKeySpec);
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return publicKey;
}
/**
* 根据模和指数生成私key
*
* @param privateKeyStr
* @return
*/
public static RSAPrivateKey getRSAPrivateKey(String privateKeyStr) {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(HexUtil.hex2Bytes(privateKeyStr));
RSAPrivateKey privateKey = null;
try {
privateKey = (RSAPrivateKey) KeyFactory.getInstance(AlgorithmConstant.ALGORITHM).generatePrivate(pkcs8EncodedKeySpec);
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return privateKey;
}
/**
* 生成公钥和私钥
* @throws NoSuchAlgorithmException
*
*/
public static String getKeys(String mapKey) throws NoSuchAlgorithmException {
HashMap<String, String> map = new HashMap<String, String>();
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
map.put("public", HexUtil.bytes2Hex(publicKey.getEncoded()));
map.put("private", HexUtil.bytes2Hex(privateKey.getEncoded()));
return map.get(mapKey);
}
// 对content字段做MD5摘要,再对摘要进行rsa私钥加密,对加密后的字符串做base64编码
public String getSing(String msg, RSAPrivateKey privateKey) throws Exception {
// md5
MessageDigest alg = MessageDigest.getInstance("MD5");//生成信息摘要。
byte[] data = alg.digest(msg.getBytes("utf-8"));//将要输入的数据转换成字节数组,并且计算摘要
// rsa
PrivateKey privateKey = RSAKeyUtil.getRSAPrivateKey(RSAKeyUtils.getKeys("private"));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] encrypted = cipher.doFinal(data);
// base64
return new BASE64Encoder().encode(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。
}
AES加密
sSrc:内容
sKey:秘钥
public static String Encrypt(String sSrc, String sKey) throws Exception {
if (sKey == null) {
System.out.print("Key为空null");
return null;
}
// 判断Key是否为16位
if (sKey.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式"
IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());//使用CBC模式,需要一个向量iv,可增加加密算法的强度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes());
return new BASE64Encoder().encode(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。
}