From a088987e7ab7005db1bb1da61dfc0cf420e02d78 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期三, 13 十一月 2024 17:11:28 +0800 Subject: [PATCH] 1 --- src/main/java/com/moon/server/helper/RsaHelper.java | 80 +-------------------------------------- 1 files changed, 3 insertions(+), 77 deletions(-) diff --git a/src/main/java/com/moon/server/helper/RsaHelper.java b/src/main/java/com/moon/server/helper/RsaHelper.java index 172eb36..7040301 100644 --- a/src/main/java/com/moon/server/helper/RsaHelper.java +++ b/src/main/java/com/moon/server/helper/RsaHelper.java @@ -16,51 +16,24 @@ import java.util.HashMap; import java.util.Map; -/** - * RSA宸ュ叿绫� - * @author WWW - */ +@SuppressWarnings("ALL") public class RsaHelper { - /** - * 绉侀挜 - */ private static String privateKey; - /** - * 鍏挜 - */ private static String publicKey; - /** - * 瀵嗛挜绠楁硶 - */ private static final String KEY_ALGORITHM = "RSA"; - /** - * RSA瀵嗛挜闀垮害锛�1024 鎴� 2048 - */ private static final int DEFAULT_RSA_KEY_SIZE = 1024; - /** - * 鏃ュ織 - */ private final static Log log = LogFactory.getLog(RsaHelper.class); - /** - * 鐢熸垚鍏閽� - */ public static void generate() { Map<String, String> result = generateRsaKey(DEFAULT_RSA_KEY_SIZE); System.out.println("鍏挜涓猴細" + result.get("publicKey")); System.out.println("绉侀挜涓猴細" + result.get("privateKey")); } - /** - * 鑾峰彇RSA鍔犲瘑绉侀挜 - * - * @return - * @throws IOException - */ public static String getPrivateKey() throws IOException { if (privateKey == null) { InputStream inPrivate = new ClassPathResource("config" + File.separator + "rsa_private_key.txt").getInputStream(); @@ -71,12 +44,6 @@ return privateKey; } - /** - * 鑾峰彇RSA鍔犲瘑鍏挜 - * - * @return - * @throws IOException - */ public static String getPublicKey() throws IOException { if (publicKey == null) { InputStream inPrivate = new ClassPathResource("config" + File.separator + "rsa_public_key.txt").getInputStream(); @@ -87,13 +54,6 @@ return publicKey; } - /** - * 璇诲彇鏂囨湰鏂囦欢 - * - * @param fileName 鏂囦欢璺緞 - * @return - * @throws IOException - */ public static String readFile(String fileName) throws IOException { File file = new File(fileName); BufferedReader br = new BufferedReader(new FileReader(file)); @@ -109,13 +69,6 @@ return result.toString(); } - /** - * 鎶奿nputStream杞垚String - * - * @param is - * @return - * @throws IOException - */ private static String inputStream2String(InputStream is) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -130,28 +83,18 @@ return str; } - /** - * 鐢熸垚RSA鐨勫叕绉侀挜 - * - * @param keySize 1025 鎴� 2048 - * @return - */ public static Map<String, String> generateRsaKey(int keySize) { Map<String, String> result = new HashMap<>(2); try { KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); - // 鍒濆鍖栧瘑閽ュ鐢熸垚鍣紝瀵嗛挜澶у皬涓�1024 2048浣� keyPairGen.initialize(keySize, new SecureRandom()); - // 鐢熸垚涓�涓瘑閽ュ锛屼繚瀛樺湪keyPair涓� KeyPair keyPair = keyPairGen.generateKeyPair(); - // 寰楀埌鍏挜瀛楃涓� String pub = new String(Base64.encodeBase64(keyPair.getPublic().getEncoded())); result.put("publicKey", pub); - // 寰楀埌绉侀挜瀛楃涓� String pri = new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded())); result.put("privateKey", pri); } catch (Exception ex) { @@ -161,22 +104,13 @@ return result; } - /** - * RSA绉侀挜瑙e瘑 - * - * @param str 鍔犲瘑鐨勫瓧绗︿覆 - * @return 瑙e瘑瀛楃涓� - * @throws Exception 鍔犲瘑杩囩▼涓殑寮傚父淇℃伅 - */ public static String decrypt(String str) throws Exception { - // 64浣嶈В鐮佸姞瀵嗗悗鐨勫瓧绗︿覆 byte[] inputByte = Base64.decodeBase64(str.getBytes(StandardCharsets.UTF_8)); - // Base64缂栫爜鐨勭閽� byte[] decoded = Base64.decodeBase64(getPrivateKey()); RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded)); - // RSA瑙e瘑锛歊SA/ECB/NoPadding + // RSA/ECB/NoPadding Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, priKey); @@ -185,20 +119,12 @@ return outStr; } - /** - * RSA鍏挜鍔犲瘑 - * - * @param str 闇�瑕佸姞瀵嗙殑瀛楃涓� - * @return 瀵嗘枃 - * @throws Exception 鍔犲瘑杩囩▼涓殑寮傚父淇℃伅 - */ public static String encrypt(String str) throws Exception { - // Base64缂栫爜鐨勫叕閽� byte[] decoded = Base64.decodeBase64(getPublicKey()); RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded)); - // RSA鍔犲瘑锛歊SA/ECB/NoPadding + // RSA/ECB/NoPadding Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, pubKey); -- Gitblit v1.9.3