基于.NET的加密/解密算法

原创 2004年08月30日 20:02:00

提供一个基于.NET SymmetricAlgorithm 类的、带私钥的加密/解密算法的包装类。使用方法:

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 { /// /// SymmCrypto 的摘要说明。 /// SymmCrypto类实现.NET框架下的加密和解密服务。 /// 原作者: Frank Fang : fangfrank@hotmail.com /// public class SymmCrypto { public enum SymmProvEnum : int { DES, RC2, Rijndael } private SymmetricAlgorithm mobjCryptoService; /// /// 使用.Net SymmetricAlgorithm 类的构造器. /// 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; } } /// /// 使用自定义SymmetricAlgorithm类的构造器. /// public SymmCrypto(SymmetricAlgorithm ServiceProvider) { mobjCryptoService = ServiceProvider; } /// /// 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. /// 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 xml 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(); } } }

提供一个基于.NET的加密/解密算法(对称加密)

提供一个基于.NET SymmetricAlgorithm 类的、带私钥的加密/解密算法的包装类。使用方法: SymmCrypto de = new SymmCrypto(SymmCrypto.Sy...
  • jaylongli
  • jaylongli
  • 2010-12-03 15:34:00
  • 1617

基于.net的RSA私钥加密 公钥解密的源码

  • 2013年04月28日 19:45
  • 102KB
  • 下载

.net加密解密学习总结

.NET中的DES加密解密 http://blog.csdn.net/pan_junbiao/article/details/7032700   System.Security.Cryptograp...
  • bcbobo21cn
  • bcbobo21cn
  • 2016-06-21 19:11:49
  • 1257

.NET使用RSA加密解密

知耻近乎勇。——孔丘PassWordHelper.cs代码:using System; using System.IO; using System.Text; using System.Globali...
  • WuLex
  • WuLex
  • 2016-12-24 10:13:15
  • 3923

JS加密解密对于asp.net解密加密

对于特殊数据传输,我们需要做到加密以及后台解密等!  一:js加密、asp.net解密        js:encodeURI(需要加密的变量)        asp.net:Server.Ur...
  • jiang199244
  • jiang199244
  • 2014-02-27 10:28:01
  • 2885

.net使用des加密算法

///         /// DES加密         ///         ///         ///         public String desEncode(String...
  • Zhangchen9091
  • Zhangchen9091
  • 2015-05-30 21:26:54
  • 525

Java和C#中3DES的加密与解密

最近在工作中遇到将Java环境的一个用户ID用表单形式传递给.net环境做校验,考虑到不能将用户ID作为明文传递,因此利用简单的3DES进行加密解密操作。 需要注意的就是: (1)两种环境下3DE...
  • buaa_shang
  • buaa_shang
  • 2015-12-10 11:04:19
  • 2556

AES加密解密(互通.NET和JAVA)

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Te...
  • q269399361
  • q269399361
  • 2014-06-20 17:21:49
  • 2034

.net的RSA私钥加密 公钥解密的源码

  • 2017年11月02日 11:42
  • 107KB
  • 下载

C#加密方法汇总(SHA1加密字符串,MD5加密字符串,可逆加密等)

方法一: //须添加对System.Web的引用 using System.Web.Security;  /// /// SHA1加密字符串 /// /// 源字符串 /...
  • cheungbill
  • cheungbill
  • 2012-06-29 13:00:34
  • 10139
收藏助手
不良信息举报
您举报文章:基于.NET的加密/解密算法
举报原因:
原因补充:

(最多只允许输入30个字)