.NET中的加密解密:私钥加密(对称加密):AES、DES、RC2、Rijindael、TripleDES
2009-04-12 14:15
417 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; using System.IO; namespace Sample_CUI { namespace Encryption_And_Decryption_PrivateKey { public class SampleTest { public static void Test() { byte[] bKey8 = UTF8Encoding.UTF8.GetBytes("12345678"); byte[] bKey16 = UTF8Encoding.UTF8.GetBytes("1234567890123456"); byte[] bKey32 = UTF8Encoding.UTF8.GetBytes("12345678901234567890123456789012"); string data = "Hello,I am Jie."; Console.WriteLine("Sample[Encrtption And Descryption] Original Data:/"{0}/"", data); Console.WriteLine(); Console.WriteLine("Aes Sample"); AesSample aes = new AesSample(); string aesData = ""; aesData = aes.Encrypt(data, bKey32); Console.WriteLine("AES Encrypt:{0}", aesData); Console.WriteLine("AES Decrypt:{0}", aes.Decrypt(aesData, bKey32)); Console.WriteLine(); Console.WriteLine("Des Sample"); DesSample des = new DesSample(); string desData = ""; desData = des.Encrypt(data, bKey8); Console.WriteLine("DES Encrypt:{0}", desData); Console.WriteLine("DES Decrypt:{0}", des.Decrypt(desData, bKey8)); Console.WriteLine(); Console.WriteLine("RC2 Sample"); Rc2Sample rc2 = new Rc2Sample(); string rc2Data = ""; rc2Data = rc2.Encrypt(data, bKey8); Console.WriteLine("RC2 Encrypt:{0}", rc2Data); Console.WriteLine("RC2 Decrypt:{0}", rc2.Decrypt(rc2Data, bKey8)); Console.WriteLine(); Console.WriteLine("Rijndael Sample"); RijndaelSample rijndael = new RijndaelSample(); string rijndaelData = ""; rijndaelData = rijndael.Encrypt(data, bKey32); Console.WriteLine("Rijndael Encrypt:{0}", rijndaelData); Console.WriteLine("Rijndael Decrypt:{0}", rijndael.Decrypt(rijndaelData, bKey32)); Console.WriteLine(); Console.WriteLine("TripleDES Sample"); TripleDESSample tripleDES = new TripleDESSample(); string tripleDESData = ""; tripleDESData = tripleDES.Encrypt(data, bKey16); Console.WriteLine("TripleDES Encrypt:{0}", tripleDESData); Console.WriteLine("TripleDES Decrypt:{0}", tripleDES.Decrypt(tripleDESData, bKey16)); Console.WriteLine("{0}{0}", "--------------------"); } } public class AesSample { Rijndael aes = null; public AesSample() { aes = Rijndael.Create(); } public string Encrypt(string data, byte[] bKey) { try { byte[] bData = Encoding.UTF8.GetBytes(data); aes.Key = bKey; aes.Mode = CipherMode.ECB; aes.Padding = PaddingMode.PKCS7; ICryptoTransform iCryptoTransform = aes.CreateEncryptor(); byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length); return Convert.ToBase64String(bResult); } catch { throw; } } public string Decrypt(string data, byte[] bKey) { try { byte[] bData = Convert.FromBase64String(data); aes.Key = bKey; aes.Mode = CipherMode.ECB; aes.Padding = PaddingMode.PKCS7; ICryptoTransform iCryptoTransform = aes.CreateDecryptor(); byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length); return Encoding.UTF8.GetString(bResult); } catch { throw; } } }//class AesSample public class DesSample { DES des = null; public DesSample() { des = new DESCryptoServiceProvider(); } public string Encrypt(string data, byte[] bKey) { try { byte[] bData = Encoding.UTF8.GetBytes(data); des.Key = bKey; ICryptoTransform iCryptoTransform = des.CreateEncryptor(); byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length); return Convert.ToBase64String(bResult); } catch { throw; } } public string Decrypt(string data, byte[] bKey) { try { byte[] bData = Convert.FromBase64String(data); des.Key = bKey; ICryptoTransform iCryptoTransform = des.CreateDecryptor(); byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length); return Encoding.UTF8.GetString(bResult); } catch { throw; } } }//class DesSample public class Rc2Sample { RC2 rc2 = null; public Rc2Sample() { rc2 = new RC2CryptoServiceProvider(); } public string Encrypt(string data, byte[] bKey) { try { byte[] bData = Encoding.UTF8.GetBytes(data); rc2.Key = bKey; ICryptoTransform iCryptoTransform = rc2.CreateEncryptor(); byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length); return Convert.ToBase64String(bResult); } catch { throw; } } public string Decrypt(string data, byte[] bKey) { try { byte[] bData = Convert.FromBase64String(data); rc2.Key = bKey; ICryptoTransform iCryptoTransform = rc2.CreateDecryptor(); byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length); return Encoding.UTF8.GetString(bResult); } catch { throw; } } } public class RijndaelSample { Rijndael rijndael = null; public RijndaelSample() { rijndael = new RijndaelManaged(); } public string Encrypt(string data, byte[] bKey) { try { byte[] bData = Encoding.UTF8.GetBytes(data); rijndael.Key = bKey; ICryptoTransform iCryptoTransform = rijndael.CreateEncryptor(); byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length); return Convert.ToBase64String(bResult); } catch { throw; } } public string Decrypt(string data, byte[] bKey) { try { byte[] bData = Convert.FromBase64String(data); rijndael.Key = bKey; ICryptoTransform iCryptoTransform = rijndael.CreateDecryptor(); byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length); return Encoding.UTF8.GetString(bResult); } catch { throw; } } } public class TripleDESSample { TripleDES tDes = null; public TripleDESSample() { tDes = new TripleDESCryptoServiceProvider(); } public string Encrypt(string data, byte[] bKey) { try { byte[] bData = Encoding.UTF8.GetBytes(data); tDes.Key = bKey; ICryptoTransform iCryptoTransform = tDes.CreateEncryptor(); byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length); return Convert.ToBase64String(bResult); } catch { throw; } } public string Decrypt(string data, byte[] bKey) { try { byte[] bData = Convert.FromBase64String(data); tDes.Key = bKey; ICryptoTransform iCryptoTransform = tDes.CreateDecryptor(); byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length); return Encoding.UTF8.GetString(bResult); } catch { throw; } } } } }
相关文章推荐
- .NET中的加密解密:私钥加密(对称加密):AES、DES、RC2、Rijindael、TripleDES
- .Net(c#)加密解密之Aes和Des
- .NET中的加密解密:私钥加密(对称加密):DES
- java实现对称加密AES和DES的加密、解密
- 常用加密解密算法【RSA、AES、DES、MD5】介绍和使用
- php 的des 加密解密 des 可以更改为tripledes等
- Java和.net加密解密互通DES
- RSA加解密使用总结,.net私钥加密公钥解密,WinCE平台RSA加解密
- C#中使用DES和AES加密解密
- C#中使用DES和AES加密解密
- actionscript DES 加密 解密 (AES,其他加密)
- NET实现RSA AES DES 字符串 加密解密以及SHA1 MD5加密
- C#中使用DES和AES加密解密
- .NET 与 Java DES 加密/解密 互通 方法
- Des 加密解密 .Net-Flex
- DES、AES加密解密的方法
- ios常见加密解密方法(RSA、DES 、AES、MD5)
- C#中使用DES和AES加密解密
- 对称加密中DES的加密与解密算法
- C# RSA加密、解密、加签、验签、支持JAVA格式公钥私钥、PEM格式公钥私钥、.NET格式公钥私钥、一般模式【支持公钥加密,私钥解密】(一)