说明 RSA是非对称密钥算法
参考 https://www.jianshu.com/p/ff8281f034f4
原理
后端根据 commons-codec 的RSA非对称密钥算法生成公钥和私钥,公钥发送给前端
前端 js 使用公钥对要加密的数据进行加密,然后传输给后端
后端使用私钥对加密数据进行解密,得到明文数据,即原前端数据
使用 pom.xml 1 2 3 4 5 <dependency > <groupId > commons-codec</groupId > <artifactId > commons-codec</artifactId > <version > 1.10</version > </dependency >
RSAUtil.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 import org.apache.commons.codec.binary.Base64;import javax.crypto.Cipher;import java.security.*;import java.security.spec.PKCS8EncodedKeySpec;import java.security.spec.X509EncodedKeySpec;import java.util.HashMap;import java.util.Map;public class RSAUtil { public static final String KEY_ALGORITHM = "RSA" ; public static final String SIGNATURE_ALGORITHM = "MD5withRSA" ; private static final String PUBLIC_KEY = "RSAPublicKey" ; private static final String PRIVATE_KEY = "RSAPrivateKey" ; public static byte [] decryptBASE64(String key) { return Base64.decodeBase64(key); } public static String encryptBASE64 (byte [] bytes) { return Base64.encodeBase64String(bytes); } public static String sign (byte [] data, String privateKey) throws Exception { byte [] keyBytes = decryptBASE64(privateKey); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec (keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec); Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initSign(priKey); signature.update(data); return encryptBASE64(signature.sign()); } public static boolean verify (byte [] data, String publicKey, String sign) throws Exception { byte [] keyBytes = decryptBASE64(publicKey); X509EncodedKeySpec keySpec = new X509EncodedKeySpec (keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PublicKey pubKey = keyFactory.generatePublic(keySpec); Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initVerify(pubKey); signature.update(data); return signature.verify(decryptBASE64(sign)); } public static byte [] decryptByPrivateKey(byte [] data, String key) throws Exception{ byte [] keyBytes = decryptBASE64(key); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec (keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(data); } public static byte [] decryptByPrivateKey(String data, String key) throws Exception { return decryptByPrivateKey(decryptBASE64(data),key); } public static byte [] decryptByPublicKey(byte [] data, String key) throws Exception { byte [] keyBytes = decryptBASE64(key); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec (keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); Key publicKey = keyFactory.generatePublic(x509KeySpec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, publicKey); return cipher.doFinal(data); } public static byte [] encryptByPublicKey(String data, String key) throws Exception { byte [] keyBytes = decryptBASE64(key); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec (keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); Key publicKey = keyFactory.generatePublic(x509KeySpec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, publicKey); return cipher.doFinal(data.getBytes()); } public static byte [] encryptByPrivateKey(byte [] data, String key) throws Exception { byte [] keyBytes = decryptBASE64(key); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec (keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, privateKey); return cipher.doFinal(data); } public static String getPrivateKey (Map<String, Key> keyMap) throws Exception { Key key = (Key) keyMap.get(PRIVATE_KEY); return encryptBASE64(key.getEncoded()); } public static String getPublicKey (Map<String, Key> keyMap) throws Exception { Key key = keyMap.get(PUBLIC_KEY); return encryptBASE64(key.getEncoded()); } public static Map<String, Key> initKey () throws Exception { KeyPairGenerator keyPairGen = KeyPairGenerator .getInstance(KEY_ALGORITHM); keyPairGen.initialize(1024 ); KeyPair keyPair = keyPairGen.generateKeyPair(); Map<String, Key> keyMap = new HashMap (2 ); keyMap.put(PUBLIC_KEY, keyPair.getPublic()); keyMap.put(PRIVATE_KEY, keyPair.getPrivate()); return keyMap; } }
测试 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 import org.junit.Before;import org.junit.Test;import java.security.Key;import java.util.Map;import static org.junit.Assert.assertEquals;import static org.junit.Assert.assertTrue;public class RSATest { private String publicKey; private String privateKey; @Before public void setUp () throws Exception { Map<String, Key> keyMap = RSAUtil.initKey(); publicKey = RSAUtil.getPublicKey(keyMap); privateKey = RSAUtil.getPrivateKey(keyMap); System.err.println("公钥: \n\r" + publicKey); System.err.println("私钥: \n\r" + privateKey); } @Test public void test () throws Exception { System.err.println("公钥加密——私钥解密" ); String inputStr = "dounine" ; byte [] encodedData = RSAUtil.encryptByPublicKey(inputStr, publicKey); byte [] decodedData = RSAUtil.decryptByPrivateKey(encodedData, privateKey); String outputStr = new String (decodedData); System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr); assertEquals(inputStr, outputStr); } @Test public void testSign () throws Exception { System.err.println("私钥加密——公钥解密" ); String inputStr = "dounine" ; byte [] data = inputStr.getBytes(); byte [] encodedData = RSAUtil.encryptByPrivateKey(data, privateKey); byte [] decodedData = RSAUtil.decryptByPublicKey(encodedData, publicKey); String outputStr = new String (decodedData); System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr); assertEquals(inputStr, outputStr); System.err.println("私钥签名——公钥验证签名" ); String sign = RSAUtil.sign(encodedData, privateKey); System.err.println("签名:" + sign); boolean status = RSAUtil.verify(encodedData, publicKey, sign); System.err.println("状态:" + status); assertTrue(status); } }
前端代码 https://github.com/travist/jsencrypt
1 2 3 4 5 6 <script src ="bin/jsencrypt.min.js" > </script > <script type ="text/javascript" > var encrypt = new JSEncrypt (); encrypt.setPublicKey ('java生成的公钥' ); var encrypted = encrypt.encrypt ('加密的字符串' ); </script >