¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, DreamLu All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: DreamLu 墿¥æ¢¦ (596392912@qq.com) |
| | | */ |
| | | package org.springblade.core.tool.utils; |
| | | |
| | | import org.springframework.lang.Nullable; |
| | | import org.springframework.util.Base64Utils; |
| | | |
| | | import org.springblade.core.tool.tuple.KeyPair; |
| | | import javax.crypto.Cipher; |
| | | import java.math.BigInteger; |
| | | import java.security.*; |
| | | import java.security.spec.*; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * RSAå ãè§£å¯å·¥å
· |
| | | * |
| | | * <p> |
| | | * 1. å
¬é¥è´è´£å å¯ï¼ç§é¥è´è´£è§£å¯ï¼ |
| | | * 2. ç§é¥è´è´£ç¾åï¼å
¬é¥è´è´£éªè¯ã |
| | | * </p> |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class RsaUtil { |
| | | /** |
| | | * æ°åç¾åï¼å¯é¥ç®æ³ |
| | | */ |
| | | public static final String RSA_ALGORITHM = "RSA"; |
| | | public static final String RSA_PADDING = "RSA/ECB/PKCS1Padding"; |
| | | |
| | | /** |
| | | * è·å KeyPair |
| | | * |
| | | * @return KeyPair |
| | | */ |
| | | public static KeyPair genKeyPair() { |
| | | return genKeyPair(1024); |
| | | } |
| | | |
| | | /** |
| | | * è·å KeyPair |
| | | * |
| | | * @param keySize key size |
| | | * @return KeyPair |
| | | */ |
| | | public static KeyPair genKeyPair(int keySize) { |
| | | try { |
| | | KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(RSA_ALGORITHM); |
| | | // å¯é¥ä½æ° |
| | | keyPairGen.initialize(keySize); |
| | | // å¯é¥å¯¹ |
| | | return new KeyPair(keyPairGen.generateKeyPair()); |
| | | } catch (NoSuchAlgorithmException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * çæRSAç§é¥ |
| | | * |
| | | * @param modulus Nç¹å¾å¼ |
| | | * @param exponent dç¹å¾å¼ |
| | | * @return {@link PrivateKey} |
| | | */ |
| | | public static PrivateKey generatePrivateKey(String modulus, String exponent) { |
| | | return generatePrivateKey(new BigInteger(modulus), new BigInteger(exponent)); |
| | | } |
| | | |
| | | /** |
| | | * çæRSAç§é¥ |
| | | * |
| | | * @param modulus Nç¹å¾å¼ |
| | | * @param exponent dç¹å¾å¼ |
| | | * @return {@link PrivateKey} |
| | | */ |
| | | public static PrivateKey generatePrivateKey(BigInteger modulus, BigInteger exponent) { |
| | | RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(modulus, exponent); |
| | | try { |
| | | KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM); |
| | | return keyFactory.generatePrivate(keySpec); |
| | | } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * çæRSAå
¬é¥ |
| | | * |
| | | * @param modulus Nç¹å¾å¼ |
| | | * @param exponent eç¹å¾å¼ |
| | | * @return {@link PublicKey} |
| | | */ |
| | | public static PublicKey generatePublicKey(String modulus, String exponent) { |
| | | return generatePublicKey(new BigInteger(modulus), new BigInteger(exponent)); |
| | | } |
| | | |
| | | /** |
| | | * çæRSAå
¬é¥ |
| | | * |
| | | * @param modulus Nç¹å¾å¼ |
| | | * @param exponent eç¹å¾å¼ |
| | | * @return {@link PublicKey} |
| | | */ |
| | | public static PublicKey generatePublicKey(BigInteger modulus, BigInteger exponent) { |
| | | RSAPublicKeySpec keySpec = new RSAPublicKeySpec(modulus, exponent); |
| | | try { |
| | | KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM); |
| | | return keyFactory.generatePublic(keySpec); |
| | | } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å¾å°å
¬é¥ |
| | | * |
| | | * @param base64PubKey å¯é¥å符串ï¼ç»è¿base64ç¼ç ï¼ |
| | | * @return PublicKey |
| | | */ |
| | | public static PublicKey getPublicKey(String base64PubKey) { |
| | | Objects.requireNonNull(base64PubKey, "base64 public key is null."); |
| | | byte[] keyBytes = Base64Utils.decodeFromString(base64PubKey); |
| | | X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); |
| | | try { |
| | | KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM); |
| | | return keyFactory.generatePublic(keySpec); |
| | | } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å¾å°å
¬é¥å符串 |
| | | * |
| | | * @param base64PubKey å¯é¥å符串ï¼ç»è¿base64ç¼ç ï¼ |
| | | * @return PublicKey String |
| | | */ |
| | | public static String getPublicKeyToBase64(String base64PubKey) { |
| | | PublicKey publicKey = getPublicKey(base64PubKey); |
| | | return getKeyString(publicKey); |
| | | } |
| | | |
| | | /** |
| | | * å¾å°ç§é¥ |
| | | * |
| | | * @param base64PriKey å¯é¥å符串ï¼ç»è¿base64ç¼ç ï¼ |
| | | * @return PrivateKey |
| | | */ |
| | | public static PrivateKey getPrivateKey(String base64PriKey) { |
| | | Objects.requireNonNull(base64PriKey, "base64 private key is null."); |
| | | byte[] keyBytes = Base64Utils.decodeFromString(base64PriKey); |
| | | PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); |
| | | try { |
| | | KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM); |
| | | return keyFactory.generatePrivate(keySpec); |
| | | } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å¾å°å¯é¥å符串ï¼ç»è¿base64ç¼ç ï¼ |
| | | * |
| | | * @param key key |
| | | * @return base 64 ç¼ç åç key |
| | | */ |
| | | public static String getKeyString(Key key) { |
| | | return Base64Utils.encodeToString(key.getEncoded()); |
| | | } |
| | | |
| | | /** |
| | | * å¾å°ç§é¥ base64 |
| | | * |
| | | * @param base64PriKey å¯é¥å符串ï¼ç»è¿base64ç¼ç ï¼ |
| | | * @return PrivateKey String |
| | | */ |
| | | public static String getPrivateKeyToBase64(String base64PriKey) { |
| | | PrivateKey privateKey = getPrivateKey(base64PriKey); |
| | | return getKeyString(privateKey); |
| | | } |
| | | |
| | | /** |
| | | * å
±è¦å å¯ |
| | | * |
| | | * @param base64PublicKey base64 çå
¬é¥ |
| | | * @param data å¾
å å¯çå
容 |
| | | * @return å å¯åçå
容 |
| | | */ |
| | | public static byte[] encrypt(String base64PublicKey, byte[] data) { |
| | | return encrypt(getPublicKey(base64PublicKey), data); |
| | | } |
| | | |
| | | /** |
| | | * å
±è¦å å¯ |
| | | * |
| | | * @param publicKey å
¬é¥ |
| | | * @param data å¾
å å¯çå
容 |
| | | * @return å å¯åçå
容 |
| | | */ |
| | | public static byte[] encrypt(PublicKey publicKey, byte[] data) { |
| | | return rsa(publicKey, data, Cipher.ENCRYPT_MODE); |
| | | } |
| | | |
| | | /** |
| | | * ç§é¥å å¯ï¼ç¨äº qpp å
ï¼å
¬é¥è§£å¯ |
| | | * |
| | | * @param base64PrivateKey base64 çç§é¥ |
| | | * @param data å¾
å å¯çå
容 |
| | | * @return å å¯åçå
容 |
| | | */ |
| | | public static byte[] encryptByPrivateKey(String base64PrivateKey, byte[] data) { |
| | | return encryptByPrivateKey(getPrivateKey(base64PrivateKey), data); |
| | | } |
| | | |
| | | /** |
| | | * ç§é¥å å¯ï¼å 坿 base64 å符串ï¼ç¨äº qpp å
ï¼å
¬é¥è§£å¯ |
| | | * |
| | | * @param base64PrivateKey base64 çç§é¥ |
| | | * @param data å¾
å å¯çå
容 |
| | | * @return å å¯åçå
容 |
| | | */ |
| | | public static String encryptByPrivateKeyToBase64(String base64PrivateKey, byte[] data) { |
| | | return Base64Util.encodeToString(encryptByPrivateKey(base64PrivateKey, data)); |
| | | } |
| | | |
| | | /** |
| | | * ç§é¥å å¯ï¼ç¨äº qpp å
ï¼å
¬é¥è§£å¯ |
| | | * |
| | | * @param privateKey ç§é¥ |
| | | * @param data å¾
å å¯çå
容 |
| | | * @return å å¯åçå
容 |
| | | */ |
| | | public static byte[] encryptByPrivateKey(PrivateKey privateKey, byte[] data) { |
| | | return rsa(privateKey, data, Cipher.ENCRYPT_MODE); |
| | | } |
| | | |
| | | /** |
| | | * å
¬é¥å å¯ |
| | | * |
| | | * @param base64PublicKey base64 å
¬é¥ |
| | | * @param data å¾
å å¯çå
容 |
| | | * @return å å¯åçå
容 |
| | | */ |
| | | @Nullable |
| | | public static String encryptToBase64(String base64PublicKey, @Nullable String data) { |
| | | if (StringUtil.isBlank(data)) { |
| | | return null; |
| | | } |
| | | return Base64Utils.encodeToString(encrypt(base64PublicKey, data.getBytes(Charsets.UTF_8))); |
| | | } |
| | | |
| | | /** |
| | | * è§£å¯ |
| | | * |
| | | * @param base64PrivateKey base64 ç§é¥ |
| | | * @param data æ°æ® |
| | | * @return è§£å¯åçæ°æ® |
| | | */ |
| | | public static byte[] decrypt(String base64PrivateKey, byte[] data) { |
| | | return decrypt(getPrivateKey(base64PrivateKey), data); |
| | | } |
| | | |
| | | /** |
| | | * è§£å¯ |
| | | * |
| | | * @param base64publicKey base64 å
¬é¥ |
| | | * @param data æ°æ® |
| | | * @return è§£å¯åçæ°æ® |
| | | */ |
| | | public static byte[] decryptByPublicKey(String base64publicKey, byte[] data) { |
| | | return decryptByPublicKey(getPublicKey(base64publicKey), data); |
| | | } |
| | | |
| | | /** |
| | | * è§£å¯ |
| | | * |
| | | * @param privateKey privateKey |
| | | * @param data æ°æ® |
| | | * @return è§£å¯åçæ°æ® |
| | | */ |
| | | public static byte[] decrypt(PrivateKey privateKey, byte[] data) { |
| | | return rsa(privateKey, data, Cipher.DECRYPT_MODE); |
| | | } |
| | | |
| | | /** |
| | | * è§£å¯ |
| | | * |
| | | * @param publicKey PublicKey |
| | | * @param data æ°æ® |
| | | * @return è§£å¯åçæ°æ® |
| | | */ |
| | | public static byte[] decryptByPublicKey(PublicKey publicKey, byte[] data) { |
| | | return rsa(publicKey, data, Cipher.DECRYPT_MODE); |
| | | } |
| | | |
| | | /** |
| | | * rsa å ãè§£å¯ |
| | | * |
| | | * @param key key |
| | | * @param data æ°æ® |
| | | * @param mode æ¨¡å¼ |
| | | * @return è§£å¯åçæ°æ® |
| | | */ |
| | | private static byte[] rsa(Key key, byte[] data, int mode) { |
| | | try { |
| | | Cipher cipher = Cipher.getInstance(RSA_PADDING); |
| | | cipher.init(mode, key); |
| | | return cipher.doFinal(data); |
| | | } catch (Exception e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * base64 æ°æ®è§£å¯ |
| | | * |
| | | * @param base64PublicKey base64 å
¬é¥ |
| | | * @param base64Data base64æ°æ® |
| | | * @return è§£å¯åçæ°æ® |
| | | */ |
| | | public static byte[] decryptByPublicKeyFromBase64(String base64PublicKey, byte[] base64Data) { |
| | | return decryptByPublicKey(getPublicKey(base64PublicKey), base64Data); |
| | | } |
| | | |
| | | /** |
| | | * base64 æ°æ®è§£å¯ |
| | | * |
| | | * @param base64PrivateKey base64 ç§é¥ |
| | | * @param base64Data base64æ°æ® |
| | | * @return è§£å¯åçæ°æ® |
| | | */ |
| | | @Nullable |
| | | public static String decryptFromBase64(String base64PrivateKey, @Nullable String base64Data) { |
| | | if (StringUtil.isBlank(base64Data)) { |
| | | return null; |
| | | } |
| | | return new String(decrypt(base64PrivateKey, Base64Utils.decodeFromString(base64Data)), Charsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * base64 æ°æ®è§£å¯ |
| | | * |
| | | * @param base64PrivateKey base64 ç§é¥ |
| | | * @param base64Data base64æ°æ® |
| | | * @return è§£å¯åçæ°æ® |
| | | */ |
| | | public static byte[] decryptFromBase64(String base64PrivateKey, byte[] base64Data) { |
| | | return decrypt(base64PrivateKey, Base64Utils.decode(base64Data)); |
| | | } |
| | | |
| | | /** |
| | | * base64 æ°æ®è§£å¯ |
| | | * |
| | | * @param base64PublicKey base64 å
¬é¥ |
| | | * @param base64Data base64æ°æ® |
| | | * @return è§£å¯åçæ°æ® |
| | | */ |
| | | @Nullable |
| | | public static String decryptByPublicKeyFromBase64(String base64PublicKey, @Nullable String base64Data) { |
| | | if (StringUtil.isBlank(base64Data)) { |
| | | return null; |
| | | } |
| | | return new String(decryptByPublicKeyFromBase64(base64PublicKey, Base64Utils.decodeFromString(base64Data)), Charsets.UTF_8); |
| | | } |
| | | |
| | | } |