提供一个基于C 的加密/解密算法
2008-02-23 07:14:35来源:互联网 阅读 ()
SymmCrypto de = new SymmCrypto(SymmCrypto.SymmProvEnum.DES); Response.Write(x.Decrypting(de.Encrypting("ok","yyy"),"yyy"));
类的实现C#编码
using System; using System.Security.Cryptography; using System.IO; using System.Text; namespace eMeng { /// <summary> /// SymmCrypto 的摘要说明。 /// SymmCrypto类实现.NET框架下的加密和解密服务。 /// 原作者: Frank Fang : fangfrank@hotmail.com /// </summary> public class SymmCrypto { public enum SymmProvEnum : int { DES, RC2, Rijndael } private SymmetricAlgorithm mobjCryptoService; /// <remarks> /// 使用.Net SymmetricAlgorithm 类的构造器. /// </remarks> public SymmCrypto(SymmProvEnum NetSelected) { switch (NetSelected) { case SymmProvEnum.DES: mobjCryptoService = new DESCryptoServiceProvider(); break; case SymmProvEnum.RC2: mobjCryptoService = new RC2CryptoServiceProvider(); break; case SymmProvEnum.Rijndael: mobjCryptoService = new RijndaelManaged(); break; } } /// <remarks> /// 使用自定义SymmetricAlgorithm类的构造器. /// </remarks> public SymmCrypto(SymmetricAlgorithm ServiceProvider) { mobjCryptoService = ServiceProvider; } /// <remarks> /// Depending on the legal key size limitations of /// a specific CryptoService provider and length of /// the private key provided, padding the secret key /// with space character to meet the legal size of the algorithm. /// </remarks> private byte[] GetLegalKey(string Key) { string sTemp; if (mobjCryptoService.LegalKeySizes.Length > 0) { int lessSize = 0, moreSize = mobjCryptoService.LegalKeySizes[0].MinSize; // key sizes are in bits while (Key.Length * 8 > moreSize) { lessSize = moreSize; moreSize = mobjCryptoService.LegalKeySizes[0].SkipSize; } sTemp = Key.PadRight(moreSize / 8, ' '); } else sTemp = Key; // convert the secret key to byte array return ASCIIEncoding.ASCII.GetBytes(sTemp); } public string Encrypting(string Source, string Key) { byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(Source); // create a MemoryStream so that the process can be done without I/O files System.IO.MemoryStream ms = new System.IO.MemoryStream(); byte[] bytKey = GetLegalKey(Key); // set the private key mobjCryptoService.Key = bytKey; mobjCryptoService.IV = bytKey; // create an Encryptor from the Provider Service instance ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor(); // create Crypto Stream that transforms a stream using the encryption CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write); // write out encrypted content into MemoryStream cs.Write(bytIn, 0, bytIn.Length); cs.FlushFinalBlock(); // get the output and trim the '\0' bytes byte[] bytOut = ms.GetBuffer(); int i = 0; for (i = 0; i < bytOut.Length; i ) if (bytOut[i] == 0) break; // convert into Base64 so that the result can be used in <a href="http://dev.21tx.com/dotnet/XML/" target="_blank">XML</a> return System.Convert.ToBase64String(bytOut, 0, i); } public string Decrypting(string Source, string Key) { // convert from Base64 to binary byte[] bytIn = System.Convert.FromBase64String(Source); // create a MemoryStream with the input System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length); byte[] bytKey = GetLegalKey(Key); // set the private key mobjCryptoService.Key = bytKey; mobjCryptoService.IV = bytKey; // create a Decryptor from the Provider Service instance ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor(); // create Crypto Stream that transforms a stream using the decryption CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read); // read out the result from the Crypto Stream System.IO.StreamReader sr = new System.IO.StreamReader( cs ); return sr.ReadToEnd(); } } }
(T127)
上一篇: 用Delphi编程实现清空回收站功能
下一篇: Delphi 编程技巧常见实例问答汇集
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash