|
@@ -0,0 +1,65 @@
|
|
|
+package com.qmth.teachcloud.common.util;
|
|
|
+
|
|
|
+import javax.crypto.Cipher;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.security.*;
|
|
|
+import java.security.interfaces.RSAPrivateKey;
|
|
|
+import java.security.interfaces.RSAPublicKey;
|
|
|
+import java.security.spec.PKCS8EncodedKeySpec;
|
|
|
+import java.util.Base64;
|
|
|
+
|
|
|
+public class RSAUtil {
|
|
|
+
|
|
|
+ // 私钥
|
|
|
+ private static final String PRIVATE_KEY = "MIICeQIBADANBgkqhkiG9w0BAQEFAASCAmMwggJfAgEAAoGBANeXZtC7kT0likxp5YcJsard5/LpLnLU8zBmnqJq27kpiEUltn7WhI9UJvPoRM0XHexrKTibWWegqYANo6voPKjytZWWbD7KqMHl/NXuwmuVrfzk6mwIN3OccEQri/a5tBFKrzo3Y8UZfFI96RA2WaXB9p7xkuLo0ZutucxF/xt5AgMBAAECgYEA1guYZcbD1UyvOVyuKNskDTvSLSb8GcB5TmWkPCcRzY2BoP1nahi7db4cCm9h16mK6Q4f1/bM1gj6IIvqIyQpC9T9aiWvESBOIc567ExeNimdKwgAlheNbTVgyscIvj96Kj8QbatUxrUULnJMXWJDLQdHyIKusoCTxL8Dx+cTgf0CQQD3xFtVlQyfwISpaoc5TWvHCamTMjsjfkvpu2GJ2NveX5Q0GyzcpgIAI6pkpRafWoWhSLv81IeomVFQXJX9UgTfAkEA3sFXYx3GqZUNues30Tpd3B+xsHAZuiSg1NJihStQzfGNG7LB37LmKLjvtnFeEXLm8XWQ/BqiHsfB4If8u2HSpwJBAOua3P9U7Nw8xeoUUuI6rPJoTcibS4FE6AsrFGVwwiOD3/psnQx1EBHx2GY8VnbSLaBg+eJCd33rzVcNs9Nf2yECQQCf4cfWRWX1DB35BuJeU3VQ7JK+IAfnOikR+Btsl+V8m9Z7Pfe3lxFpwyuTGJW7Emy+fHLe8ELSsu/uN8zntdPbAkEA8vPOk/X8TojgKtY2ZHHV+KyupRSvR2Mu6cQJstcU355HRgRCm+5XGXHYiR+EN8Cqu4FS1sw0HtO/k3wSiXr2LQ==";
|
|
|
+
|
|
|
+ public static void createKey() throws NoSuchAlgorithmException {
|
|
|
+ // KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
|
|
|
+ KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
|
|
|
+ // 初始化密钥对生成器
|
|
|
+ keyPairGen.initialize(1024, new SecureRandom());
|
|
|
+ // 生成一个密钥对,保存在keyPair中
|
|
|
+ KeyPair keyPair = keyPairGen.generateKeyPair();
|
|
|
+ // 得到私钥
|
|
|
+ RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
|
|
|
+ // 得到公钥
|
|
|
+ RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
|
|
|
+ String publicKeyString = Base64.getEncoder().encodeToString(publicKey.getEncoded());
|
|
|
+ // 得到私钥字符串
|
|
|
+ String privateKeyString = Base64.getEncoder().encodeToString(privateKey.getEncoded());
|
|
|
+ // 将公钥和私钥保存到Map
|
|
|
+ //0表示公钥
|
|
|
+ System.out.println("public = " + publicKeyString);
|
|
|
+ System.out.println("private = " + privateKeyString);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String decrypt(String str) {
|
|
|
+ //64位解码加密后的字符串
|
|
|
+ byte[] inputByte = Base64.getDecoder().decode(str.getBytes(StandardCharsets.UTF_8));
|
|
|
+ //base64编码的私钥
|
|
|
+ byte[] decoded = Base64.getDecoder().decode(PRIVATE_KEY);
|
|
|
+ RSAPrivateKey priKey = null;
|
|
|
+ //RSA解密
|
|
|
+ Cipher cipher = null;
|
|
|
+ String outStr = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
|
|
|
+ cipher = Cipher.getInstance("RSA");
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, priKey);
|
|
|
+ outStr = new String(cipher.doFinal(inputByte));
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return outStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ try {
|
|
|
+ createKey();
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|