C# 实现3Des加密 解密 - SummerA
2014-01-06 20:14
337 查看
1 public class _3DESEncrypt 2 { 3 4 public static string Encrypt3DES(string a_strString, string a_strKey) 5 { 6 TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider(); 7 DES.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(a_strKey, "md5").Substring(0, 24)); 8 DES.Mode = CipherMode.ECB; 9 ICryptoTransform DESEncrypt = DES.CreateEncryptor(); 10 byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(a_strString); 11 return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)); 12 } 13 14 public static string Decrypt3DES(string a_strString, string a_strKey) 15 { 16 TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider(); 17 DES.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(a_strKey, "md5").Substring(0, 24)); 18 DES.Mode = CipherMode.ECB; 19 DES.Padding = System.Security.Cryptography.PaddingMode.PKCS7; 20 ICryptoTransform DESDecrypt = DES.CreateDecryptor(); 21 string result = ""; 22 try 23 { 24 byte[] Buffer = Convert.FromBase64String(a_strString); 25 26 result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)); 27 28 //MemoryStream msDecrypt = new MemoryStream(Buffer); 29 //CryptoStream csDecrypt = new CryptoStream(msDecrypt, 30 // DES.CreateDecryptor(DES.Key, DES.IV), 31 // CryptoStreamMode.Read); 32 33 //// Create buffer to hold the decrypted data. 34 //byte[] fromEncrypt = new byte[Buffer.Length]; 35 36 //// Read the decrypted data out of the crypto stream 37 //// and place it into the temporary buffer. 38 //csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length); 39 //result = System.Text.Encoding.Default.GetString(fromEncrypt); 40 } 41 catch (Exception e) 42 { 43 } 44 return result; 45 46 } 47 }
相关文章推荐
- C#实现简单的3DES加密解密功能示例
- C#实现3DES加密24位密钥
- C#实现RSA加密解密
- C#的3DES加密/解密
- C#实现的AES加密解密完整实例
- JAVA和C# 3DES加密解密
- C# 下base64加密解密实现
- C#实现AES加密和解密函数
- C# 程序简单实现加密解密程序代码
- C#实现3DES加密
- 使用C#实现AES加密解密
- JAVA和C# 3DES加密解密
- C#中可以与java进行互相转换的3des的加密和解密
- C#使用DES和AES实现加密解密功能示例
- C#实现DES加密解密,AES加密解密
- [Delphi] XE2下实现标准的3DES加解密一种方法(可与Java,c#互通)
- C# 加密解密(DES,3DES,MD5,Base64) 类
- JAVA和C# 3DES加密解密
- 不对称加密和解密,用C#实现