[总结]DotNet中用到的加密算法总结

本文介绍了一种使用C#实现DES和3DES加密解密的方法,包括了不同模式下的加密过程,如ECB和CBC,并且演示了如何结合Base64和HEX进行数据转换。
1None.gifpublic class CryptUtil
  2ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
  3InBlock.gif        public static string DecryptString(string input)
  4ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
  5InBlock.gif            if (input.Equals(string.Empty))
  6ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
  7InBlock.gif                return input;
  8ExpandedSubBlockEnd.gif            }

  9InBlock.gif
 10ExpandedSubBlockStart.gifContractedSubBlock.gif            byte[] byKey = dot.gif{0x630x680x650x6E0x790x750x610x6E};
 11ExpandedSubBlockStart.gifContractedSubBlock.gif            byte[] IV = dot.gif{0xFE0xDC0xBA0x980x760x540x320x10};
 12InBlock.gif            byte[] inputByteArray = new Byte[input.Length];
 13InBlock.gif            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
 14InBlock.gif            inputByteArray = Convert.FromBase64String(input);
 15InBlock.gif            MemoryStream ms = new MemoryStream();
 16InBlock.gif            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
 17InBlock.gif            cs.Write(inputByteArray, 0, inputByteArray.Length);
 18InBlock.gif            cs.FlushFinalBlock();
 19InBlock.gif            Encoding encoding = new UTF8Encoding();
 20InBlock.gif            return encoding.GetString(ms.ToArray());
 21ExpandedSubBlockEnd.gif        }

 22InBlock.gif
 23InBlock.gif        public static string EncryptString(string input)
 24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 25InBlock.gif            if (input.Equals(string.Empty))
 26ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 27InBlock.gif                return input;
 28ExpandedSubBlockEnd.gif            }

 29InBlock.gif
 30ExpandedSubBlockStart.gifContractedSubBlock.gif            byte[] byKey = dot.gif{0x630x680x650x6E0x790x750x610x6E};
 31ExpandedSubBlockStart.gifContractedSubBlock.gif            byte[] IV = dot.gif{0xFE0xDC0xBA0x980x760x540x320x10};
 32InBlock.gif            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
 33InBlock.gif            byte[] inputByteArray = Encoding.UTF8.GetBytes(input);
 34InBlock.gif            MemoryStream ms = new MemoryStream();
 35InBlock.gif            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
 36InBlock.gif            cs.Write(inputByteArray, 0, inputByteArray.Length);
 37InBlock.gif            cs.FlushFinalBlock();
 38InBlock.gif            return Convert.ToBase64String(ms.ToArray());
 39ExpandedSubBlockEnd.gif        }

 40ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 41InBlock.gif        /// DES + Base64 加密
 42InBlock.gif        /// </summary>
 43InBlock.gif        /// <param name="input">明文字符串</param>
 44ExpandedSubBlockEnd.gif        /// <returns>已加密字符串</returns>

 45InBlock.gif        public static string DesBase64Encrypt(string input)
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 47InBlock.gif            System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();
 48InBlock.gif            des.Mode = System.Security.Cryptography.CipherMode.ECB;
 49InBlock.gif            ICryptoTransform ct;
 50InBlock.gif            MemoryStream ms;
 51InBlock.gif            CryptoStream cs;
 52InBlock.gif            byte[] byt;
 53ExpandedSubBlockStart.gifContractedSubBlock.gif            byte[] Key = new byte[8]dot.gif{56,50,55,56,56,55,49,49}
 54ExpandedSubBlockStart.gifContractedSubBlock.gif            byte[] IV = new byte[8]dot.gif{0,0,0,0,0,0,0,0};
 55InBlock.gif
 56InBlock.gif            ct = des.CreateEncryptor(Key, IV);
 57InBlock.gif
 58InBlock.gif            byt = Encoding.GetEncoding("GB2312").GetBytes(input); //根据 GB2312 编码对字符串处理,转换成 byte 数组
 59InBlock.gif            
 60InBlock.gif            ms = new MemoryStream();
 61InBlock.gif            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
 62InBlock.gif            cs.Write(byt, 0, byt.Length);
 63InBlock.gif            cs.FlushFinalBlock();
 64InBlock.gif
 65InBlock.gif            cs.Close();
 66InBlock.gif
 67InBlock.gif            byte[] answer = ms.ToArray();
 68InBlock.gif            for(int j=0;j<answer.Length;j++)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 70InBlock.gif                Console.Write(answer[j].ToString()+ " ");
 71ExpandedSubBlockEnd.gif            }

 72InBlock.gif            Console.WriteLine();
 73InBlock.gif            return Convert.ToBase64String(ms.ToArray()); // 将加密的 byte 数组依照 Base64 编码转换成字符串
 74ExpandedSubBlockEnd.gif        }

 75InBlock.gif
 76ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 77InBlock.gif        /// DES + Base64 解密
 78InBlock.gif        /// </summary>
 79InBlock.gif        /// <param name="input">密文字符串</param>
 80ExpandedSubBlockEnd.gif        /// <returns>解密字符串</returns>

 81InBlock.gif        public static string DesBase64Decrypt(string input)
 82ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 83InBlock.gif            System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();
 84InBlock.gif            des.Mode = System.Security.Cryptography.CipherMode.ECB;
 85InBlock.gif            ICryptoTransform ct;
 86InBlock.gif            MemoryStream ms;
 87InBlock.gif            CryptoStream cs;
 88InBlock.gif            byte[] byt;
 89ExpandedSubBlockStart.gifContractedSubBlock.gif            byte[] Key = new byte[8]dot.gif{56,50,55,56,56,55,49,49}
 90ExpandedSubBlockStart.gifContractedSubBlock.gif            byte[] IV = new byte[8]dot.gif{0,0,0,0,0,0,0,0};
 91InBlock.gif            
 92InBlock.gif            ct = des.CreateDecryptor(Key, IV);
 93InBlock.gif            byt = Convert.FromBase64String(input); // 将 密文 以 Base64 编码转换成 byte 数组
 94InBlock.gif
 95InBlock.gif            ms = new MemoryStream();
 96InBlock.gif            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
 97InBlock.gif            cs.Write(byt, 0, byt.Length);
 98InBlock.gif            cs.FlushFinalBlock();
 99InBlock.gif
