您好,登錄后才能下訂單哦!
這篇文章主要介紹C#設計模式之Strategy策略模式如何解決007大破密碼危機問題,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
具體如下:
一、理論定義
策略模式 定義了 多套算法,這些算法在 客戶端 可以任意切換。
二、應用舉例
需求描述:話說007在皇家賭場賭牌,突然接到M夫人的急電,要求立刻去非洲 尋找一個DES對稱算法密鑰,以破解敵人的軍*情*機*密
1、(英*國*軍*情*六*局)MI6=Military Intelligence 6 截獲了 一個非*洲戰*區軍*事*機*密文件,采用 MD5,RAS,加密,解密,都無法破解
后來發現,這文件被DES加密, 必須找回對稱密鑰,才可以破解
2、邦德 火速趕往 非洲,目標只有一個:找到密鑰。
三、具體編碼
1.定義安全 算法接口,里面有加密和解密方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Com.Design.Gof.Strategy { public interface ISecurity { /// <summary> /// 加密 /// </summary> /// <param name="EncryptString">要加密字符串</param> /// <returns></returns> string Encrypt(string EncryptString); /// <summary> /// 解密 /// </summary> /// <param name="EncryptString">要解密字符串</param> /// <returns></returns> string Decrypt(string EncryptString); } }
2.MD5加密
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Security.Cryptography; namespace Com.Design.Gof.Strategy { public class MD5 : ISecurity { /// <summary> /// 用MD5加密 /// </summary> /// <param name="s"></param> /// <returns></returns> public string Encrypt(string s) { byte[] b = Encoding.Default.GetBytes(s); b = new MD5CryptoServiceProvider().ComputeHash(b); string output = ""; for (int i = 0; i < b.Length; i++) output += b[i].ToString("x").PadLeft(2, '0'); return output; } /// <summary> /// MD5不提供解密 /// </summary> /// <param name="EncryptString"></param> /// <returns></returns> public virtual string Decrypt(string EncryptString) { return string.Empty; } } }
3.RSA加密
using System; using System.Text; using System.IO; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; namespace Com.Design.Gof.Strategy { public class RSA:ISecurity { private static readonly string key=new RSACryptoServiceProvider().ToXmlString(true); /// <summary> /// RSA加密函數 /// </summary> /// <param name="xmlPublicKey">說明:KEY必須是XML的行式,返回的是字符串</param> /// <param name="EncryptString"></param> /// <returns></returns> public string Encrypt(string s) { try { byte[] PlainTextBArray; byte[] CypherTextBArray; string Result; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(key); PlainTextBArray = (new UnicodeEncoding()).GetBytes(s); CypherTextBArray = rsa.Encrypt(PlainTextBArray, false); Result = Convert.ToBase64String(CypherTextBArray); return Result; } catch { return "敵人密碼太難破解,已經超過了RSA算法的承受能力,要采取分段加密"; } } /// <summary> /// RSA解密函數 /// </summary> /// <param name="xmlPrivateKey"></param> /// <param name="DecryptString"></param> /// <returns></returns> public string Decrypt(string s) { try { byte[] PlainTextBArray; byte[] DypherTextBArray; string Result; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(key); PlainTextBArray = Convert.FromBase64String(s); DypherTextBArray = rsa.Decrypt(PlainTextBArray, false); Result = (new UnicodeEncoding()).GetString(DypherTextBArray); return Result; } catch { return "敵人密碼太難破解,已經超過了RSA算法的承受能力,要采取分段解密"; } } } }
4.DES加密
using System; using System.Text; using System.IO; using System.Security.Cryptography; /// <summary> ///MethodResult 的摘要說明 /// </summary> namespace Com.Design.Gof.Strategy { public class DES:ISecurity { private static byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; //密鑰向量 //加密解密key public string SymmetricKey { get; set; } /// <summary> /// 加密 /// </summary> /// <param name="EncryptString">待加密的字符串</param> /// <param name="EncryptKey">加密密鑰</param> /// <returns>加密成功返回加密后的字符串,失敗返回源串</returns> public string Encrypt(string EncryptString) { byte[] byKey = null; byKey = System.Text.Encoding.UTF8.GetBytes(SymmetricKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = System.Text.Encoding.UTF8.GetBytes(EncryptString); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Convert.ToBase64String(ms.ToArray()); } /// <summary> /// 解密 /// </summary> /// <param name="EncryptString">待解密的字符串</param> /// <returns>解密成功返回解密后的字符串,失敗返源串</returns> public string Decrypt(string EncryptString) { byte[] byKey = null; byte[] inputByteArray = new Byte[EncryptString.Length]; try { byKey = System.Text.Encoding.UTF8.GetBytes(SymmetricKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(EncryptString); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = new System.Text.UTF8Encoding(); return encoding.GetString(ms.ToArray()); } catch { return ""; } } } }
5.(英*國*軍*情*六*局)MI6=Military Intelligence 6
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Com.Design.Gof.Strategy { /// <summary> /// (英*國*軍*情*六*局)MI6=Military Intelligence 6 /// </summary> public class MilitaryIntelligence6 {/// <summary> /// 安全策略 /// </summary> private ISecurity Security { get; set; } /// <summary> /// 被加密的軍*情*信*息 /// </summary> public string ClassifiedInfomation { get; set; } /// <summary> /// 加密 /// </summary> /// <param name="s"></param> /// <returns></returns> public string Encrypt() { return Security.Encrypt(ClassifiedInfomation); } /// <summary> /// 解密 /// </summary> /// <param name="s"></param> /// <returns></returns> public string Decrypt(string s) { return Security.Decrypt(s); } } }
6.主函數
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Com.Design.Gof.Strategy; namespace Com.Design.Gof.Test { class Program { /// <summary> /// 往期 設計模式測試項目是一起的,想測試誰,就調用誰 /// </summary> /// <param name="args"></param> static void Main(string[] args) { MilitaryIntelligence6 MI6= new MilitaryIntelligence6 { //默認密碼策略 MD5 Security = new MD5(), //被加密的 軍*情*信*息 ClassifiedInfomation = @"+30/SxY2HZ0UtqUVNGMsad0zfajsHQmja1NVC+639zC6y0dE/8XDZJefMl0NwBJ+sUA8LC8k/IPEeTtFqW6OWaaZH9A+TNWzrJ6MSV2qiM3px6wFAyDkJsMKex0mJNe5", }; //用 MD5 破解 string result_md5 = MI6.Encrypt(); Console.WriteLine("用MD5破解敵*人機密文件:" + result_md5); Console.WriteLine("MD5加密后,還是一團亂麻,機密文件無法破解"); Console.WriteLine(); //用 RSA 破解 MI6.Security = new RSA(); string Result_RSA = MI6.Encrypt(); Console.WriteLine(Result_RSA); //用 DES 破解 string symmetricKey = "AfricaArea";//007完成使命,拿到了密鑰 MI6.Security = new DES { SymmetricKey = symmetricKey }; //解密后的內容應該是:軍-情-機-密-信-息:我軍將要攻打 非*洲,戰區指揮官:隆美爾,坦克:500輛,飛機:2000架 Console.WriteLine(); Console.WriteLine("007獲取到了DES解密密碼,打開了 軍-事-機-密文件,內容如下:" + MI6.Decrypt(MI6.ClassifiedInfomation)); Console.ReadKey(); } } }
7.運行結果
以上是“C#設計模式之Strategy策略模式如何解決007大破密碼危機問題”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。