Java 实现常见的非对称加密算法
布禾 人气:0概述
非对称加密算法与对称加密算法的主要差别在于非对称加密算法用于加密和解密的密钥不相同,非对称加密算法密钥分为公钥和私钥,公钥加密只能用私钥解密,反之私钥加密只能用公钥解密。相比对称加密算法,非对称加密算法加/解密效率低,但安全性高,这两种算法一般结合使用。常见非对称加密算法有RSA、ECC、Elgamal等。
使用RSA实现加密解密
公钥加密,私钥解密。
package com.ss.utils; import javax.crypto.Cipher; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.Base64; import java.util.HashMap; import java.util.Map; public class RsaUtil { public static final String KEY_TYPE_PRIVATE_KEY = "privateKey"; public static final String KEY_TYPE_PUBLIC_KEY = "publicKey"; /** * 生成公钥和私钥 * * @return * @throws NoSuchAlgorithmException */ public static Map<String, String> generateKey() throws NoSuchAlgorithmException { Map<String, String> resultMap = new HashMap<>(); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); KeyPair keyPair = keyPairGenerator.generateKeyPair(); Base64.Encoder encoder = Base64.getEncoder(); resultMap.put(KEY_TYPE_PRIVATE_KEY, encoder.encodeToString(keyPair.getPrivate().getEncoded())); resultMap.put(KEY_TYPE_PUBLIC_KEY, encoder.encodeToString(keyPair.getPublic().getEncoded())); return resultMap; } /** * RSA加密 * @param key * @param content * @param keyType * @return * @throws Exception */ public static String rsaEncrypt(String key, String content, String keyType) throws Exception { return rsa(key, content.getBytes(), keyType, Cipher.ENCRYPT_MODE); } /** * RSA解密 * @param key * @param content * @param keyType * @return * @throws Exception */ public static String rsaDecrypt(String key, String content, String keyType) throws Exception { return rsa(key, Base64.getDecoder().decode(content), keyType, Cipher.DECRYPT_MODE); } private static String rsa(String key, byte[] content, String keyType, int mode) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); if (KEY_TYPE_PRIVATE_KEY.equals(keyType)) { cipher.init(mode, keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(key)))); } else { cipher.init(mode, keyFactory.generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(key)))); } byte[] bytes = cipher.doFinal(content); return mode == Cipher.DECRYPT_MODE ? new String(bytes) : Base64.getEncoder().encodeToString(bytes); } public static void main(String[] args) throws Exception { String content = "大王叫我来巡山呐"; //生成密钥对 Map<String, String> keyMap = generateKey(); System.out.println("私钥:" + keyMap.get(KEY_TYPE_PRIVATE_KEY)); System.out.println("公钥:" + keyMap.get(KEY_TYPE_PUBLIC_KEY)); //私钥加密,公钥解密 String privateKeyData = rsaEncrypt(keyMap.get(KEY_TYPE_PRIVATE_KEY), content, KEY_TYPE_PRIVATE_KEY); System.out.println("私钥加密:" + privateKeyData); System.out.println("公钥解密:" + rsaDecrypt(keyMap.get(KEY_TYPE_PUBLIC_KEY), privateKeyData, KEY_TYPE_PUBLIC_KEY)); //公钥加密,私钥解密 String publicKeyData = rsaEncrypt(keyMap.get(KEY_TYPE_PUBLIC_KEY), content, KEY_TYPE_PUBLIC_KEY); System.out.println("公钥加密:" + publicKeyData); System.out.println("私钥解密:" + rsaDecrypt(keyMap.get(KEY_TYPE_PRIVATE_KEY), publicKeyData, KEY_TYPE_PRIVATE_KEY)); } }
输出
私钥:MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDQPZnj0+/uh5jG0/rqVsXKLve7wTw2LVEwdBhm+SPrLoHYb4+6idNeF4bWgTqNRs2hfewq5cyiwXujT+eqp0g1ebSLWSUWzBsktynQMggxb8IwtnFYzmZX7BSAUudrfTre74RtKS0krYY5PCXCGR/EGbbXMVv1m2S59BrkWEI8c/iORhRAJ5/pqRW/51cIimPx9iTYx/QZC4VsCRi8ZrBaXcJhEi4Y/YzOFhfITID4ATis0Z3yw3FVybKzJo3Nexupcec5qaLBlwuDkY6E4NgQq51bjBCUB78RgX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BT8FIp8jghCz2j+lAIiTfGZvsK9qUdZEmwTAEjV6uP4avF6njriglGJ4KhcYXEO66tOJWe2nQ1hyYXEHS43h9F0dtlWDjF6Xr6wdmUALnhprHDwKPdT/1T8p7+M5Fz7fUC7TJulBHWCSZvhgl405PvN+iTv7VysBJKRPks1JnmMe6BxFyhxXZfNRHmUyQvTMSfWt/A5gOy8ao/SOwWv0QMSh5NbocSd/tpjn27kw== 私钥解密:大王叫我来巡山呐
使用RSA实现数字签名
私钥签名,公钥验证。
package com.ss.utils; import java.security.*; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.Base64; import java.util.HashMap; import java.util.Map; public class SignatureUtil { public static final String KEY_TYPE_PRIVATE_KEY = "privateKey"; public static final String KEY_TYPE_PUBLIC_KEY = "publicKey"; /** * 生成公钥和私钥 * * @return * @throws NoSuchAlgorithmException */ public static Map<String, String> generateKey() throws NoSuchAlgorithmException { Map<String, String> resultMap = new HashMap<>(); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); KeyPair keyPair = keyPairGenerator.generateKeyPair(); Base64.Encoder encoder = Base64.getEncoder(); resultMap.put(KEY_TYPE_PRIVATE_KEY, encoder.encodeToString(keyPair.getPrivate().getEncoded())); resultMap.put(KEY_TYPE_PUBLIC_KEY, encoder.encodeToString(keyPair.getPublic().getEncoded())); return resultMap; } /** * 私钥签名 * @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 = "大王叫我来巡山呐"; //生成密钥对 Map<String, String> keyMap = generateKey(); //私钥签名 String sign = generateSignature(keyMap.get(KEY_TYPE_PRIVATE_KEY), content); System.out.println("私钥签名:" + sign); //公钥验证 boolean verifyResult = verifySignature(keyMap.get(KEY_TYPE_PUBLIC_KEY), content, sign); System.out.println("公钥验证:" + verifyResult); //将内容做下修改,再进行公钥验证 boolean verifyResult2 = verifySignature(keyMap.get(KEY_TYPE_PUBLIC_KEY), content + "啦啦啦啦", sign); System.out.println("公钥验证:" + verifyResult2); } }
输出
私钥签名:XDWeOUVZhXNPl58mcmRK8ht9TAhnREc+NlnSs2b6PCJuj29ABuoXsjZeJqSspGw8hm9ckHVSTrUt8pX4BFBFzrINDRBsx3TdLp2L8nMkhjFUfo0qcyZo8ReEGFCNr0ergkq2W1Me/5W40GrQz4cJcQ4KV/QWzD8YHWcnDvKg9Q0nXJSngyhJTzUB8o/pgo/tmkA9+bWW212UD9nzEXu5aoPEcYyoo3iFWRX5o9Jgg6ZF+exmWizwZHkg4eD1zC5IN3m4yRS6GRbnZInEuDsOMp9W7HouDLUirkFZiSkyW+DVto+L6CE5eNBrWC52zxiNkVBlkyYKOatSpcrQrtnyiQ== 公钥验证:true 公钥验证:false
加载全部内容