Java非对称加密解密
从微信公众号摘抄,稍加改动,收藏
另外https://www.jianshu.com/p/048be4864559 写得挺好
import lombok.Data; import javax.crypto.Cipher; import java.security.*; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.Base64; public class SignatureUtil { /** * 生成公钥和私钥 * * @return * @throws NoSuchAlgorithmException */ public static Keys generateKey() throws NoSuchAlgorithmException { Keys resultMap = new Keys(); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); KeyPair keyPair = keyPairGenerator.generateKeyPair(); Base64.Encoder encoder = Base64.getEncoder(); resultMap.setPrivateKey(encoder.encodeToString(keyPair.getPrivate().getEncoded())); resultMap.setPublicKey(encoder.encodeToString(keyPair.getPublic().getEncoded())); return resultMap; } /** * RSA加密-pub-en,pri-de * * @param key * @param content * @return * @throws Exception */ public static String rsaEncrypt(String key, String content) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, keyFactory.generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(key)))); byte[] bytes = cipher.doFinal(content.getBytes()); return Base64.getEncoder().encodeToString(bytes); } /** * RSA解密-pub-en,pri-de * * @param key * @param content * @return * @throws Exception */ public static String rsaDecrypt(String key, String content) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(key)))); byte[] bytes = cipher.doFinal(Base64.getDecoder().decode(content)); return new String(bytes); } /** * RSA加密-pri-en,pub-de * * @param key * @param content * @return * @throws Exception */ public static String rsaEncryptReverse(String key, String content) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(key)))); byte[] bytes = cipher.doFinal(content.getBytes()); return Base64.getEncoder().encodeToString(bytes); } /** * RSA解密-pri-en,pub-de * * @param key * @param content * @return * @throws Exception */ public static String rsaDecryptReverse(String key, String content) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, keyFactory.generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(key)))); byte[] bytes = cipher.doFinal(Base64.getDecoder().decode(content)); return new String(bytes); } /** * 私钥签名 * * @param privateKeyStr * @param content * @return * @throws Exception */ public static String generateSignature(String privateKeyStr, String content) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); Signature signature = Signature.getInstance("SHA1withRSA"); PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKeyStr))); signature.initSign(privateKey); signature.update(content.getBytes()); return Base64.getEncoder().encodeToString(signature.sign()); } /** * 公钥验证 * * @param publicKeyStr * @param content * @param sign * @return * @throws Exception */ public static boolean verifySignature(String publicKeyStr, String content, String sign) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); Signature signature = Signature.getInstance("SHA1withRSA"); PublicKey publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(publicKeyStr))); signature.initVerify(publicKey); signature.update(content.getBytes()); return signature.verify(Base64.getDecoder().decode(sign)); } public static void main(String[] args) throws Exception { String content = "大王叫我来巡山呐"; //生成密钥对 Keys keys = generateKey(); //公钥加密,私钥解密 String publicKeyData = rsaEncrypt(keys.publicKey, content); System.out.println("公钥加密:" + publicKeyData); System.out.println("私钥解密:" + rsaDecrypt(keys.privateKey, publicKeyData)); //私钥加密,公钥解密 String privateKeyData = rsaEncryptReverse(keys.privateKey, content); System.out.println("私钥加密:" + privateKeyData); System.out.println("公钥解密:" + rsaDecryptReverse(keys.publicKey, privateKeyData)); //私钥签名 String sign = generateSignature(keys.privateKey, content); System.out.println("私钥签名:" + sign); //公钥验证 boolean verifyResult = verifySignature(keys.publicKey, content, sign); System.out.println("公钥验证:" + verifyResult); //将内容做下修改,再进行公钥验证 boolean verifyResult2 = verifySignature(keys.publicKey, content + "啦啦啦啦", sign); System.out.println("公钥验证:" + verifyResult2); } @Data public static class Keys { public String publicKey; public String privateKey; } }