C#简单的加密类实例

所属分类: 软件编程 / C#教程 阅读数: 135
收藏 0 赞 0 分享
复制代码 代码如下:

public static class EncryptAndDecrypt
     {
         //加密
         public static string Encrypt(string input)
         {
             // 盐值
             string saltValue = "saltValue";
             // 密码值
             string pwdValue = "pwdValue";
             byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(input);
             byte[] salt = System.Text.UTF8Encoding.UTF8.GetBytes(saltValue);
             // AesManaged - 高级加密标准(AES) 对称算法的管理类
             System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged();
             // Rfc2898DeriveBytes - 通过使用基于 HMACSHA1 的伪随机数生成器,实现基于密码的密钥派生功能 (PBKDF2 - 一种基于密码的密钥派生函数)
             // 通过 密码 和 salt 派生密钥
             System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes(pwdValue, salt);
             /**/
             /*
          * AesManaged.BlockSize - 加密操作的块大小(单位:bit)
          * AesManaged.LegalBlockSizes - 对称算法支持的块大小(单位:bit)
          * AesManaged.KeySize - 对称算法的密钥大小(单位:bit)
          * AesManaged.LegalKeySizes - 对称算法支持的密钥大小(单位:bit)
          * AesManaged.Key - 对称算法的密钥
          * AesManaged.IV - 对称算法的密钥大小
          * Rfc2898DeriveBytes.GetBytes(int 需要生成的伪随机密钥字节数) - 生成密钥
          */
             aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
             aes.KeySize = aes.LegalKeySizes[0].MaxSize;
             aes.Key = rfc.GetBytes(aes.KeySize / 8);
             aes.IV = rfc.GetBytes(aes.BlockSize / 8);
             // 用当前的 Key 属性和初始化向量 IV 创建对称加密器对象
             System.Security.Cryptography.ICryptoTransform encryptTransform = aes.CreateEncryptor();
             // 加密后的输出流
             System.IO.MemoryStream encryptStream = new System.IO.MemoryStream();
             // 将加密后的目标流(encryptStream)与加密转换(encryptTransform)相连接
             System.Security.Cryptography.CryptoStream encryptor = new System.Security.Cryptography.CryptoStream
                 (encryptStream, encryptTransform, System.Security.Cryptography.CryptoStreamMode.Write);
             // 将一个字节序列写入当前 CryptoStream (完成加密的过程)
             encryptor.Write(data, 0, data.Length);
             encryptor.Close();
             // 将加密后所得到的流转换成字节数组,再用Base64编码将其转换为字符串
             string encryptedString = Convert.ToBase64String(encryptStream.ToArray());
             return encryptedString;
         }

 
         #region silverlight密码解密
         /**/
         /// <summary>
         /// 解密数据
         /// </summary>
         /// <param name="input">加密后的字符串</param>
         /// <returns>加密前的字符串</returns>
         public static string Decrypt(string input)
         {
             // 盐值(与加密时设置的值一致)
             string saltValue = "saltValue";
             // 密码值(与加密时设置的值一致)
             string pwdValue = "pwdValue";
             byte[] encryptBytes = Convert.FromBase64String(input);
             byte[] salt = Encoding.UTF8.GetBytes(saltValue);
             System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged();
             System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes(pwdValue, salt);
             aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
             aes.KeySize = aes.LegalKeySizes[0].MaxSize;
             aes.Key = rfc.GetBytes(aes.KeySize / 8);
             aes.IV = rfc.GetBytes(aes.BlockSize / 8);
             // 用当前的 Key 属性和初始化向量 IV 创建对称解密器对象
             System.Security.Cryptography.ICryptoTransform decryptTransform = aes.CreateDecryptor();
             // 解密后的输出流
             MemoryStream decryptStream = new MemoryStream();
             // 将解密后的目标流(decryptStream)与解密转换(decryptTransform)相连接
             System.Security.Cryptography.CryptoStream decryptor = new System.Security.Cryptography.CryptoStream(
                 decryptStream, decryptTransform, System.Security.Cryptography.CryptoStreamMode.Write);
             // 将一个字节序列写入当前 CryptoStream (完成解密的过程)
             decryptor.Write(encryptBytes, 0, encryptBytes.Length);
             decryptor.Close();
             // 将解密后所得到的流转换为字符串
             byte[] decryptBytes = decryptStream.ToArray();
             string decryptedString = UTF8Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
             return decryptedString;
         }
         #endregion
     }
更多精彩内容其他人还在看

C# 大小写转换(金额)实例代码

C# 大小写转换(金额)实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm中Panel实现用鼠标操作滚动条的实例方法

由于在WinForm中Panel不能直接响应鼠标的滚动事件,只好采用捕获窗体的滚动事件。
收藏 0 赞 0 分享

C#访问应用程序配置文件的方法

C#访问应用程序配置文件的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#读写文件的方法汇总

C#读写文件的方法汇总,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#计算代码执行时间的方法

在一些测试工作时我们需要获得高精度的代码执行时间以比较其效率。
收藏 0 赞 0 分享

C# 动画窗体(AnimateWindow)的小例子

C# 动画窗体(AnimateWindow)的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# zxing二维码写入的实例代码

C# zxing二维码写入的实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

c#判断输入的是不是数字的小例子

c#判断输入的是不是数字的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

c# 深拷贝与浅拷贝的区别分析及实例

浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用. 深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的.
收藏 0 赞 0 分享

C#利用com操作excel释放进程的解决方法

最近利用Microsoft.Office.Interop.Excel.Application读取一个excel后,进程中一直存在excel,在网上找了一阵子,其中有几个解决方案
收藏 0 赞 0 分享
查看更多