记录一下所学到的东西,不一定适合各种情况,因为架构的原因所以使用了jfinal的两个包,可以参考一下。
import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import javax.crypto.Cipher; import org.apache.commons.codec.binary.Base64; import com.jfinal.kit.StrKit; import com.jfinal.plugin.redis.Redis; public class RSAtool { /** * 生成密钥对 * @throws Exception */ public static void generatorKeyPair() throws Exception { KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM_RSA); keyPairGen.initialize(1024); KeyPair keyPair = keyPairGen.generateKeyPair(); RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); byte[] keyBs = rsaPublicKey.getEncoded(); String publicKey = encodeBase64(keyBs); RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); keyBs = rsaPrivateKey.getEncoded(); String privateKey = encodeBase64(keyBs); Redis.use().set("fp_publicKey", publicKey); Redis.use().set("fp_privateKey", privateKey); } /** * 获取公钥 * @return * @throws Exception */ public static PublicKey getPublicKey() throws Exception { X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(decodeBase64(getPublicKeyValue())); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA); return keyFactory.generatePublic(publicKeySpec); } public static String getPublicKeyValue() throws Exception { String publicKey = Redis.use().get("fp_publicKey"); if (StrKit.isBlank(publicKey)) { generatorKeyPair(); } return Redis.use().get("fp_publicKey"); } public static String getPrivateKeyValue() throws Exception { String publicKey = Redis.use().get("fp_privateKey"); if (StrKit.isBlank(publicKey)) { generatorKeyPair(); } return Redis.use().get("fp_privateKey"); } /** * 获取私钥 * @return * @throws Exception */ public static PrivateKey getPrivateKey() throws Exception { PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(decodeBase64(getPrivateKeyValue())); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA); return keyFactory.generatePrivate(privateKeySpec); } /** * 公钥加密 * @param data * @return * @throws Exception */ public static String encryptionByPublicKey(String source) throws Exception{ PublicKey publicKey = getPublicKey(); Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, publicKey); cipher.update(source.getBytes("UTF-8")); String target = encodeBase64(cipher.doFinal()); System.out.println("公钥加密后的数据:\r\n" + target); return target; } /** * 私钥解密 * @param target * @throws Exception */ public static String decryptionByPrivateKey(String target) throws Exception { PrivateKey privateKey = getPrivateKey(); Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, privateKey); cipher.update(decodeBase64(target)); String source = new String(cipher.doFinal(), "UTF-8"); return source; } /** * base64编码 * @param source * @return * @throws Exception */ public static String encodeBase64(byte[] source) throws Exception{ return new String(Base64.encodeBase64(source), "UTF-8"); } /** * Base64解码 * @param target * @return * @throws Exception */ public static byte[] decodeBase64(String target) throws Exception{ return Base64.decodeBase64(target.getBytes("UTF-8")); } }