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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章