登录集成插件
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

68 lines
2.1 KiB

package com.fr.plugin.decision.integration.utils;
import com.fr.base.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
/**
* @Author JianYe.Wang
* @Data 2021/8/16 15:06
* @Description TODO
* @Version 10.0
**/
public class AESUtils {
private static final String AES_ECB_PKCS_5_PADDING = "AES/ECB/PKCS5Padding";
private static final String HMAC_SHA_256 = "HmacSHA256";
/**
* AES加密
*
* @param content 待加密的内容
* @param encryptKey 加密密钥
* @return 加密后的byte[]
*/
public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
Cipher cipher = Cipher.getInstance(AES_ECB_PKCS_5_PADDING);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));
return cipher.doFinal(content.getBytes("utf-8"));
}
/**
* AES解密
*
* @param encryptBytes 待解密的byte[]
* @param decryptKey 解密密钥
* @return 解密后的String
*/
public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
Cipher cipher = Cipher.getInstance(AES_ECB_PKCS_5_PADDING);
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));
byte[] decryptBytes = cipher.doFinal(encryptBytes);
return new String(decryptBytes, "utf-8");
}
/**
* HmacSHA256 签名
*
* @param content
* @param signKey
* @return base64 encode
* @throws Exception
*/
public static String hmacSHA256Sign(String content, String signKey) throws Exception {
Mac sha256Hmac = Mac.getInstance(HMAC_SHA_256);
SecretKeySpec secret_key = new SecretKeySpec(signKey.getBytes(), HMAC_SHA_256);
sha256Hmac.init(secret_key);
return Base64.encode(sha256Hmac.doFinal(content.getBytes()));
}
}