Commit ef9df30c authored by yanzg's avatar yanzg

修改实体位置

parent 4d193721
package com.yanzuoguang.util.helper;
import com.yanzuoguang.util.contants.SystemContants;
import com.yanzuoguang.util.exception.CodeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
/**
* RSA
*
* @author 颜佐光
*/
public final class RsaHelper {
private static final Logger logger = LoggerFactory.getLogger(RsaHelper.class);
private static final String ALGORITHM_RSA = "RSA";
private static final String ALGORITHM_SIGN = "MD5withRSA";
private static final int KEYPAIR_LEN = 1024;
private RsaHelper() {
super();
}
/**
* 生成密钥对
*
* @throws Exception
*/
public static void generatorKeyPair() throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM_RSA);
keyPairGen.initialize(KEYPAIR_LEN);
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
byte[] keyBs = rsaPublicKey.getEncoded();
String publicKey = encodeBase64(keyBs);
logger.info("生成的公钥:\t{}", publicKey);
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
keyBs = rsaPrivateKey.getEncoded();
String privateKey = encodeBase64(keyBs);
logger.info("生成的私钥:\t{}", privateKey);
}
/**
* 获取公钥
*
* @return
* @throws Exception
*/
private static PublicKey getPublicKey(String publicKey) throws Exception {
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(decodeBase64(publicKey));
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA);
return keyFactory.generatePublic(publicKeySpec);
}
/**
* 获取私钥
*
* @return
* @throws Exception
*/
private static PrivateKey getPrivateKey(String privateKey) throws Exception {
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(decodeBase64(privateKey));
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA);
return keyFactory.generatePrivate(privateKeySpec);
}
/**
* 公钥加密
*
* @param source
* @param publicKeyStr
* @return
* @throws Exception
*/
public static String encryptionByPublicKey(String source, String publicKeyStr) {
try {
PublicKey publicKey = getPublicKey(publicKeyStr);
Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
cipher.update(source.getBytes(SystemContants.UTF8));
String target = encodeBase64(cipher.doFinal());
return target;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
/**
* 公钥解密
*
* @param target
* @throws Exception
*/
public static String decryptionByPublicKey(String target, String publicKeyStr) throws Exception {
PublicKey publicKey = getPublicKey(publicKeyStr);
Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicKey);
cipher.update(decodeBase64(target));
return new String(cipher.doFinal(), SystemContants.UTF8);
}
/**
* 公钥验证签名
*
* @return
* @throws Exception
*/
public static void verifyByPublicKey(String target, String sign, String publicKeyStr) throws Exception {
PublicKey publicKey = getPublicKey(publicKeyStr);
Signature signature = Signature.getInstance(ALGORITHM_SIGN);
signature.initVerify(publicKey);
signature.update(target.getBytes(SystemContants.UTF8));
if (signature.verify(decodeBase64(sign))) {
logger.info("sign true");
} else {
logger.info("sign false");
}
}
/**
* 私钥加密
*
* @param source
* @return
* @throws Exception
*/
public static String encryptionByPrivateKey(String source, String privateKeyStr) throws Exception {
PrivateKey privateKey = getPrivateKey(privateKeyStr);
Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
cipher.update(source.getBytes(SystemContants.UTF8));
String target = encodeBase64(cipher.doFinal());
return target;
}
/**
* 私钥解密
*
* @param target
* @throws Exception
*/
public static String decryptionByPrivateKey(String target, String privateKeyStr) {
try {
PrivateKey privateKey = getPrivateKey(privateKeyStr);
Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
cipher.update(decodeBase64(target));
return new String(cipher.doFinal(), SystemContants.UTF8);
} catch (Exception ex) {
throw new CodeException("解密失败:" + ex.getMessage(), ex);
}
}
/**
* 私钥签名
*
* @param target
* @return
* @throws Exception
*/
public static String signByPrivateKey(String target, String privateKeyStr) throws Exception {
PrivateKey privateKey = getPrivateKey(privateKeyStr);
Signature signature = Signature.getInstance(ALGORITHM_SIGN);
signature.initSign(privateKey);
signature.update(target.getBytes(SystemContants.UTF8));
String sign = encodeBase64(signature.sign());
return sign;
}
/**
* base64编码
*
* @param source
* @return
* @throws Exception
*/
private static String encodeBase64(byte[] source) throws Exception {
return new String(Base64.getEncoder().encode(source), SystemContants.UTF8);
}
/**
* Base64解码
*
* @param target
* @return
* @throws Exception
*/
private static byte[] decodeBase64(String target) throws Exception {
return Base64.getDecoder().decode(target.getBytes(SystemContants.UTF8));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment