C#_SymmetricAlgorithm(对称算法)

SymmetricAlgorithm(对称算法)

一、简介

对称密码算法有时又叫传统密码算法,就是加密密钥能够从解密密钥中推算出来,反过来也成立。

对称算法的加密和解密用公式可以表示为:

Ek(M)=C

Dk©=M

常用的采用对称密码术的加密方案有5个组成部分(如图所示)

l)明文:原始信息。

2)加密算法:以密钥为参数,对明文进行多种置换和转换的规则和步骤,变换结果为密文。

3)密钥:加密与解密算法的参数,直接影响对明文进行变换的结果。

4)密文:对明文进行变换的结果。

5)解密算法:加密算法的逆变换,以密文为输入、密钥为参数,变换结果为明文

二、SymmetricAlgorithm 的派生类

SymmetricAlgorithm Aes DES RC2 TripleDES

举例 使用Aes类进行加密和解密

    static void Main(string[] args)
    {
        string msg = "e1r2364uh发货的前方后仍1和和和破i就9哦破吉普请法25它";
        byte[] msgBytes = Encoding.Default.GetBytes(msg);

        using (Aes aes = Aes.Create())
        {
            Console.WriteLine("origin msg:" + BitConverter.ToString(msgBytes));

            byte[] enmsg = EncryptStringToBytes_Aes(msgBytes, aes.Key, aes.IV);
            Console.WriteLine("encrypt msg:" + BitConverter.ToString(enmsg));

            byte[] demsg = DecryptStringFromBytes_Aes(enmsg, aes.Key, aes.IV);
            Console.WriteLine("decrypt msg:" + BitConverter.ToString(demsg));
        }
    }
    static byte[] EncryptStringToBytes_Aes(byte[] plainText, byte[] Key, byte[] IV)
    {
        // Check arguments.
        if (plainText == null || plainText.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("IV");
        byte[] encrypted;

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

            // Create an encryptor to perform the stream transform.
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

            // Create the streams used for encryption.
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        //Write all data to the stream.
                        swEncrypt.Write(Encoding.Default.GetString(plainText));
                    }
                    encrypted = msEncrypt.ToArray();
                }
            }
        }

        // Return the encrypted bytes from the memory stream.
        return encrypted;
    }

    static byte[] DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
    {
        // Check arguments.
        if (cipherText == null || cipherText.Length <= 0)
            throw new ArgumentNullException("cipherText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("IV");

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

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

            // Create a decryptor to perform the stream transform.
            ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

            // Create the streams used for decryption.
            using (MemoryStream msDecrypt = new MemoryStream(cipherText))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {

                        // Read the decrypted bytes from the decrypting stream
                        // and place them in a string.
                        plaintext = srDecrypt.ReadToEnd();
                    }
                }
            }
        }

        return Encoding.Default.GetBytes(plaintext);
    }

参考:https://docs.microsoft.com/zh-cn/dotnet/api/system.security.cryptography.aes?view=net-5.0

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值