添加rsa加解密工具类

This commit is contained in:
winLsy
2018-02-08 13:53:59 +08:00
parent 1e4a76609c
commit fab23c6c04
@@ -0,0 +1,85 @@
package com.micro.fast.util;
import org.apache.commons.net.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.UnsupportedEncodingException;
import java.security.*;
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;
/**
* 添加rsa加解密工具类
*
* @author lsy
*/
public class RSAUtil {
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidKeySpecException {
String[] keys = keys();
String encode = jdkRSA(keys[1], "2018-1-1 2018-12-30");
System.out.println("使用私钥加密后的内容为:"+encode);
String decode = deJdkRSA(keys[0], encode);
System.out.println("使用公钥解密后的内容为:"+decode);
}
/**
* 生成公钥私钥
* @return
* @throws NoSuchAlgorithmException
*/
public static String[] keys() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(512);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
return new String[]{Base64.encodeBase64String(rsaPublicKey.getEncoded()),Base64.encodeBase64String(rsaPrivateKey.getEncoded())};
}
/**
* 私钥加密内容
* @param rsaPrivateKey
* @param content
* @return
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws UnsupportedEncodingException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public static String jdkRSA(String rsaPrivateKey, String content) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(rsaPrivateKey));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] result = cipher.doFinal(content.getBytes("UTF-8"));
content = Base64.encodeBase64String(result);
return content;
}
/**
* 使用公钥解密内容
* @param rsaPublicKey
* @param content
* @return
*/
public static String deJdkRSA(String rsaPublicKey, String content) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Cipher cipher = Cipher.getInstance("RSA");
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(rsaPublicKey));
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] result = cipher.doFinal(Base64.decodeBase64(content));
return new String(result);
}
}