提供一个基于.NET的加密/解密算法[转.CNSDN.com.cn]

 提供一个基于.NET SymmetricAlgorithm 类的、带私钥的加密/解密算法的包装类。使用方法: 
   
None.gif  SymmCrypto de  =   new  SymmCrypto(SymmCrypto.SymmProvEnum.DES); 
None.gif  Response.Write(x.Decrypting(de.Encrypting(
" ok " , " yyy " ), " yyy " )); 
None.gif  类的实现C#编码 
None.gif   
None.gif  
using  System; 
None.gif  
using  System.Security.Cryptography; 
None.gif  
using  System.IO; 
None.gif  
using  System.Text; 
None.gif   
None.gif  
namespace  eMeng 
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// <summary> 
InBlock.gif   
/// SymmCrypto 的摘要说明。 
InBlock.gif   
/// SymmCrypto类实现.NET框架下的加密和解密服务。 
InBlock.gif   
/// 原作者: Frank Fang : fangfrank@hotmail.com 
ExpandedSubBlockEnd.gif   
/// </summary> 

InBlock.gif   public class SymmCrypto 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif   
public enum SymmProvEnum : int 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif   DES, RC2, Rijndael 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
InBlock.gif   
private SymmetricAlgorithm mobjCryptoService; 
InBlock.gif   
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// <remarks> 
InBlock.gif   
/// 使用.Net SymmetricAlgorithm 类的构造器. 
ExpandedSubBlockEnd.gif   
/// </remarks> 

InBlock.gif   public SymmCrypto(SymmProvEnum NetSelected) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif   
switch (NetSelected) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif   
case SymmProvEnum.DES: 
InBlock.gif   mobjCryptoService 
= new DESCryptoServiceProvider(); 
InBlock.gif   
break
InBlock.gif   
case SymmProvEnum.RC2: 
InBlock.gif   mobjCryptoService 
= new RC2CryptoServiceProvider(); 
InBlock.gif   
break
InBlock.gif   
case SymmProvEnum.Rijndael: 
InBlock.gif   mobjCryptoService 
= new RijndaelManaged(); 
InBlock.gif   
break
ExpandedSubBlockEnd.gif   }
 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// <remarks> 
InBlock.gif   
/// 使用自定义SymmetricAlgorithm类的构造器. 
ExpandedSubBlockEnd.gif   
/// </remarks> 

InBlock.gif   public SymmCrypto(SymmetricAlgorithm ServiceProvider) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif   mobjCryptoService 
= ServiceProvider; 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// <remarks> 
InBlock.gif   
/// Depending on the legal key size limitations of 
InBlock.gif   
/// a specific CryptoService provider and length of 
InBlock.gif   
/// the private key provided, padding the secret key 
InBlock.gif   
/// with space character to meet the legal size of the algorithm. 
ExpandedSubBlockEnd.gif   
/// </remarks> 

InBlock.gif   private byte[] GetLegalKey(string Key) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif   
string sTemp; 
InBlock.gif   
if (mobjCryptoService.LegalKeySizes.Length > 0
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif   
int lessSize = 0, moreSize = mobjCryptoService.LegalKeySizes[0].MinSize; 
InBlock.gif   
// key sizes are in bits 
InBlock.gif
   while (Key.Length * 8 > moreSize) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif   lessSize 
= moreSize; 
InBlock.gif   moreSize 
+= mobjCryptoService.LegalKeySizes[0].SkipSize; 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   sTemp 
= Key.PadRight(moreSize / 8' '); 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
else 
InBlock.gif   sTemp 
= Key; 
InBlock.gif   
InBlock.gif   
// convert the secret key to byte array 
InBlock.gif
   return ASCIIEncoding.ASCII.GetBytes(sTemp); 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
InBlock.gif   
public string Encrypting(string Source, string Key) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif   
byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(Source); 
InBlock.gif   
// create a MemoryStream so that the process can be done without I/O files 
InBlock.gif
   System.IO.MemoryStream ms = new System.IO.MemoryStream(); 
InBlock.gif   
InBlock.gif   
byte[] bytKey = GetLegalKey(Key); 
InBlock.gif   
InBlock.gif   
// set the private key 
InBlock.gif
   mobjCryptoService.Key = bytKey; 
InBlock.gif   mobjCryptoService.IV 
= bytKey; 
InBlock.gif   
InBlock.gif   
// create an Encryptor from the Provider Service instance 
InBlock.gif
   ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor(); 
InBlock.gif   
InBlock.gif   
// create Crypto Stream that transforms a stream using the encryption 
InBlock.gif
   CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write); 
InBlock.gif   
InBlock.gif   
// write out encrypted content into MemoryStream 
InBlock.gif
   cs.Write(bytIn, 0, bytIn.Length); 
InBlock.gif   cs.FlushFinalBlock(); 
InBlock.gif   
InBlock.gif   
// get the output and trim the '\0' bytes 
InBlock.gif
   byte[] bytOut = ms.GetBuffer(); 
InBlock.gif   
int i = 0
InBlock.gif   
for (i = 0; i < bytOut.Length; i++
InBlock.gif   
if (bytOut[i] == 0
InBlock.gif   
break
InBlock.gif   
InBlock.gif   
// convert into Base64 so that the result can be used in xml 
InBlock.gif
   return System.Convert.ToBase64String(bytOut, 0, i); 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
InBlock.gif   
public string Decrypting(string Source, string Key) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif   
// convert from Base64 to binary 
InBlock.gif
   byte[] bytIn = System.Convert.FromBase64String(Source); 
InBlock.gif   
// create a MemoryStream with the input 
InBlock.gif
   System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length); 
InBlock.gif   
InBlock.gif   
byte[] bytKey = GetLegalKey(Key); 
InBlock.gif   
InBlock.gif   
// set the private key 
InBlock.gif
   mobjCryptoService.Key = bytKey; 
InBlock.gif   mobjCryptoService.IV 
= bytKey; 
InBlock.gif   
InBlock.gif   
// create a Decryptor from the Provider Service instance 
InBlock.gif
   ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor(); 
InBlock.gif   
InBlock.gif   
// create Crypto Stream that transforms a stream using the decryption 
InBlock.gif
   CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read); 
InBlock.gif   
InBlock.gif   
// read out the result from the Crypto Stream 
InBlock.gif
   System.IO.StreamReader sr = new System.IO.StreamReader( cs ); 
InBlock.gif   
return sr.ReadToEnd(); 
ExpandedSubBlockEnd.gif   }
 
ExpandedSubBlockEnd.gif   }
 
ExpandedBlockEnd.gif  }
  
None.gif

Trackback: http://www.cnsdn.com.cn/blog/trackback.asp?tbID=1724
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值