100InBlock.gif            cs.Close();
101InBlock.gif
102InBlock.gif            return Encoding.GetEncoding("GB2312").GetString(ms.ToArray()); // 将 明文 以 GB2312 编码转换成字符串
103ExpandedSubBlockEnd.gif        }

104InBlock.gif        
105InBlock.gif        
106InBlock.gif        
107ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
108InBlock.gif        /// DES + Base64 加密
109InBlock.gif        /// </summary>
110InBlock.gif        /// <param name="input">明文字符串</param>
111ExpandedSubBlockEnd.gif        /// <returns>已加密字符串</returns>

112InBlock.gif        public static string DesBase64EncryptForID5(string input)
113ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
114InBlock.gif            System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();
115InBlock.gif            des.Mode = System.Security.Cryptography.CipherMode.CBC;
116InBlock.gif            ICryptoTransform ct;
117InBlock.gif            MemoryStream ms;
118InBlock.gif            CryptoStream cs;
119InBlock.gif            byte[] byt;
120ExpandedSubBlockStart.gifContractedSubBlock.gif            byte[] Key = new byte[8]dot.gif{56,50,55,56,56,55,49,49}
121ExpandedSubBlockStart.gifContractedSubBlock.gif            byte[] IV = new byte[8]dot.gif{56,50,55,56,56,55,49,49}
122InBlock.gif
123InBlock.gif            ct = des.CreateEncryptor(Key, IV);
124InBlock.gif
125InBlock.gif            byt = Encoding.GetEncoding("GB2312").GetBytes(input); //根据 GB2312 编码对字符串处理,转换成 byte 数组
126InBlock.gif            
127InBlock.gif            ms = new MemoryStream();
128InBlock.gif            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
129InBlock.gif            cs.Write(byt, 0, byt.Length);
130InBlock.gif            cs.FlushFinalBlock();
131InBlock.gif
132InBlock.gif            cs.Close();
133InBlock.gif
134InBlock.gif            byte[] answer = ms.ToArray();
135InBlock.gif            for(int j=0;j<answer.Length;j++)
136ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
137InBlock.gif                Console.Write(answer[j].ToString()+ " ");
138ExpandedSubBlockEnd.gif            }

