C#DES暗号解読の使い方

15949 ワード

主にC#で使用される3つのクラス:MemoryStreamメモリストリームDESCryptoServiceProvider暗号化サービスプロバイダクラスCryptoStreamデータストリームが暗号化変換されたストリームに接続されている
以下は簡易コードです.テスト済みです.
 1     public static string strKey = "abcdefgh";
 2     public static string strIV = "ijklmnop";
 3     private XmlDocument XmlDoc = null;
 4     private XmlElement XmlRoot = null;
 5 
 6     //  
 7     public static string Encrypt(string _strQ)
 8     {
 9         byte[] buffer = Encoding.UTF8.GetBytes(_strQ);
10         MemoryStream ms = new MemoryStream();
11         DESCryptoServiceProvider tdes = new DESCryptoServiceProvider();
12         CryptoStream encStream = new CryptoStream(ms, tdes.CreateEncryptor(Encoding.UTF8.GetBytes(strKey), Encoding.UTF8.GetBytes(strIV)), CryptoStreamMode.Write);
13         encStream.Write(buffer, 0, buffer.Length);
14         encStream.FlushFinalBlock();
15         return Convert.ToBase64String(ms.ToArray()).Replace("+", "%");
16     }
17 
18     //  
19     public static string Decrypt(string _strQ)
20     {
21         _strQ = _strQ.Replace("%", "+");
22         byte[] buffer = Convert.FromBase64String(_strQ);
23         MemoryStream ms = new MemoryStream();
24         DESCryptoServiceProvider tdes = new DESCryptoServiceProvider();
25         CryptoStream encStream = new CryptoStream(ms, tdes.CreateDecryptor(Encoding.UTF8.GetBytes(strKey), Encoding.UTF8.GetBytes(strIV)), CryptoStreamMode.Write);
26         encStream.Write(buffer, 0, buffer.Length);
27         encStream.FlushFinalBlock();
28         return Encoding.UTF8.GetString(ms.ToArray());
29     }
30 
31     //  
32     public void XmlLoadDecrypt(XmlDocument xmlDoc, string fileName)
33     {
34         FileStream fileStream = new FileStream(fileName, FileMode.Open);
35         byte[] bsXml = new byte[fileStream.Length];
36         fileStream.Read(bsXml, 0, bsXml.Length);
37         fileStream.Close();
38 
39         MemoryStream ms = new MemoryStream();
40         DESCryptoServiceProvider tdes = new DESCryptoServiceProvider();
41         CryptoStream encStream = new CryptoStream(ms, tdes.CreateDecryptor(Encoding.UTF8.GetBytes(strKey), Encoding.UTF8.GetBytes(strIV)), CryptoStreamMode.Write);
42         encStream.Write(bsXml, 0, bsXml.Length);
43         encStream.FlushFinalBlock();
44 
45         xmlDoc.Load(new MemoryStream(ms.ToArray()));
46     }
47 
48     //  
49     public void XmlSaveEncrypt(XmlDocument xmlDoc, string fileName)
50     {
51         if (!File.Exists(fileName))
52             File.Create(fileName).Close();
53 
54         FileStream fileStream = new FileStream(fileName, FileMode.Truncate);
55         MemoryStream msXml = new MemoryStream();
56         xmlDoc.Save(msXml);
57 
58         DESCryptoServiceProvider tdes = new DESCryptoServiceProvider();
59         CryptoStream cs = new CryptoStream(fileStream, tdes.CreateEncryptor(Encoding.UTF8.GetBytes(strKey), Encoding.UTF8.GetBytes(strIV)), CryptoStreamMode.Write);
60         cs.Write(msXml.ToArray(), 0, msXml.ToArray().Length);
61         cs.FlushFinalBlock();
62 
63         msXml.Close();
64         fileStream.Close();
65     }
66 
67     //  
68     string str1 = "abcdefghijklmnopqrstuvwxyz";
69     if (GUI.Button(new Rect(0, 300, 100, 50), "Encrypt"))
70     {
71         Debug.Log(Encrypt(str1));
72     }
73     if (GUI.Button(new Rect(0, 350, 100, 50), "Decrypt"))
74     {
75         Debug.Log(Decrypt(Encrypt(str1)));
76     }
77     if (GUI.Button(new Rect(0, 400, 100, 50), "Load Decrypt Xml"))
78     {
79         XmlDoc = new XmlDocument();
80         XmlLoadDecrypt(XmlDoc, "Assets/123.xml");
81         XmlRoot = XmlDoc.DocumentElement;
82         Debug.Log(XmlRoot.GetAttribute("Test"));
83     }
84     if (GUI.Button(new Rect(0, 450, 100, 50), "Save Encrypt Xml"))
85     {
86         XmlDoc = new XmlDocument();
87         XmlRoot = XmlDoc.CreateElement("RegisterRecords");
88         XmlDoc.AppendChild(XmlRoot);
89         XmlRoot.SetAttribute("Test", "something");
90         XmlSaveEncrypt(XmlDoc, "Assets/123.xml");
91     }