JAVA 对应的 AES 加密 与 解密

jovien 2017-06-29 03:23:05

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);
}



求大神们写一个对应的C#版本的加密与揭秘方法!!!!!!!!!!!!
...全文
333 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jovien 2017-06-29
  • 打赏
  • 举报
回复
引用 5 楼 guwei4037 的回复:
/// <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);
}	
JAVA 中 还有一段忘记贴出来了,采用的是CBC模式,增加了向量 private static final String SECRET_KEY = "2cc8325f45829ed1"; private static IvParameterSpec ivParameterSpec; private static SecretKeySpec secretKeySpec; private static Cipher cipher;
全栈极简 2017-06-29
  • 打赏
  • 举报
回复
/// <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);
}	
全栈极简 2017-06-29
  • 打赏
  • 举报
回复
自己对照着修改一下。
/// <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);
}
jovien 2017-06-29
  • 打赏
  • 举报
回复
再没人来帖子就沉了啊
jovien 2017-06-29
  • 打赏
  • 举报
回复
高人都没时间来指导一下吗?
jovien 2017-06-29
  • 打赏
  • 举报
回复
希望大神帮助一下。。。

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