139InBlock.gif            Console.WriteLine();
140InBlock.gif            return Convert.ToBase64String(ms.ToArray()); // 将加密的 byte 数组依照 Base64 编码转换成字符串
141ExpandedSubBlockEnd.gif        }

142InBlock.gif        
143InBlock.gif        
144ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
145InBlock.gif        /// DES + Base64 解密
146InBlock.gif        /// </summary>
147InBlock.gif        /// <param name="input">密文字符串</param>
148ExpandedSubBlockEnd.gif        /// <returns>解密字符串</returns>

149InBlock.gif        public static string DesBase64DecryptForID5(string input)
150ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
151InBlock.gif            System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();
152InBlock.gif            des.Mode = System.Security.Cryptography.CipherMode.CBC;
153InBlock.gif            ICryptoTransform ct;
154InBlock.gif            MemoryStream ms;
155InBlock.gif            CryptoStream cs;
156InBlock.gif            byte[] byt;
157ExpandedSubBlockStart.gifContractedSubBlock.gif            byte[] Key = new byte[8]dot.gif{56,50,55,56,56,55,49,49}
158ExpandedSubBlockStart.gifContractedSubBlock.gif            byte[] IV = new byte[8]dot.gif{56,50,55,56,56,55,49,49}
159InBlock.gif            
160InBlock.gif            ct = des.CreateDecryptor(Key, IV);
161InBlock.gif            byt = Convert.FromBase64String(input); // 将 密文 以 Base64 编码转换成 byte 数组
162InBlock.gif
163InBlock.gif            ms = new MemoryStream();
164InBlock.gif            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
165InBlock.gif            cs.Write(byt, 0, byt.Length);
166InBlock.gif            cs.FlushFinalBlock();
167InBlock.gif
168InBlock.gif            cs.Close();
169InBlock.gif
170InBlock.gif            return Encoding.GetEncoding("GB2312").GetString(ms.ToArray()); // 将 明文 以 GB2312 编码转换成字符串
171ExpandedSubBlockEnd.gif        }

172InBlock.gif        
173InBlock.gif
174ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
175InBlock.gif        /// 3DES 加密 Byte[] to HEX string
176InBlock.gif        /// </summary>
177InBlock.gif        /// <param name="input">明文字符串</param>
178ExpandedSubBlockEnd.gif        /// <returns>已加密字符串</returns>

179InBlock.gif        public static string ThreeDesEncryptHEX(string input)
180ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
181InBlock.gif            string result = "";
182InBlock.gif            System.Security.Cryptography.TripleDES des = System.Security.Cryptography.TripleDES.Create();
183InBlock.gif            des.Mode = System.Security.Cryptography.CipherMode.CBC;
184InBlock.gif            des.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
185InBlock.gif            ICryptoTransform ct;
186InBlock.gif            MemoryStream ms;
187InBlock.gif            CryptoStream cs;
188InBlock.gif            byte[] byt;
189ExpandedSubBlockStart.gifContractedSubBlock.gif            byte[] Key = new byte[24]dot.gif{
190InBlock.gif                                         1,2,3,4,5,6,
191InBlock.gif                                         1,2,3,4,5,6,
192InBlock.gif                                         1,2,3,4,5,6,
193InBlock.gif                                         1,2,3,4,5,6
194ExpandedSubBlockEnd.gif                                     }

