4 changed files with 62 additions and 2 deletions
@ -0,0 +1,48 @@ |
|||
package com.ccsens.util; |
|||
|
|||
import cn.hutool.core.codec.Base64; |
|||
import cn.hutool.core.util.CharsetUtil; |
|||
import cn.hutool.core.util.ObjectUtil; |
|||
import cn.hutool.core.util.StrUtil; |
|||
import cn.hutool.crypto.SecureUtil; |
|||
import cn.hutool.crypto.symmetric.SymmetricAlgorithm; |
|||
import cn.hutool.crypto.symmetric.SymmetricCrypto; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
/** |
|||
* @author 逗 |
|||
*/ |
|||
@Slf4j |
|||
@Component |
|||
public class SymmetricCryptoUtil { |
|||
|
|||
/**生成密钥*/ |
|||
public static String generateKey(){ |
|||
byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded(); |
|||
return Base64.encode(key); |
|||
} |
|||
|
|||
/**加密*/ |
|||
public static String encrypt(String key, String value){ |
|||
String encryptString = null; |
|||
if(StrUtil.isNotBlank(key) && StrUtil.isNotBlank(value)){ |
|||
SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, Base64.decode(key)); |
|||
encryptString = aes.encryptHex(value); |
|||
} |
|||
return encryptString; |
|||
} |
|||
|
|||
/**解密*/ |
|||
public static String decode(String key, String encryptString){ |
|||
SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, Base64.decode(key)); |
|||
//解密为字符串
|
|||
String decodeString = null; |
|||
try { |
|||
decodeString = aes.decryptStr(encryptString, CharsetUtil.CHARSET_UTF_8); |
|||
}catch (Exception e){ |
|||
log.error("解密失败"); |
|||
} |
|||
return decodeString; |
|||
} |
|||
} |
Loading…
Reference in new issue