ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Aes加密

2021-05-22 23:30:58  阅读:252  来源: 互联网

标签:Aes 加密 string encrypted IV Key original


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

代码下载

标签:Aes,加密,string,encrypted,IV,Key,original
来源: https://blog.csdn.net/l1300568813/article/details/117173276

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有