1 packagecom.sdocean.security;2
3 importorg.apache.commons.codec.binary.Base64;4
5 importjavax.crypto.Cipher;6 import java.security.*;7 importjava.security.interfaces.RSAPrivateKey;8 importjava.security.interfaces.RSAPublicKey;9 importjava.security.spec.PKCS8EncodedKeySpec;10 importjava.security.spec.X509EncodedKeySpec;11
12 /**
13 * @ClassName RSAUtil14 * @Description [一句话描述做什么]15 * @Author wangdong16 * @Date 2019/10/14 14:3817 * @Version V1.018 **/
19 public classRSAUtil {20
21 public static String publicKey; //公钥
22 public static String privateKey; //私钥
23
24 /**
25 * 生成公钥和私钥26 */
27 public static voidgenerateKey() {28 //1.初始化秘钥
29 KeyPairGenerator keyPairGenerator;30 try{31 keyPairGenerator = KeyPairGenerator.getInstance("RSA");32 SecureRandom sr = new SecureRandom(); //随机数生成器
33 keyPairGenerator.initialize(1024, sr); //设置4096位长的秘钥
34 KeyPair keyPair = keyPairGenerator.generateKeyPair(); //开始创建
35 RSAPublicKey rsaPublicKey =(RSAPublicKey) keyPair.getPublic();36 RSAPrivateKey rsaPrivateKey =(RSAPrivateKey) keyPair.getPrivate();37 //进行转码
38 publicKey =Base64.encodeBase64String(rsaPublicKey.getEncoded());39 //进行转码
40 privateKey =Base64.encodeBase64String(rsaPrivateKey.getEncoded());41 } catch(NoSuchAlgorithmException e) {42 e.printStackTrace();43 }44 }45
46 /**
47 * 私钥匙加密或解密48 *49 *@paramcontent50 *@paramprivateKeyStr51 *@return
52 */
53 public static String encryptByprivateKey(String content, String privateKeyStr, intopmode) {54 //私钥要用PKCS8进行处理
55 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = newPKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyStr));56 KeyFactory keyFactory;57 PrivateKey privateKey;58 Cipher cipher;59 byte[] result;60 String text = null;61 try{62 keyFactory = KeyFactory.getInstance("RSA");63 //还原Key对象
64 privateKey =keyFactory.generatePrivate(pkcs8EncodedKeySpec);65 cipher = Cipher.getInstance("RSA");66 cipher.init(opmode, privateKey);67 //加密解密
68 text =encryptTxt(opmode,cipher,content);69 } catch(Exception e) {70 e.printStackTrace();71 }72 returntext;73 }74
75 public static String encryptTxt(intopmode, Cipher cipher, String content){76 byte[] result;77 String text = null;78 try{79 if (opmode == Cipher.ENCRYPT_MODE) { //加密
80 result =cipher.doFinal(content.getBytes());81 text =Base64.encodeBase64String(result);82 } else if (opmode == Cipher.DECRYPT_MODE) { //解密
83 result =cipher.doFinal(Base64.decodeBase64(content));84 text = new String(result, "UTF-8");85 }86 }catch(Exception e){87 e.printStackTrace();88 }89 returntext;90 }91
92 /**
93 * 公钥匙加密或解密94 *95 *@paramcontent96 *@return
97 */
98 public static String encryptByPublicKey(String content, String publicKeyStr, intopmode) {99 //公钥要用X509进行处理
100 X509EncodedKeySpec x509EncodedKeySpec = newX509EncodedKeySpec(Base64.decodeBase64(publicKeyStr));101 KeyFactory keyFactory;102 PublicKey publicKey;103 Cipher cipher;104 byte[] result;105 String text = null;106 try{107 keyFactory = KeyFactory.getInstance("RSA");108 //还原Key对象
109 publicKey =keyFactory.generatePublic(x509EncodedKeySpec);110 cipher = Cipher.getInstance("RSA");111 cipher.init(opmode, publicKey);112 text =encryptTxt(opmode,cipher,content);113 } catch(Exception e) {114 e.printStackTrace();115 }116 returntext;117 }118
119 public static voidmain(String[] args) {120 //1. 生成(公钥和私钥)密钥对121 //RSAUtil.generateKey();122 //System.out.println("公钥:" + RSAUtil.publicKey);123 //System.out.println("私钥:" + RSAUtil.privateKey);
124 System.out.println("----------公钥加密私钥解密(推荐),非对称加密,公钥保存在客户端,私钥保存在服务端-------------");125 String public_key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCLgJDYvPYWrUfSDeTas8fKb8dBmKt6VNPBMz0KXVTlEMX+3XTv6lHarqspBp/WzX0gfDveKxJ3WnGuJt8jO0ivfOCodSxXc9tvZSC6fhAneAbUkFWiBOa1bMuit6zS7WiQs5draAW6vIRYWBCSZm7jV+HW9BpPYxiI1lpCD8Qd+wIDAQAB";126 //使用 公钥加密,私钥解密
127 String textsr = "{\n" +
128 " \"base\":{\n" +
129 " \"uid\":12,\n" +
130 " \"name\":\"李四2\"\n" +
131 " },\n" +
132 " \"sex\":\"男\"\n" +
133 "}";134 String simi="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIuAkNi89hatR9IN5Nqzx8pvx0GY\r\n" +
135 "q3pU08EzPQpdVOUQxf7ddO/qUdquqykGn9bNfSB8O94rEndaca4m3yM7SK984Kh1LFdz229lILp+\r\n" +
136 "ECd4BtSQVaIE5rVsy6K3rNLtaJCzl2toBbq8hFhYEJJmbuNX4db0Gk9jGIjWWkIPxB37AgMBAAEC\r\n" +
137 "gYA7QYtFZU6q50QAp8I0W/VbugtLg8wjFHE2OcuN4YM0IeHYlr0bQ88tMtClhpjSp0qlR+AuSOF5\r\n" +
138 "LUPok75our/umKUsanT46Sepa/Wgh7vhh3oV5fqYA0J9lfdfqInJMCggavntcmHAEDjXbMJ8oMJS\r\n" +
139 "O2v9Tsc4nliLXaW0c/ulkQJBANuEQdzEFAYQPaxri1KZhDICWdLCqnPdda7roK7cYKO1H5akaVLn\r\n" +
140 "aS/DGMq00zpQiV8d9jWXqiYC2DKsUzYEuCUCQQCir/JJdJw+SBcAUSnS0/dZjbDB8mdkW2YafSwP\r\n" +
141 "rftrjHtnDEZoCZrccIv+V48aLXE4xIiHStS2cZRn278zDxOfAkBcryf75ZIqyTqxDStjMcxeLR3t\r\n" +
142 "3iGIEE57D6PlkMkA9h2jRkHuLiT8dJyIIfc5nP2TepwVHrdJm1PbeWUc3rs9AkBoAkrJUYcOfnJO\r\n" +
143 "HpW5lxB9LYwDAuG9FH37k2kXASlLQf4nDH76xqE+K6okyn/PwZuwKl4K+PlzRrwVhjUABLSLAkEA\r\n" +
144 "i87GLH9FV4Af4TMVt7UykSpmtU4BweqY0bQsRZdWnpdZtbmmMmUTB0+Eb1q5okVSgTktCtXECLda\r\n" +
145 "uy2IlrDGuw==";146 String encryptByPublic =RSAUtil.encryptByPublicKey(textsr, public_key, Cipher.ENCRYPT_MODE);147 System.out.println("公钥加密:" +encryptByPublic);148 String jiemi=RSAUtil.encryptByprivateKey(encryptByPublic, simi, Cipher.DECRYPT_MODE);149 System.out.println("私钥解密:"+jiemi);150 }151
152
153 }