110,538
社区成员
发帖
与我相关
我的任务
分享
public static void commonEncryptDecrypt(String pwd) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException {
ivParameterSpec = new IvParameterSpec(SECRET_KEY.getBytes("UTF-8"));
secretKeySpec = new SecretKeySpec(pwd.getBytes("UTF-8"), "AES");
cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
}
//加密
public static String encrypt(String toBeEncrypt,String pwd) throws InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException{
commonEncryptDecrypt(pwd);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] encrypted = cipher.doFinal(toBeEncrypt.getBytes());
return Base64.getEncoder().encodeToString(Base64.getEncoder().encode(encrypted));
}
//解密
public static String decrypt(String encrypted,String pwd) throws InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException {
commonEncryptDecrypt(pwd);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] baseStr = Base64.getDecoder().decode(encrypted);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(baseStr));
return new String(decryptedBytes);
}
/// <summary>
/// AES加密
/// </summary>
/// <param name="data">要加密的数据</param>
/// <param name="key">密钥</param>
/// <returns>AES加密结果</returns>
public static string AESEncrypt(string data, string key)
{
Aes aes = new AesCryptoServiceProvider();
aes.Key = Encoding.UTF8.GetBytes(key);
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
ICryptoTransform cryptoTransform = aes.CreateEncryptor();
byte[] bytes = Encoding.UTF8.GetBytes(data);
byte[] encryptedBytes = cryptoTransform.TransformFinalBlock(bytes, 0, bytes.Length);
return Convert.ToBase64String(encryptedBytes);
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="data">要解密的数据</param>
/// <param name="key">密钥</param>
/// <returns>AES解密结果</returns>
public static string AESDecrypt(string data, string key)
{
byte[] bytes = Convert.FromBase64String(data);
Aes aes = new AesCryptoServiceProvider();
aes.Key = Encoding.UTF8.GetBytes(key);
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
ICryptoTransform cryptoTransform = aes.CreateDecryptor();
byte[] encryptedBytes = cryptoTransform.TransformFinalBlock(bytes, 0, bytes.Length);
return Encoding.UTF8.GetString(encryptedBytes);
}
/// <summary>
/// AES加密
/// </summary>
/// <param name="data">要加密的数据</param>
/// <param name="key">密钥</param>
/// <returns>AES加密结果</returns>
public static string AESEncrypt(string data, string key)
{
Aes aes = new AesCryptoServiceProvider();/// <summary>
/// AES解密
/// </summary>
/// <param name="data">要解密的数据</param>
/// <param name="key">密钥</param>
/// <returns>AES解密结果</returns>
public static string AESDecrypt(string data, string key)
{
byte[] bytes = Convert.FromBase64String(data);
Aes aes = new AesCryptoServiceProvider();
aes.Key = Encoding.UTF8.GetBytes(key);
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
ICryptoTransform cryptoTransform = aes.CreateDecryptor();
byte[] encryptedBytes = cryptoTransform.TransformFinalBlock(bytes, 0, bytes.Length);
return Encoding.UTF8.GetString(encryptedBytes);
}
aes.Key = Encoding.UTF8.GetBytes(key);
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
ICryptoTransform cryptoTransform = aes.CreateEncryptor();
byte[] bytes = Encoding.UTF8.GetBytes(data);
byte[] encryptedBytes = cryptoTransform.TransformFinalBlock(bytes, 0, bytes.Length);
return Convert.ToBase64String(encryptedBytes);
}