关于.NET加密技术

导读:
我现在网站用户密码加密用的是.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;

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值