¶Ô±ÈÐÂÎļþ |
| | |
| | | package org.dromara.common.encrypt.utils; |
| | | |
| | | import cn.hutool.core.codec.Base64; |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.crypto.SecureUtil; |
| | | import cn.hutool.crypto.SmUtil; |
| | | import cn.hutool.crypto.asymmetric.KeyType; |
| | | import cn.hutool.crypto.asymmetric.RSA; |
| | | import cn.hutool.crypto.asymmetric.SM2; |
| | | |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * å®å
¨ç¸å
³å·¥å
·ç±» |
| | | * |
| | | * @author è马 |
| | | */ |
| | | public class EncryptUtils { |
| | | /** |
| | | * å
¬é¥ |
| | | */ |
| | | public static final String PUBLIC_KEY = "publicKey"; |
| | | /** |
| | | * ç§é¥ |
| | | */ |
| | | public static final String PRIVATE_KEY = "privateKey"; |
| | | |
| | | /** |
| | | * Base64å å¯ |
| | | * |
| | | * @param data å¾
å 坿°æ® |
| | | * @return å å¯åå符串 |
| | | */ |
| | | public static String encryptByBase64(String data) { |
| | | return Base64.encode(data, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * Base64è§£å¯ |
| | | * |
| | | * @param data å¾
è§£å¯æ°æ® |
| | | * @return è§£å¯åå符串 |
| | | */ |
| | | public static String decryptByBase64(String data) { |
| | | return Base64.decodeStr(data, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * AESå å¯ |
| | | * |
| | | * @param data å¾
è§£å¯æ°æ® |
| | | * @param password ç§é¥å符串 |
| | | * @return å å¯åå符串, éç¨Base64ç¼ç |
| | | */ |
| | | public static String encryptByAes(String data, String password) { |
| | | if (StrUtil.isBlank(password)) { |
| | | throw new IllegalArgumentException("AESéè¦ä¼ å
¥ç§é¥ä¿¡æ¯"); |
| | | } |
| | | // aesç®æ³çç§é¥è¦æ±æ¯16ä½ã24ä½ã32ä½ |
| | | int[] array = {16, 24, 32}; |
| | | if (!ArrayUtil.contains(array, password.length())) { |
| | | throw new IllegalArgumentException("AESç§é¥é¿åº¦è¦æ±ä¸º16ä½ã24ä½ã32ä½"); |
| | | } |
| | | return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).encryptBase64(data, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * AESè§£å¯ |
| | | * |
| | | * @param data å¾
è§£å¯æ°æ® |
| | | * @param password ç§é¥å符串 |
| | | * @return è§£å¯åå符串 |
| | | */ |
| | | public static String decryptByAes(String data, String password) { |
| | | if (StrUtil.isBlank(password)) { |
| | | throw new IllegalArgumentException("AESéè¦ä¼ å
¥ç§é¥ä¿¡æ¯"); |
| | | } |
| | | // aesç®æ³çç§é¥è¦æ±æ¯16ä½ã24ä½ã32ä½ |
| | | int[] array = {16, 24, 32}; |
| | | if (!ArrayUtil.contains(array, password.length())) { |
| | | throw new IllegalArgumentException("AESç§é¥é¿åº¦è¦æ±ä¸º16ä½ã24ä½ã32ä½"); |
| | | } |
| | | return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).decryptStr(data, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * sm4å å¯ |
| | | * |
| | | * @param data å¾
å 坿°æ® |
| | | * @param password ç§é¥å符串 |
| | | * @return å å¯åå符串, éç¨Base64ç¼ç |
| | | */ |
| | | public static String encryptBySm4(String data, String password) { |
| | | if (StrUtil.isBlank(password)) { |
| | | throw new IllegalArgumentException("SM4éè¦ä¼ å
¥ç§é¥ä¿¡æ¯"); |
| | | } |
| | | // sm4ç®æ³çç§é¥è¦æ±æ¯16ä½é¿åº¦ |
| | | int sm4PasswordLength = 16; |
| | | if (sm4PasswordLength != password.length()) { |
| | | throw new IllegalArgumentException("SM4ç§é¥é¿åº¦è¦æ±ä¸º16ä½"); |
| | | } |
| | | return SmUtil.sm4(password.getBytes(StandardCharsets.UTF_8)).encryptBase64(data, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * sm4è§£å¯ |
| | | * |
| | | * @param data å¾
è§£å¯æ°æ® |
| | | * @param password ç§é¥å符串 |
| | | * @return è§£å¯åå符串 |
| | | */ |
| | | public static String decryptBySm4(String data, String password) { |
| | | if (StrUtil.isBlank(password)) { |
| | | throw new IllegalArgumentException("SM4éè¦ä¼ å
¥ç§é¥ä¿¡æ¯"); |
| | | } |
| | | // sm4ç®æ³çç§é¥è¦æ±æ¯16ä½é¿åº¦ |
| | | int sm4PasswordLength = 16; |
| | | if (sm4PasswordLength != password.length()) { |
| | | throw new IllegalArgumentException("SM4ç§é¥é¿åº¦è¦æ±ä¸º16ä½"); |
| | | } |
| | | return SmUtil.sm4(password.getBytes(StandardCharsets.UTF_8)).decryptStr(data, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * 产çsm2å è§£å¯éè¦çå
¬é¥åç§é¥ |
| | | * |
| | | * @return å
¬ç§é¥Map |
| | | */ |
| | | public static Map<String, String> generateSm2Key() { |
| | | Map<String, String> keyMap = new HashMap<>(2); |
| | | SM2 sm2 = SmUtil.sm2(); |
| | | keyMap.put(PRIVATE_KEY, sm2.getPrivateKeyBase64()); |
| | | keyMap.put(PUBLIC_KEY, sm2.getPublicKeyBase64()); |
| | | return keyMap; |
| | | } |
| | | |
| | | /** |
| | | * sm2å
¬é¥å å¯ |
| | | * |
| | | * @param data å¾
å 坿°æ® |
| | | * @param publicKey å
¬é¥ |
| | | * @return å å¯åå符串, éç¨Base64ç¼ç |
| | | */ |
| | | public static String encryptBySm2(String data, String publicKey) { |
| | | if (StrUtil.isBlank(publicKey)) { |
| | | throw new IllegalArgumentException("SM2éè¦ä¼ å
¥å
¬é¥è¿è¡å å¯"); |
| | | } |
| | | SM2 sm2 = SmUtil.sm2(null, publicKey); |
| | | return sm2.encryptBase64(data, StandardCharsets.UTF_8, KeyType.PublicKey); |
| | | } |
| | | |
| | | /** |
| | | * sm2ç§é¥è§£å¯ |
| | | * |
| | | * @param data å¾
å 坿°æ® |
| | | * @param privateKey ç§é¥ |
| | | * @return è§£å¯åå符串 |
| | | */ |
| | | public static String decryptBySm2(String data, String privateKey) { |
| | | if (StrUtil.isBlank(privateKey)) { |
| | | throw new IllegalArgumentException("SM2éè¦ä¼ å
¥ç§é¥è¿è¡è§£å¯"); |
| | | } |
| | | SM2 sm2 = SmUtil.sm2(privateKey, null); |
| | | return sm2.decryptStr(data, KeyType.PrivateKey, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * 产çRSAå è§£å¯éè¦çå
¬é¥åç§é¥ |
| | | * |
| | | * @return å
¬ç§é¥Map |
| | | */ |
| | | public static Map<String, String> generateRsaKey() { |
| | | Map<String, String> keyMap = new HashMap<>(2); |
| | | RSA rsa = SecureUtil.rsa(); |
| | | keyMap.put(PRIVATE_KEY, rsa.getPrivateKeyBase64()); |
| | | keyMap.put(PUBLIC_KEY, rsa.getPublicKeyBase64()); |
| | | return keyMap; |
| | | } |
| | | |
| | | /** |
| | | * rsaå
¬é¥å å¯ |
| | | * |
| | | * @param data å¾
å 坿°æ® |
| | | * @param publicKey å
¬é¥ |
| | | * @return å å¯åå符串, éç¨Base64ç¼ç |
| | | */ |
| | | public static String encryptByRsa(String data, String publicKey) { |
| | | if (StrUtil.isBlank(publicKey)) { |
| | | throw new IllegalArgumentException("RSAéè¦ä¼ å
¥å
¬é¥è¿è¡å å¯"); |
| | | } |
| | | RSA rsa = SecureUtil.rsa(null, publicKey); |
| | | return rsa.encryptBase64(data, StandardCharsets.UTF_8, KeyType.PublicKey); |
| | | } |
| | | |
| | | /** |
| | | * rsaç§é¥è§£å¯ |
| | | * |
| | | * @param data å¾
å 坿°æ® |
| | | * @param privateKey ç§é¥ |
| | | * @return è§£å¯åå符串 |
| | | */ |
| | | public static String decryptByRsa(String data, String privateKey) { |
| | | if (StrUtil.isBlank(privateKey)) { |
| | | throw new IllegalArgumentException("RSAéè¦ä¼ å
¥ç§é¥è¿è¡è§£å¯"); |
| | | } |
| | | RSA rsa = SecureUtil.rsa(privateKey, null); |
| | | return rsa.decryptStr(data, KeyType.PrivateKey, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * md5å å¯ |
| | | * |
| | | * @param data å¾
å 坿°æ® |
| | | * @return å å¯åå符串, éç¨Hexç¼ç |
| | | */ |
| | | public static String encryptByMd5(String data) { |
| | | return SecureUtil.md5(data); |
| | | } |
| | | |
| | | /** |
| | | * sha256å å¯ |
| | | * |
| | | * @param data å¾
å 坿°æ® |
| | | * @return å å¯åå符串, éç¨Hexç¼ç |
| | | */ |
| | | public static String encryptBySha256(String data) { |
| | | return SecureUtil.sha256(data); |
| | | } |
| | | |
| | | } |