关于.NET加密技术

来源:互联网 发布:淘宝库存怎么导出 编辑:程序博客网 时间:2024/05/16 18:14
导读:
我现在网站用户密码加密用的是.net自带的
FormsAuthentication.HashPasswordForStoringInConfigFile(Password,"MD5");
        //加密密码变成32位小写字符串
pass= FormsAuthentication.HashPasswordForStoringInConfigFile(pass,"md5");
pass = pass.ToLower();
***************************************
哈希加密密码
using System.Web.Security;
password = FormsAuthentication.HashPasswordForStoringInConfigFile(password + dr["注册IP"], "SHA1");
关于加密:
对流可以执行对称加密,因此对称加密对于加密大量的数据很有用。对少量字节执行不对称加密,因此不对称加密只对少量的数据有用
在ASP.NET中实现加密非常容易。.NET SDK中提供了FormsAuthentication类,其中的HashPasswordForStoringInConfigFile方法可直接使用MD5
和SHA1算法。
例: MD5.Text = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text,"MD5");
        //SHA1 use FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text,"SHA1");
加密用于达到以下目的:
保密性:防止用户的标识或数据被读取。
数据完整性:防止数据被更改。
身份验证:确保数据发自特定的一方
私钥加密
私钥加密又称为对称加密,因为同一密钥既用于加密又用于解密。私钥加密算法非常快(与公钥算法相比),特别适用于对较大的数据流执行
加密转换。它使用一个密钥和一个初始化向量 (IV)对数据执行加密转换
NET Framework 提供以下实现私钥加密算法的类:
DESCryptoServiceProvider
RC2CryptoServiceProvider
RijndaelManaged
TripleDESCryptoServiceProvider
DESCryptoServiceProvider 类
示例使用具有指定 Key 和初始化向量 (IV) 的 DESCryptoServiceProvider,加密 inName指定的文件,并将加密结果输出到 outName 指定的
文件。
private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
        {
        //Create the file streams to handle the input and output files.
        FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
        FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
        fout.SetLength(0);
        //Create variables to help with read and write.
        byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
        long rdlen =0; //This is the total number of bytes written.
        long totlen = fin.Length; //This is the total length of the input file.
        int len; //This is the number of bytes to be written at a time.
        DES des = new DESCryptoServiceProvider();
        CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
        Console.WriteLine("Encrypting...");
        //Read from the input file, then encrypt and write to the output file.
        while(rdlen         {
        len = fin.Read(bin, 0, 100);
        encStream.Write(bin, 0, len);
        rdlen = rdlen + len;
        Console.WriteLine("{0} bytes processed", rdlen);
        }
        encStream.Close();
        fout.Close();
        fin.Close();
        }
[Visual Basic, C#] 可以相同的方法进行解密;使用 CreateDecryptor而不是 CreateEncryptor。必须使用加密该文件所用的同一 Key 和
IV 进行解密。
RC2CryptoServiceProvider 类
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace RC2CryptoServiceProvider_Examples
{
        class MyMainClass
        {
        public static void Main()
        {
        string original = "This is a much longer string of data than a public/private key algorithm will accept.";
        string roundtrip;
        ASCIIEncoding textConverter = new ASCIIEncoding();
        RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();
        byte[] fromEncrypt;
        byte[] encrypted;
        byte[] toEncrypt;
        byte[] key;
        byte[] IV;
        Console.WriteLine("Effective key size is {0} bits.", rc2CSP.EffectiveKeySize);
        //Create a new key and initialization vector.
        rc2CSP.GenerateKey();
        rc2CSP.GenerateIV();
        //Get the key and IV.
        key = rc2CSP.Key;
        本文转自
http://blog.sina.com.cn/s/blog_3f33dac1010003zp.html
原创粉丝点击