Aes加密

Aes加密

#AES加密

密码学中的高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。

简介

这个标准用来替代原先的DES(Data Encryption Standard),已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院 (NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一 1
该算法为比利时密码学家Joan Daemen和Vincent Rijmen所设计,结合两位作者的名字,以Rijdael之名命之,投稿高级加密标准的甄选流程。(Rijdael的发音近于 “Rhine doll”。)

##代码

1.Key和IV

        const string _Key = "Shanghai_CaoHeJing_20210521_2137";  // key
        const string _IV = "_20210522202718_";                   // initialization vector

2.加密

/// <summary>
/// 使用指定的Key和IV加密字符串
/// </summary>
/// <param name="original">需要加密的字符串</param>
/// <returns>字符串加密后的密文</returns>
public static string Encrypt(string original)
{
    return AesEncrypt(original, _Key, _IV);
}

/// <summary>
/// 把字符串original加密
/// </summary>
/// <param name="original">需要加密的字符串</param>
/// <param name="Key">加密密钥</param>
/// <param name="IV">加密向量</param>
/// <returns>加密后的密文</returns>
static string AesEncrypt(string original, string Key, string IV)
{
    // Check arguments.
    if (original == null || original.Length <= 0)
        throw new ArgumentNullException("original");
    if (Key == null || Key.Length <= 0)
        throw new ArgumentNullException("Key");
    if (IV == null || IV.Length <= 0)
        throw new ArgumentNullException("IV");

    byte[] key = Encoding.UTF8.GetBytes(Key);
    byte[] iv = Encoding.UTF8.GetBytes(IV);
    byte[] encrypted;

    // Create an Aes object with the specified key and IV.
    using (Aes aes = Aes.Create())
    {
        aes.Key = key;
        aes.IV = iv;

        // Create an encryptor to perform the stream transform.
        ICryptoTransform transform = aes.CreateEncryptor();

        // Create the streams used for encryption.
        using(MemoryStream stream = new MemoryStream())
        {
            using(CryptoStream crypto = new CryptoStream(stream, transform, CryptoStreamMode.Write))
            {
                using(StreamWriter writer = new StreamWriter(crypto))
                {
                    // Write all data to the stream.
                    writer.Write(original);
                }
                encrypted = stream.ToArray();
            }
        }
    }
    return Convert.ToBase64String(encrypted);
}

2.解密

/// <summary>
/// 使用指定的Key和IV进行解密
/// </summary>
/// <param name="encrypted">需要解密的密文</param>
/// <returns>解密后的明文</returns>
public static string Decrypt(string encrypted)
{
   	return AesDecrypt(encrypted, _Key, _IV);
}

/// <summary>
/// 把字符串encrypted解密
/// </summary>
/// <param name="encrypted">需要解密的密文</param>
/// <param name="Key">加密密钥</param>
/// <param name="IV">加密向量</param>
/// <returns>解密后的明文</returns>
static string AesDecrypt(string encrypted, string Key, string IV)
{
    // Check arguments.
    if (encrypted == null || encrypted.Length <= 0)
        throw new ArgumentNullException("encrypted");
    if (Key == null || Key.Length <= 0)
        throw new ArgumentNullException("Key");
    if (IV == null || IV.Length <= 0)
        throw new ArgumentNullException("IV");

    byte[] data = Convert.FromBase64String(encrypted);
    byte[] key = Encoding.UTF8.GetBytes(Key);
    byte[] iv = Encoding.UTF8.GetBytes(IV);

    // Declare the string used to hold the decrypted text.
    string original = null;

    // Create an Aes object with the specified key and IV.
    using (Aes aes = Aes.Create())
    {
        aes.Key = key;
        aes.IV = iv;

        // Create a decryptor to perform the stream transform.
        ICryptoTransform transform = aes.CreateDecryptor();

        // Create the streams used for decryption.
        using (MemoryStream stream = new MemoryStream(data))
        {
            using(CryptoStream crypto = new CryptoStream(stream, transform, CryptoStreamMode.Read))
            {
                using(StreamReader reader = new StreamReader(crypto))
                {
                    // Read the decrypted bytes from the decrypting stream and place them in a string.
                    original = reader.ReadToEnd();
                }
            }
        }
    }
    return original;
}

代码下载

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值