vs 2005中的加密无解密问题

http://access911.net/n/doc1.asp?mode=a&bid=005202&aid=4988810 

using System;
using System.Security.Cryptography ;
using System.Text;
using System.IO;


namespace SEDO
{
/// <summary>
/// SEDO 的摘要说明。
/// SEDO 实现的是用一个封装了4种对称加密方法(Des,Rc2,Rijndael,TripleDes)的组件
///
/// 注意事项:
/// 1:TripleDes和Rijndael加密/解密对象使用16或者24位byte的Key
/// 2:Rijndael只能使用16位的初始化向量IV
/// 3:Des和Rc2均使用8位Byte的Key和IV
/// 4:对需要加密/解密的数据流采用何种方法进行编码/解码,由调用组件的用户自己决定
/// 5:密钥和初始化向量IV由使用者自己定义
/// 程序员: 王海波 2003-05-19 [email protected]
/// </summary>

//定义加密类型的枚举
public enum EncryptionAlgorithm {Des = 1, Rc2, Rijndael, TripleDes};


//定义加密类
internal class EncryptTransformer
{
private EncryptionAlgorithm algorithmID;
private byte[] initVec;
private byte[] encKey;

internal EncryptTransformer(EncryptionAlgorithm algId)
{
//Save the algorithm being used.
algorithmID = algId;
}

internal ICryptoTransform GetCryptoServiceProvider(byte[] bytesKey)
{
//当数据密钥Key或者初始化向量IV为空的时候,将使用加密对象自动产生的密钥Key或者初始化向量IV
switch (algorithmID)
{
case EncryptionAlgorithm.Des:
{
DES des = new DESCryptoServiceProvider();
des.Mode = CipherMode.CBC;

// See if a key was provided
if (null == bytesKey)
{
encKey = des.Key;
}
else
{
des.Key = bytesKey;
encKey = des.Key;
}
// See if the client provided an initialization vector
if (null == initVec)
{ // Have the algorithm create one
initVec = des.IV;
}
else
{ //No, give it to the algorithm
des.IV = initVec;
}
return des.CreateEncryptor();
}
case EncryptionAlgorithm.TripleDes:
{
TripleDES des3 = new TripleDESCryptoServiceProvider();
des3.Mode = CipherMode.CBC;
// See if a key was provided
if (null == bytesKey)
{
encKey = des3.Key;
}
else
{
des3.Key = bytesKey;
encKey = des3.Key;
}
// See if the client provided an IV
if (null == initVec)
{ //Yes, have the alg create one
initVec = des3.IV;
}
else
{ //No, give it to the alg.
des3.IV = initVec;
}
return des3.CreateEncryptor();
}
case EncryptionAlgorithm.Rc2:
{
RC2 rc2 = new RC2CryptoServiceProvider();
rc2.Mode = CipherMode.CBC;
// Test to see if a key was provided
if (null == bytesKey)
{
encKey = rc2.Key;
}
else
{
rc2.Key = bytesKey;
encKey = rc2.Key;
}
// See if the client provided an IV
if (null == initVec)
{ //Yes, have the alg create one
initVec = rc2.IV;
}
else
{ //No, give it to the alg.
rc2.IV = initVec;
}
return rc2.CreateEncryptor();
}
case EncryptionAlgorithm.Rijndael:
{
Rijndael rijndael = new RijndaelManaged();
rijndael.Mode = CipherMode.CBC;
// Test to see if a key was provided
if(null == bytesKey)
{
encKey = rijndael.Key;
}
else
{
rijndael.Key = bytesKey;
encKey = rijndael.Key;
}
// See if the client provided an IV
if(null == initVec)
{ //Yes, have the alg create one
initVec = rijndael.IV;
}
else
{ //No, give it to the alg.
rijndael.IV = initVec;
}
return rijndael.CreateEncryptor();
}
default:
{
throw new CryptographicException("Algorithm ID '" +
algorithmID +
"' not supported.");
}
}
}

 

 

md5加密

static   public   string   EncodePassword(string   strInput,   int   EncodeType)  
          {  
                  if   (EncodeType   ==   1)  
                          return   System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strInput,   "md5");  
                  else  
                          return   System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strInput,   "sha1");  
          }

发布了33 篇原创文章 · 获赞 0 · 访问量 6万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章