195ExpandedSubBlockStart.gifContractedSubBlock.gif            byte[] IV = new byte[8]dot.gif{1,2,3,4,5,6,1,2};
196InBlock.gif
197InBlock.gif            ct = des.CreateEncryptor(Key, IV);
198InBlock.gif
199InBlock.gif            byt = Encoding.GetEncoding("GB2312").GetBytes(input); //根据 GB2312 编码对字符串处理,转换成 byte 数组
200InBlock.gif            
201InBlock.gif            ms = new MemoryStream();
202InBlock.gif            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
203InBlock.gif            cs.Write(byt, 0, byt.Length);
204InBlock.gif            cs.FlushFinalBlock();
205InBlock.gif
206InBlock.gif            cs.Close();
207InBlock.gif
208InBlock.gif            byte[] answer = ms.ToArray();
209InBlock.gif            for(int j=0;j<answer.Length;j++)
210ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
211InBlock.gif                result += answer[j].ToString("x").PadLeft(2,'0');
212ExpandedSubBlockEnd.gif            }

213InBlock.gif            return result;
214ExpandedSubBlockEnd.gif        }

215InBlock.gif
216ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
217InBlock.gif        /// 3DES + HEX to byte[] 解密
218InBlock.gif        /// </summary>
219InBlock.gif        /// <param name="input">密文字符串</param>
220ExpandedSubBlockEnd.gif        /// <returns>解密字符串</returns>

221InBlock.gif        public static string ThreeDesDecryptHEX(string input)
222ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
223InBlock.gif            System.Security.Cryptography.TripleDES des = System.Security.Cryptography.TripleDES.Create();
224InBlock.gif            des.Mode = System.Security.Cryptography.CipherMode.CBC;
225InBlock.gif            des.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
226InBlock.gif            ICryptoTransform ct;
227InBlock.gif            MemoryStream ms;
228InBlock.gif            CryptoStream cs;
229ExpandedSubBlockStart.gifContractedSubBlock.gif            byte[] Key = new byte[24]dot.gif{
230InBlock.gif                                         1,2,3,4,5,6,
231InBlock.gif                                         1,2,3,4,5,6,
232InBlock.gif                                         1,2,3,4,5,6,
233InBlock.gif                                         1,2,3,4,5,6
234ExpandedSubBlockEnd.gif                                     }

235ExpandedSubBlockStart.gifContractedSubBlock.gif            byte[] IV = new byte[8]dot.gif{1,2,3,4,5,6,1,2};
236InBlock.gif            
237InBlock.gif            ct = des.CreateDecryptor(Key, IV);
238InBlock.gif            //byt = Convert.FromBase64String(input); // 将 密文 以 HEX to byte[]编码转换成 byte 数组
239InBlock.gif            if(input.Length<=1)
240ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
241InBlock.gif                throw new Exception("encrypted HEX string is too short!");
242ExpandedSubBlockEnd.gif            }

243InBlock.gif            byte[] byt = new byte[input.Length/2];
244InBlock.gif            for(int i=0;i<byt.Length;i++)
245ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
246InBlock.gif                //Console.WriteLine(input.Substring(i*2,2));
247InBlock.gif                byt[i] = Convert.ToByte(input.Substring(i*2,2),16);
248ExpandedSubBlockEnd.gif            }

249InBlock.gif
250InBlock.gif            ms = new MemoryStream();
251InBlock.gif            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
252InBlock.gif            cs.Write(byt, 0, byt.Length);
253InBlock.gif            cs.FlushFinalBlock();
254InBlock.gif
255InBlock.gif            cs.Close();
256InBlock.gif
257InBlock.gif            return Encoding.GetEncoding("GB2312").GetString(ms.ToArray()); // 将 明文 以 GB2312 编码转换成字符串
258ExpandedSubBlockEnd.gif        }

259ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
260InBlock.gif        /// Base64解码
261InBlock.gif        /// </summary>
262InBlock.gif        /// <param name="base64Str"></param>
263ExpandedSubBlockEnd.gif        /// <returns></returns>

264InBlock.gif        public static string DecodingFromBase64(string base64Str)
265ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
266InBlock.gif            Byte[] bytes = Convert.FromBase64String(base64Str);
267InBlock.gif            return System.Text.Encoding.UTF8.GetString(bytes);
268ExpandedSubBlockEnd.gif        }

269ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
270InBlock.gif        /// Base64编码
271InBlock.gif        /// </summary>
272InBlock.gif        /// <param name="str"></param>
273ExpandedSubBlockEnd.gif        /// <returns></returns>

