• .NET中的加密和解密


    对一个字符串进行加密所需的步骤如下:

    1.将源字符串转换成一个字节数组

    2.初始化一个加密算法类

    3.使用这个加密算法类来生成一个加密者(encryptor对象),实现IcryptoTransform接口。他需要密钥和IV值。

    4.使用加密者对象来初始化一个密文数据流(CryptoStream对象)。该数据流还需要知道你要加密哪些数据,以及用来写入加密数据的目标数据流。

    5.使用这个密文数据流生成已加密数据,并写到由前面创建的源字节数组创建的目标内存数据流中。

    6.获取存储在这个数据流中的字节数据。

    7.将这些字节数据转换成一个字符串。

    解密采用的模式与加密类似:

    1.将源字符串转换成一个字节数组。

    2.根据这个字节数组填充内存数据流的值。

    3.初始化一个加密算法类。

    4.使用加密算法类生成一个解密者(decryptor对象),实现ICrytoTransform接口。他需要密钥和IV值。

    5.使用解密者对象来初始化一个密文数据流(CryptoStream对象)。该数据流还需要知道你要解密什么数据,并需要一个从中读取已加密数据的源数据流。

    6.使用密文数据流来读取已解密数据(可以使用StreamReader.ReadToEnd方法来获取字符串类型结果)。

    代码示例:

    namespace SecurityLib
    {
        /// <summary>
        ///StringEncryptor 的摘要说明
        /// </summary>
        public static class StringEncryptor
        {
            public static string Encrypt(string sourceData)
            {
                byte[] key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
                byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
                try
                {
                    byte[] sourceDataBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(sourceData);
                    MemoryStream tempStream = new MemoryStream();
                    DESCryptoServiceProvider encryptor = new DESCryptoServiceProvider();
                    CryptoStream encryptionStream = new CryptoStream(tempStream,
                        encryptor.CreateEncryptor(key, iv), CryptoStreamMode.Write);
                    encryptionStream.Write(sourceDataBytes, 0, sourceDataBytes.Length);
                    encryptionStream.FlushFinalBlock();
                    byte[] encryptedDataBytes = tempStream.GetBuffer();
                    return Convert.ToBase64String(encryptedDataBytes, 0, (int)tempStream.Length);
                }
                catch
                {
                    throw new StringEncryptorException("Unable to encrypt data.");
                }
            }
    
            public static string Decrypt(string sourceData)
            {
                byte[] key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
                byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
                try
                {
                    byte[] encryptedDataBytes = Convert.FromBase64String(sourceData);
                    MemoryStream tempStream = new MemoryStream(encryptedDataBytes, 0, encryptedDataBytes.Length);
                    DESCryptoServiceProvider decryptor = new DESCryptoServiceProvider();
                    CryptoStream decryptionStream = new CryptoStream(tempStream,
                        decryptor.CreateDecryptor(key, iv), CryptoStreamMode.Read);
                    StreamReader allDataReader = new StreamReader(decryptionStream);
                    return allDataReader.ReadToEnd();
                }
                catch
                {
                    throw new StringEncryptorException("Unable to decrypt data.");
                }
            }
        }
    }
  • 相关阅读:
    2018ACM上海大都会赛 F Color it【基础的扫描线】
    2018大都会赛 A Fruit Ninja【随机数】
    两个数互质的概率
    【shell脚本学习-3】
    【mysql学习-1】
    【HCNE题型自我考究】
    【为系统营造的一个安全的环境】
    【nginx下对服务器脚本php的支持】
    【linux基于Postfix和Dovecot邮件系统的搭建】
    不同状态的动态路由协议对比
  • 原文地址:https://www.cnblogs.com/nero/p/1630907.html
Copyright © 2020-2023  润新知