openSSL 实现登录加密,解密
package com.iorange.research.common.core.tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
/**
* ssl 加密解密
*/
@Component
@Slf4j
public class OpenSSLVerify {
private final static String PRIVATE_KEY =
"MIICXgIBAAKBgQDY+w8lFisfSa4Pwok3iCjFHSpoUOTnZRcnGr7NJOCpsa5lzgq3" +
"EMIXbKYiwT66COfljFF3pz/fi0brRCsH1WXaDEVbBc4bR7ZZzE485uRdiNDX15cl" +
"ETEfzfcugQ1cKuOXYF9n+xYUnGk7A/qbRN1FubSPTisNmE+x+tTBxC2wvQIDAQAB" +
"AoGBAMe2JRVslxpANd65npSnVWdAVgUX1+iHak0K1+138PUf3rZDdCdVXGA1F6FL" +
"ntGaWt80TNPF9AtsZUUsRDL8nrXl756MsChIfd1UkzbAS7tZD48pyd4m33+Bz4rm" +
"+NgFgh0AGTy2UshRNKdGoRiMOmN+5/WYfKcI0BWQ1CUz2EeBAkEA+0VsHZvBnoqk" +
"+xLkaJoct/BOh+maA90EfL7Q1OoKP+g0Ke2Z36hyNITLvOfnvwYVM+zGwkJk8FsI" +
"5sQceZ9DIQJBAN0QboNdmtUkTA/OhUgsWgwSf+R1VQgiqszQB4JjD2Er5wD2DU9h" +
"d+6sE7rdHKyQEPARiGbBkdjYe7K0nX/WVh0CQQCazZJmS2dl/ZTW+jSuHQREblBz" +
"e3/fkXMKR6TxB594793zVubN3EFACfBbWaR1E5JMgzuQwsbdLbi2M2w3od8BAkEA" +
"0YBKC6sBGm/suaox+8U0jW5W0A82R6B7F+EqzLHOmHt/0BRfZtRrUcC4bgYwr4VU" +
"1/71stRNwSDgkerDvKxHLQJAJ6v4cA9I+3XZmjwvf2cUIQHsU/K1DmgxWkIrsasb" +
"cxlfMr3U6s9ngySas7JcsHimKZnxSAq/dY/TdfOX9YMkDQ==";
private final static String PUBLIC_KEY =
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDY+w8lFisfSa4Pwok3iCjFHSpo" +
"UOTnZRcnGr7NJOCpsa5lzgq3EMIXbKYiwT66COfljFF3pz/fi0brRCsH1WXaDEVb" +
"Bc4bR7ZZzE485uRdiNDX15clETEfzfcugQ1cKuOXYF9n+xYUnGk7A/qbRN1FubSP" +
"TisNmE+x+tTBxC2wvQIDAQAB";
/**
* 字节数据转字符串专用集合
*/
private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* 根据公钥字符串加载公钥
*
* @param publicKeyStr 公钥字符串
* @return
* @throws Exception
*/
public static RSAPublicKey loadPublicKey(String publicKeyStr) throws Exception {
try {
//byte[] buffer = DatatypeConverter.parseBase64Binary(publicKeyStr);
//byte[] buffer = strToBase64(publicKeyStr);
byte[] buffer = Base64Utils.decodeFromString(publicKeyStr);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
return (RSAPublicKey) keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
throw new Exception("无此算法", e);
} catch (InvalidKeySpecException e) {
throw new Exception("公钥非法", e);
} catch (NullPointerException e) {
throw new Exception("公钥数据为空", e);
}
}
/**
* 根据私钥字符串加载私钥
*
* @param privateKeyStr 私钥字符串
* @return
* @throws Exception
*/
public static RSAPrivateKey loadPrivateKey(String privateKeyStr) throws Exception {
try {
//byte[] buffer = strToBase64(privateKeyStr);
//byte[] buffer = DatatypeConverter.parseBase64Binary(privateKeyStr);
byte[] buffer = Base64Utils.decodeFromString(privateKeyStr);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
} catch (NoSuchAlgorithmException e) {
throw new Exception("无此算法", e);
} catch (InvalidKeySpecException e) {
throw new Exception("私钥非法", e);
} catch (NullPointerException e) {
throw new Exception("私钥数据为空", e);
}
}
/**
* 公钥加密
*
* @param publicKey 公钥
* @param plainTextData 明文数据
* @return
* @throws Exception 加密过程中的异常信息
*/
public static String encrypt(RSAPublicKey publicKey, byte[] plainTextData) throws Exception {
if (publicKey == null) {
throw new Exception("加密公钥为空, 请设置");
}
Cipher cipher = null;
try {
// 使用默认RSA
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] output = cipher.doFinal(plainTextData);
return new String(base64ToStr(output));
} catch (NoSuchAlgorithmException e) {
throw new Exception("无此加密算法");
} catch (NoSuchPaddingException e) {
e.printStackTrace();
return null;
} catch (InvalidKeyException e) {
throw new Exception("加密公钥非法,请检查");
} catch (IllegalBlockSizeException e) {
throw new Exception("明文长度非法");
} catch (BadPaddingException e) {
throw new Exception("明文数据已损坏");
}
}
/**
* 私钥解密
*
* @param privateKey 私钥
* @param cipherData 密文数据
* @return 明文
* @throws Exception 解密过程中的异常信息
*/
public static String decrypt(RSAPrivateKey privateKey, byte[] cipherData) throws Exception {
if (privateKey == null) {
throw new Exception("解密私钥为空, 请设置");
}
Cipher cipher = null;
try {
// 使用默认RSA
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] output = cipher.doFinal(cipherData);
return new String(output,"UTF-8");
} catch (NoSuchAlgorithmException e) {
throw new Exception("无此解密算法");
} catch (NoSuchPaddingException e) {
e.printStackTrace();
return null;
} catch (InvalidKeyException e) {
throw new Exception("解密私钥非法,请检查");
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
throw new Exception("密文长度非法");
} catch (BadPaddingException e) {
e.printStackTrace();
throw new Exception("密文数据已损坏");
}
}
public static String base64ToStr(byte[] b) {
return Base64Utils.encodeToString(b);
}
public static byte[] strToBase64(String str) {
return Base64Utils.decodeFromString(str);
}
public static String jiemi(String str) throws Exception {
log.info("解密密文:" + str);
RSAPrivateKey rsaPrivateKey = loadPrivateKey(PRIVATE_KEY);
String decrypt = decrypt(rsaPrivateKey, strToBase64(str));
return decrypt;
}
public static String jiami(String str) throws Exception {
RSAPublicKey rsaPublicKey = loadPublicKey(PUBLIC_KEY);
String encrypt = encrypt(rsaPublicKey, str.getBytes("UTF-8"));
return encrypt;
}
}