274InBlock.gif        public static string EncodingToBase64(string str)
275ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
276InBlock.gif            return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
277ExpandedSubBlockEnd.gif        }

278ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
279InBlock.gif        /// 根据指定的编码方式Base64解码
280InBlock.gif        /// </summary>
281InBlock.gif        /// <param name="base64Str"></param>
282InBlock.gif        /// <param name="strEncoding"></param>
283ExpandedSubBlockEnd.gif        /// <returns></returns>

284InBlock.gif        public static string DecodingFromBase64(string base64Str,System.Text.Encoding strEncoding)
285ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
286InBlock.gif            Byte[] bytes = Convert.FromBase64String(base64Str);
287InBlock.gif            return strEncoding.GetString(bytes);
288ExpandedSubBlockEnd.gif        }

289ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
290InBlock.gif        /// 根据指定的编码方式Base64编码
291InBlock.gif        /// </summary>
292InBlock.gif        /// <param name="str"></param>
293InBlock.gif        /// <param name="strEncoding"></param>
294ExpandedSubBlockEnd.gif        /// <returns></returns>

295InBlock.gif        public static string EncodingToBase64(string str,System.Text.Encoding strEncoding)
296ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
297InBlock.gif            return Convert.ToBase64String(strEncoding.GetBytes(str));
298ExpandedSubBlockEnd.gif        }

299ExpandedBlockEnd.gif    }

两个常用的方法

 1ExpandedBlockStart.gifContractedBlock.gif    /**//// <summary>
 2InBlock.gif        /// 通过字节数组形式的密钥获取字符串形式的密钥
 3ExpandedBlockEnd.gif        /// </summary>

 4None.gif        void GetStringByByteArray()
 5ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
 6ExpandedSubBlockStart.gifContractedSubBlock.gif            byte[] Key = new byte[8]dot.gif{56,50,55,56,56,55,49,49}
 7InBlock.gif            Response.Write(System.Text.Encoding.Default.GetString(Key));
 8InBlock.gif            Response.End();
 9ExpandedBlockEnd.gif        }

10None.gif
11ExpandedBlockStart.gifContractedBlock.gif        /**//// <summary>
12InBlock.gif        /// 通过字符串形式的密钥获取字节数组形式的密钥
13ExpandedBlockEnd.gif        /// </summary>

14None.gif        void GetByteArrayByString()
15ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
16InBlock.gif            string key = "82788711";
17InBlock.gif            Response.Write(System.Text.Encoding.Default.GetBytes(key));
18InBlock.gif            Response.End();
19InBlock.gif            
20ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/cxd4321/archive/2007/02/25/655684.html

标题基于Python的自主学习系统后端设计与实现AI更换标题第1章引言介绍自主学习系统的研究背景、意义、现状以及本文的研究方法和创新点。1.1研究背景与意义阐述自主学习系统在教育技术领域的重要性和应用价值。1.2国内外研究现状分析国内外在自主学习系统后端技术方面的研究进展。1.3研究方法与创新点概述本文采用Python技术栈的设计方法和系统创新点。第2章相关理论与技术总结自主学习系统后端开发的相关理论和技术基础。2.1自主学习系统理论阐述自主学习系统的定义、特征和理论基础。2.2Python后端技术栈介绍DjangoFlask等Python后端框架及其适用场景。2.3数据库技术讨论关系型和非关系型数据库在系统中的应用方案。第3章系统设计与实现详细介绍自主学习系统后端的设计方案和实现过程。3.1系统架构设计提出基于微服务的系统架构设计方案。3.2核心模块设计详细说明用户管理、学习资源管理、进度跟踪等核心模块设计。3.3关键技术实现阐述个性化推荐算法、学习行为分析等关键技术的实现。第4章系统测试与评估对系统进行功能测试和性能评估。4.1测试环境与方法介绍测试环境配置和采用的测试方法。4.2功能测试结果展示各功能模块的测试结果和问题修复情况。4.3性能评估分析分析系统在高并发等场景下的性能表现。第5章结论与展望总结研究成果并提出未来改进方向。5.1研究结论概括系统设计的主要成果和技术创新。5.2未来展望指出系统局限性并提出后续优化方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值