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

代码下载

上一篇:【Applied Algebra】可满足性模理论(Satisfiability Modulo Theories)入门


下一篇:Python爬取企查查网页中潜在的信息字典