C#简单的加密类
1.加密
public class EncryptHepler {
// 验值
static string saltValue = "XXXX";
// 密码值
static string pwdValue = "XXXX";
/// <summary>
/// 加密
/// </summary>
public static string Encrypt( string input ) {
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 );
aes.BlockSize = aes.LegalBlockSizes[].MaxSize;
aes.KeySize = aes.LegalKeySizes[].MaxSize;
aes.Key = rfc.GetBytes( aes.KeySize / );
aes.IV = rfc.GetBytes( aes.BlockSize / );
// 用当前的 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, , data.Length );
encryptor.Close( );
// 将加密后所得到的流转换成字节数组,再用Base64编码将其转换为字符串
string encryptedString = Convert.ToBase64String( encryptStream.ToArray( ) );
return encryptedString;
}
2.解密
/// <summary>
/// 解密
/// </summary>
public static string Decrypt( string input ) {
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[].MaxSize;
aes.KeySize = aes.LegalKeySizes[].MaxSize;
aes.Key = rfc.GetBytes( aes.KeySize / );
aes.IV = rfc.GetBytes( aes.BlockSize / ); // 用当前的 Key 属性和初始化向量 IV 创建对称解密器对象
System.Security.Cryptography.ICryptoTransform decryptTransform = aes.CreateDecryptor( );
// 解密后的输出流
System.IO.MemoryStream decryptStream = new System.IO.MemoryStream( ); // 将解密后的目标流(decryptStream)与解密转换(decryptTransform)相连接
System.Security.Cryptography.CryptoStream decryptor = new System.Security.Cryptography.CryptoStream(
decryptStream, decryptTransform, System.Security.Cryptography.CryptoStreamMode.Write );
// 将一个字节序列写入当前 CryptoStream (完成解密的过程)
decryptor.Write( encryptBytes, , encryptBytes.Length );
decryptor.Close( ); // 将解密后所得到的流转换为字符串
byte[ ] decryptBytes = decryptStream.ToArray( );
string decryptedString = UTF8Encoding.UTF8.GetString( decryptBytes, , decryptBytes.Length );
return decryptedString;
}
}//class end
C#简单的加密类的更多相关文章
- php加密类
1.需求 了解php加密类的使用 2.例子 参考ci的3.1.2的新版加密类,一个不传参,用默认加密算法,加密模式的例子 //0.加载加密类 $this->load->library('e ...
- VC++ 一个简单的Log类
在软件开发中,为程序建立Log日志是很必要的,它可以记录程序运行的状态以及出错信息,方便维护和调试. 下面实现了一个简单的Log类,使用非常简单,仅供参考. // CLogHelper.h : hea ...
- C#加密类
var es= EncryptSugar.GetInstance(); string word = "abc"; var wordEncrypt = es.Encrypto(wor ...
- PHP的AES加密类
PHP的AES加密类 aes.php <?php /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ...
- C++ 最简单的日志类
最近搞一个 C++ 项目的二次开发,没玩过 C++,可谓步履维艰.自己写个简单的日志类都被各种坑折磨.终于搞定了. 参考了这篇博客,并且进一步简化:https://www.cnblogs.com/Ds ...
- C# DES加密类,16位的加密。
这个加密类是与java写的DES加密不同时,自己写的,最后与Java的加密相同了,解决了加密后不同的问题. 可以直接调用里面的加密和解密的方法. using System; using System. ...
- Java实现一个简单的加密解密方法
Crypto是Java语言写的一个简单的加密解密方法. 使用方法: 加密方法 String cipherte=Enande.encrypt(content, pass): 解密方法 Enande.de ...
- php实现aes加密类
php实现的aes加密类,代码中有使用方法. <?php //php aes加密类 class AESMcrypt { public $iv = null; public $key = null ...
- python+selenium之自定义封装一个简单的Log类
python+selenium之自定义封装一个简单的Log类 一. 问题分析: 我们需要封装一个简单的日志类,主要有以下内容: 1. 生成的日志文件格式是 年月日时分秒.log 2. 生成的xxx.l ...
随机推荐
- Java再学习——停止一个正在运行的线程
关于这个问题,先了解一下Thread类方法中被废弃的那些方法.suspend(), resume(),stop()/stop(Throwable obj),destroy() 首先,stop(Thro ...
- Ext.Net TextField Enter事件
(1)DirectEvents触发后台 <ext:TextField ID="txt_Upc" runat="server" Width="15 ...
- Apple 预计于内华达州雷诺市再盖一个数据中心
Apple 为了满足各位对 Siri 姐(妹?那个谁去问问她年纪拜托)还有 iCloud 等云端服务的爱护,所以近期之内不断地在各地建置他们的数据中心来维持云端数据传输的稳定度.从美国的北卡. 俄勒冈 ...
- UIWebView获得内容的高 高度自适应 宽度自适应
UIWebView获得内容的高-作出自适应高的UIWebView- (void)webViewDidFinishLoad:(UIWebView *)webView { NSString *height ...
- Gvim 在进行文件对比时报cannot read or write temp files
本机环境为win7 64位旗舰版,gvim安装的是GVim7.4.解决办法如下: 在安装目录下有个"_vimrc"文件.修改19行.将 if &sh =~ '\<cm ...
- 【Linux】Shell脚本编程(三)
流程控制: 循环语句:for,while,until while循环: while CONDITION; do 循环体 done 进入条件:当CONDITION为“真”: 退出条件:当CONDITIO ...
- DWZ按钮居中显示
- C++字符类型总结区别wchar_t,char,WCHAR
转至:http://www.360doc.com/content/12/0807/01/9290626_228750141.shtml 1.区别wchar_t,char,WCHAR ANSI:即 ch ...
- 转:android 录制视频的Demo
转:http://blog.csdn.net/peijiangping1989/article/details/7049991 在这里给出自己的一个测试DEMO,里面注释很详细.简单的视频录制功能. ...
- ASP.NET 状态的传递和保存
1,HTTP协议是无状态的.服务器不会记住上次给浏览器的处理结果,如果需要上次处理结果(上次状态)就需要浏览器把处理结果值(上次状态)再次给服务器. 2,URL传值:通过URL参数或者通过Form表单 ...