该文章将会不断的收入.net下的加密算法,这些算法将会采用策略模式来统一管理。
using System; using System.Collections.Generic; using System.Text; namespace EncryptAndDecrypt { public class EncryptAndDecrytHandler { IEncrytAndDecrypt _EncryptAndDecrypt; public EncryptAndDecrytHandler(IEncrytAndDecrypt iencrytanddecrypt) { _EncryptAndDecrypt = iencrytanddecrypt; } public string Encrypt(string data) { return _EncryptAndDecrypt.Encrypt(data); } public string Decrypt(string data) { return _EncryptAndDecrypt.Decrypt(data); } } }
加密解密的调用者
using System; using System.Collections.Generic; using System.Text; namespace EncryptAndDecrypt { public interface IEncrytAndDecrypt { string Encrypt(string data); string Decrypt(string data); } }
策略接口
using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptography; using System.IO; namespace EncryptAndDecrypt { public class DESCrypt:IEncrytAndDecrypt { private byte[] rgbKey = new byte[] { 1, 9, 8, 7, 0, 1, 0, 3 }; private byte[] rgbIv = new byte[] { 1, 9, 8, 7, 0, 1, 0, 3 }; #region IEncrytAndDecrypt 成员 public string Encrypt(string data) { MemoryStream mstream = new MemoryStream(); DESCryptoServiceProvider deskey = new DESCryptoServiceProvider(); CryptoStream cstream = new CryptoStream(mstream, deskey.CreateEncryptor(rgbKey,rgbIv), CryptoStreamMode.Write); StreamWriter sw = new StreamWriter(cstream); sw.WriteLine(data); sw.Close(); cstream.Close(); byte[] bytes = mstream.ToArray(); mstream.Close(); return Convert.ToBase64String(bytes); } public string Decrypt(string data) { string tmp; MemoryStream mstream = new MemoryStream(Convert.FromBase64String(data)); DESCryptoServiceProvider deskey = new DESCryptoServiceProvider(); CryptoStream cstream = new CryptoStream(mstream, deskey.CreateDecryptor(rgbKey, rgbIv), CryptoStreamMode.Read); StreamReader sr = new StreamReader(cstream); tmp = sr.ReadLine(); sr.Close(); cstream.Close(); mstream.Close(); return tmp; } #endregion } }
DES加密算法
using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptography; using System.IO; namespace EncryptAndDecrypt { public class TripleDesCrypt:IEncrytAndDecrypt { private byte[] rgbKey; private byte[] rgbIv; public TripleDesCrypt() { rgbKey = new byte[130]; rgbIv = new byte[130]; for (int i = 0; i < 130; i++) { rgbKey[i] = rgbIv[i] = (byte)(new Random().Next(1, 255)); } } #region IEncrytAndDecrypt 成员 public string Encrypt(string data) { TripleDESCryptoServiceProvider tripleDes=new TripleDESCryptoServiceProvider(); MemoryStream mstream = new MemoryStream(); CryptoStream cstream = new CryptoStream(mstream, tripleDes.CreateEncryptor(rgbKey, rgbIv), CryptoStreamMode.Write); StreamWriter sw = new StreamWriter(cstream); sw.WriteLine(data); cstream.Close(); byte[] result = mstream.ToArray(); mstream.Close(); return Convert.ToBase64String(result); } public string Decrypt(string data) { TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider(); MemoryStream mstream = new MemoryStream(Convert.FromBase64String(data)); CryptoStream cstream = new CryptoStream(mstream, tripleDes.CreateDecryptor(rgbKey, rgbIv), CryptoStreamMode.Read); StreamReader sr = new StreamReader(cstream); string result = sr.ReadLine(); sr.Close(); cstream.Close(); mstream.Close(); return result; } #endregion } }
TripleDES加密算法
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索string
, new
, using
, system
, public
, string算法
, MemoryStream
TripleDES
c 加密解密算法、java加密解密算法、java aes加密解密算法、加密解密算法、字符串加密解密算法,以便于您获取更多的相关知识。
时间: 2024-10-18 23:04:34