parent
55364e3219
commit
aa10527a31
@ -0,0 +1,158 @@ |
||||
package com.cjy.back.ybsjAppointment.utils; |
||||
|
||||
|
||||
|
||||
import org.apache.commons.lang.StringUtils; |
||||
|
||||
import javax.crypto.Cipher; |
||||
import javax.crypto.spec.SecretKeySpec; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.util.Base64; |
||||
|
||||
/** |
||||
* @author liangjiawei |
||||
* @createDate 2023/7/17 |
||||
*/ |
||||
public class Encryption { |
||||
/** |
||||
* 加解密密钥, 外部可以 |
||||
*/ |
||||
public static final String AES_DATA_SECURITY_KEY = "4%YkW!@g5LGcf9Ut"; |
||||
/** |
||||
* 算法/加密模式/填充方式 |
||||
*/ |
||||
private static final String AES_PKCS5P = "AES/ECB/PKCS5Padding"; |
||||
|
||||
private static final String AES_PERSON_KEY_SECURITY_KEY = "pisnyMyZYXuCNcRd"; |
||||
|
||||
/** |
||||
* 加密 |
||||
* |
||||
* @param str |
||||
* 需要加密的字符串 |
||||
* @param key |
||||
* 密钥 |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public static String encrypt(String str, String key) { |
||||
if (StringUtils.isEmpty(key)) { |
||||
throw new RuntimeException("key不能为空"); |
||||
} |
||||
try { |
||||
if (str == null) { |
||||
return null; |
||||
} |
||||
// 判断Key是否为16位
|
||||
if (key.length() != 16) { |
||||
return null; |
||||
} |
||||
byte[] raw = key.getBytes(StandardCharsets.UTF_8); |
||||
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); |
||||
// "算法/模式/补码方式"
|
||||
Cipher cipher = Cipher.getInstance(AES_PKCS5P); |
||||
cipher.init(Cipher.ENCRYPT_MODE, skeySpec); |
||||
byte[] encrypted = cipher.doFinal(str.getBytes(StandardCharsets.UTF_8)); |
||||
// 使用Base64进行URL安全编码
|
||||
return Base64.getUrlEncoder().encodeToString(encrypted); |
||||
} catch (Exception ex) { |
||||
return null; |
||||
} |
||||
|
||||
} |
||||
|
||||
/** |
||||
* 解密 |
||||
* |
||||
* @param str 需要解密的字符串 |
||||
* @param key 密钥 |
||||
* @return |
||||
*/ |
||||
public static String decrypt(String str, String key) { |
||||
if (StringUtils.isEmpty(key)) { |
||||
throw new IllegalArgumentException("key不能为空"); |
||||
} |
||||
try { |
||||
if (StringUtils.isEmpty(str)) { |
||||
return null; |
||||
} |
||||
// 判断Key是否为16位
|
||||
if (key.getBytes(StandardCharsets.UTF_8).length != 16) { |
||||
return null; |
||||
} |
||||
byte[] raw = key.getBytes(StandardCharsets.UTF_8); |
||||
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); |
||||
Cipher cipher = Cipher.getInstance(AES_PKCS5P); |
||||
cipher.init(Cipher.DECRYPT_MODE, skeySpec); |
||||
// 先用base64解密
|
||||
byte[] encrypted = Base64.getUrlDecoder().decode(str); |
||||
try { |
||||
byte[] original = cipher.doFinal(encrypted); |
||||
return new String(original, StandardCharsets.UTF_8); |
||||
} catch (Exception e) { |
||||
throw new RuntimeException("解密失败", e); |
||||
} |
||||
} catch (Exception ex) { |
||||
throw new RuntimeException("解密失败", ex); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 加密 |
||||
* |
||||
* @param str 需要加密的字符串 |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public static String encrypt(String str) { |
||||
return encrypt(str,AES_DATA_SECURITY_KEY); |
||||
} |
||||
|
||||
/** |
||||
* 解密 |
||||
* @param str 需要解密的字符串 |
||||
* @return |
||||
*/ |
||||
public static String decrypt(String str) { |
||||
return decrypt(str,AES_DATA_SECURITY_KEY); |
||||
} |
||||
|
||||
/** |
||||
* 查询的时候对某些字段解密 |
||||
* |
||||
* @param str |
||||
* @return |
||||
*/ |
||||
public static String aesDecrypt(String str) { |
||||
if (StringUtils.isBlank(str)) { |
||||
return " "; |
||||
} |
||||
String sql = " AES_DECRYPT(from_base64(" + str + ")," + "'" + AES_DATA_SECURITY_KEY + "')"; |
||||
return sql; |
||||
} |
||||
|
||||
/** |
||||
* 对personKey加密 |
||||
* |
||||
* @param personKey |
||||
* @return |
||||
*/ |
||||
public static String encryptPersonKey(String personKey) { |
||||
return encrypt(personKey, AES_PERSON_KEY_SECURITY_KEY); |
||||
} |
||||
|
||||
/** |
||||
* 对personKey解密 |
||||
* |
||||
* @param personKey |
||||
* @return |
||||
*/ |
||||
public static String decryptPersonKey(String personKey) { |
||||
return decrypt(personKey, AES_PERSON_KEY_SECURITY_KEY); |
||||
} |
||||
|
||||
public static void main(String[] args) { |
||||
System.out.println(encrypt("3261015184","Xt9saen8xKY3eSmb")); |
||||
System.out.println(decrypt("sgU2EbT-lORewPaFztQKPg==","Xt9saen8xKY3eSmb")); |
||||
} |
||||
} |
Loading…
Reference in new issue