¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.se.common.core.utils; |
| | | |
| | | import org.apache.commons.codec.binary.Base64; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.core.io.ClassPathResource; |
| | | |
| | | import javax.crypto.Cipher; |
| | | import java.io.*; |
| | | 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.security.spec.X509EncodedKeySpec; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * RSAå·¥å
·ç±» |
| | | * |
| | | * @author WWW |
| | | * @date 2024-08-25 |
| | | */ |
| | | public class RsaUtils { |
| | | /** |
| | | * ç§é¥ |
| | | */ |
| | | 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 static final Logger log = LoggerFactory.getLogger(RsaUtils.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(); |
| | | privateKey = inputStream2String(inPrivate); |
| | | inPrivate.close(); |
| | | } |
| | | |
| | | 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(); |
| | | publicKey = inputStream2String(inPrivate); |
| | | inPrivate.close(); |
| | | } |
| | | |
| | | 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)); |
| | | |
| | | StringBuilder result = new StringBuilder(); |
| | | |
| | | String line = null; |
| | | while ((line = br.readLine()) != null) { |
| | | result.append(System.lineSeparator() + line); |
| | | } |
| | | br.close(); |
| | | |
| | | return result.toString(); |
| | | } |
| | | |
| | | /** |
| | | * æinputStream转æString |
| | | * |
| | | * @param is |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | private static String inputStream2String(InputStream is) throws IOException { |
| | | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| | | |
| | | int i = -1; |
| | | while ((i = is.read()) != -1) { |
| | | baos.write(i); |
| | | } |
| | | |
| | | String str = baos.toString(); |
| | | baos.close(); |
| | | |
| | | 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) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * RSAç§é¥è§£å¯ |
| | | * |
| | | * @param str å å¯çå符串 |
| | | * @return è§£å¯å符串 |
| | | * @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è§£å¯ï¼RSA/ECB/NoPadding |
| | | Cipher cipher = Cipher.getInstance("RSA"); |
| | | cipher.init(Cipher.DECRYPT_MODE, priKey); |
| | | |
| | | String outStr = new String(cipher.doFinal(inputByte)); |
| | | |
| | | 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å å¯ï¼RSA/ECB/NoPadding |
| | | Cipher cipher = Cipher.getInstance("RSA"); |
| | | cipher.init(Cipher.ENCRYPT_MODE, pubKey); |
| | | |
| | | String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8))); |
| | | |
| | | return outStr; |
| | | } |
| | | } |
| | | |