谈谈数据加密的处理_第1页
谈谈数据加密的处理_第2页
谈谈数据加密的处理_第3页
谈谈数据加密的处理_第4页
谈谈数据加密的处理_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、谈谈数据加密的处理-提供各种算法处理 数据的加密重要性大家皆知,很多情况下需要对数据进行加密处理,但各种重要数据的加密要求不一样,有些需要时可逆的,有些是不要求可逆的,可逆的一般称之为对称加密算法,不可逆的一般可以成为非对称加密算法。如登录密码,一般较好的方式是采用不可逆的加密算法,如MD5、SHA256、哈希数值等,当然也有的采用可逆的强度好一些的加密方式,在选择加密键值的时候,变化一下也算是比较不错的选择。另外一些为了掩人耳目或者不让别人直接可以查看到,就采用其他的加密算法,如DES加密算法、AES的RijndaelManaged加密算法,或者也可以采用Base64加密,甚至我看到一个很变

2、态的方式,就是用MD5加密的头,内容则采用Base64的方式加密,一般不知道内情的还真的不好处理。 在吉日走火入魔失眠夜浅谈管理软件信息安全,用户名、密码的加密解密【附C#配套加密解密源码】 的文章中也对加密做了一些介绍和分析,并贴出了MD5、DES的加密方式代码,吉日的文章图文并茂、哲理及诙谐例子并存,本文有感而发,做一些补充,希望园子同行共通过切磋,交流心得。 加密字符串的方式有很多,也都可以通过这样的加密文件内容,不过较好的方式可能是采用DES及AES两种方式来加密文件了,下面贴出的代码中包含了加解密文件的算法。下面具体贴出本人收藏的各种加密算法代码。 1、DES加密字符串及文件等 如果

3、想可逆的算法,这种方式一般不错,只要结合动态密钥,就可以做出强度比较高的加密应用了。 #region DES对称加密解密 public const string DEFAULT_ENCRYPT_KEY = 12345678;/ / 使用默认加密/ / / public static string DesEncrypt(string strText) try return DesEncrypt(strText, DEFAULT_ENCRYPT_KEY); catch return ; / / 使用默认解密/ / / public static string DesDecrypt(string s

4、trText) try return DesDecrypt(strText, DEFAULT_ENCRYPT_KEY); catch return ; / / Encrypt the string / Attention:key must be 8 bits / / string/ key/ public static string DesEncrypt(string strText, string strEncrKey) byte byKey = null;byte IV = 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF ; byKey = E

5、ncoding.UTF8.GetBytes(strEncrKey.Substring(0, 8); DESCryptoServiceProvider des = new DESCryptoServiceProvider();byte inputByteArray = Encoding.UTF8.GetBytes(strText); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);

6、 cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock();return Convert.ToBase64String(ms.ToArray(); / / Decrypt string / Attention:key must be 8 bits / / Decrypt string/ key/ output stringpublic static string DesDecrypt(string strText, string sDecrKey) byte byKey = null;byte IV = 0

7、x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF ;byte inputByteArray = new BytestrText.Length; byKey = Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(strText); MemoryStream ms = new MemoryStream(

8、); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); Encoding encoding = new UTF8Encoding();return encoding.GetString(ms.ToArray(); / / Encrypt files / Attention:key must be 8 bits

9、 / / Encrypt file path/ output file/ keypublic static void DesEncrypt(string m_InFilePath, string m_OutFilePath, string strEncrKey) byte byKey = null;byte IV = 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF ; byKey = Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8); FileStream fin = new FileStream(

10、m_InFilePath, FileMode.Open, FileAccess.Read); FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write); fout.SetLength(0);/Create variables to help with read and write. byte bin = new byte100; /This is intermediate storage for the encryption. long rdlen = 0; /This is

11、 the total number of bytes written. long totlen = fin.Length; /This is the total length of the input file. int len; /This is the number of bytes to be written at a time. DES des = new DESCryptoServiceProvider(); CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(byKey, IV), CryptoSt

12、reamMode.Write);/Read from the input file, then encrypt and write to the output file. while (rdlen totlen) len = fin.Read(bin, 0, 100); encStream.Write(bin, 0, len); rdlen = rdlen + len; encStream.Close(); fout.Close(); fin.Close(); / / Decrypt files / Attention:key must be 8 bits / / Decrypt filepa

13、th/ output filepath/ keypublic static void DesDecrypt(string m_InFilePath, string m_OutFilePath, string sDecrKey) byte byKey = null;byte IV = 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF ; byKey = Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8); FileStream fin = new FileStream(m_InFilePath, FileMo

14、de.Open, FileAccess.Read); FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write); fout.SetLength(0);/Create variables to help with read and write. byte bin = new byte100; /This is intermediate storage for the encryption. long rdlen = 0; /This is the total number of

15、 bytes written. long totlen = fin.Length; /This is the total length of the input file. int len; /This is the number of bytes to be written at a time. DES des = new DESCryptoServiceProvider(); CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);/Rea

16、d from the input file, then encrypt and write to the output file. while (rdlen totlen) len = fin.Read(bin, 0, 100); encStream.Write(bin, 0, len); rdlen = rdlen + len; encStream.Close(); fout.Close(); fin.Close(); #endregion 2、 对称加密算法AES RijndaelManaged加密解密 #region 对称加密算法AES RijndaelManaged加密解密 priva

17、te static readonly string Default_AES_Key = #kim123;private static byte Keys = 0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79,0x53,0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F ;/ / 对称加密算法AES RijndaelManaged加密(RijndaelManaged(AES)算法是块式加密算法)/ / 待加密字符串/ 加密结果字符串public static string AES_Encrypt(string encryp

18、tString) return AES_Encrypt(encryptString, Default_AES_Key); / / 对称加密算法AES RijndaelManaged加密(RijndaelManaged(AES)算法是块式加密算法)/ / 待加密字符串/ 加密密钥,须半角字符/ 加密结果字符串public static string AES_Encrypt(string encryptString, string encryptKey) encryptKey = GetSubString(encryptKey, 32, ); encryptKey = encryptKey.Pad

19、Right(32, ); RijndaelManaged rijndaelProvider = new RijndaelManaged(); rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32); rijndaelProvider.IV = Keys; ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor();byte inputData = Encoding.UTF8.GetBytes(encryptString);b

20、yte encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length);return Convert.ToBase64String(encryptedData); / / 对称加密算法AES RijndaelManaged解密字符串/ / 待解密的字符串/ 解密成功返回解密后的字符串,失败返源串public static string AES_Decrypt(string decryptString) return AES_Decrypt(decryptString, Default_AES

21、_Key); / / 对称加密算法AES RijndaelManaged解密字符串/ / 待解密的字符串/ 解密密钥,和加密密钥相同/ 解密成功返回解密后的字符串,失败返回空public static string AES_Decrypt(string decryptString, string decryptKey) try decryptKey = GetSubString(decryptKey, 32, ); decryptKey = decryptKey.PadRight(32, ); RijndaelManaged rijndaelProvider = new RijndaelMan

22、aged(); rijndaelProvider.Key = Encoding.UTF8.GetBytes(decryptKey); rijndaelProvider.IV = Keys; ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor();byte inputData = Convert.FromBase64String(decryptString);byte decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputDa

23、ta.Length);return Encoding.UTF8.GetString(decryptedData); catch return string.Empty; / / 按字节长度(按字节,一个汉字为2个字节)取得某字符串的一部分/ / 源字符串/ 所取字符串字节长度/ 附加字符串(当字符串不够长时,尾部所添加的字符串,一般为.)/ 某字符串的一部分private static string GetSubString(string sourceString, int length, string tailString) return GetSubString(sourceString,

24、 0, length, tailString); / / 按字节长度(按字节,一个汉字为2个字节)取得某字符串的一部分/ / 源字符串/ 索引位置,以0开始/ 所取字符串字节长度/ 附加字符串(当字符串不够长时,尾部所添加的字符串,一般为.)/ 某字符串的一部分private static string GetSubString(string sourceString, int startIndex, int length, string tailString) string myResult = sourceString;/当是日文或韩文时(注:中文的范围:u4e00 - u9fa5, 日文

25、在u0800 - u4e00, 韩文为xAC00-xD7A3)if (System.Text.RegularExpressions.Regex.IsMatch(sourceString, u0800-u4e00+) | System.Text.RegularExpressions.Regex.IsMatch(sourceString, xAC00-xD7A3+) /当截取的起始位置超出字段串长度时if (startIndex = sourceString.Length) return string.Empty; else return sourceString.Substring(startI

26、ndex, (length + startIndex) sourceString.Length) ? (sourceString.Length - startIndex) : length); /中文字符,如中国人民abcd123if (length startIndex) int endIndex = bytesSource.Length;/当要截取的长度在字符串的有效长度范围内if (bytesSource.Length (startIndex + length) endIndex = length + startIndex; else /当不在有效范围内时,只取到字符串的结尾 lengt

27、h = bytesSource.Length - startIndex; tailString = ; int anResultFlag = new intlength;int nFlag = 0;/字节大于127为双字节字符for (int i = startIndex; i 127) nFlag+;if (nFlag = 3) nFlag = 1; else nFlag = 0; anResultFlagi = nFlag; /最后一个字节为双字节字符的一半if (bytesSourceendIndex - 1 127) & (anResultFlaglength - 1 = 1) len

28、gth = length + 1; byte bsResult = new bytelength; Array.Copy(bytesSource, startIndex, bsResult, 0, length); myResult = Encoding.Default.GetString(bsResult); myResult = myResult + tailString;return myResult; return string.Empty; / / 加密文件流/ / / public static CryptoStream AES_EncryptStrream(FileStream

29、fs, string decryptKey) decryptKey = GetSubString(decryptKey, 32, ); decryptKey = decryptKey.PadRight(32, ); RijndaelManaged rijndaelProvider = new RijndaelManaged(); rijndaelProvider.Key = Encoding.UTF8.GetBytes(decryptKey); rijndaelProvider.IV = Keys; ICryptoTransform encrypto = rijndaelProvider.Cr

30、eateEncryptor(); CryptoStream cytptostreamEncr = new CryptoStream(fs, encrypto, CryptoStreamMode.Write);return cytptostreamEncr; / / 解密文件流/ / / public static CryptoStream AES_DecryptStream(FileStream fs, string decryptKey) decryptKey = GetSubString(decryptKey, 32, ); decryptKey = decryptKey.PadRight

31、(32, ); RijndaelManaged rijndaelProvider = new RijndaelManaged(); rijndaelProvider.Key = Encoding.UTF8.GetBytes(decryptKey); rijndaelProvider.IV = Keys; ICryptoTransform Decrypto = rijndaelProvider.CreateDecryptor(); CryptoStream cytptostreamDecr = new CryptoStream(fs, Decrypto, CryptoStreamMode.Rea

32、d);return cytptostreamDecr; / / 对指定文件加密/ / / / public static bool AES_EncryptFile(string InputFile, string OutputFile) try string decryptKey = ; FileStream fr = new FileStream(InputFile, FileMode.Open); FileStream fren = new FileStream(OutputFile, FileMode.Create); CryptoStream Enfr = AES_EncryptStr

33、ream(fren, decryptKey);byte bytearrayinput = new bytefr.Length; fr.Read(bytearrayinput, 0, bytearrayinput.Length); Enfr.Write(bytearrayinput, 0, bytearrayinput.Length); Enfr.Close(); fr.Close(); fren.Close(); catch /文件异常return false; return true; / / 对指定的文件解压缩/ / / / public static bool AES_DecryptFi

34、le(string InputFile, string OutputFile) try string decryptKey = ; FileStream fr = new FileStream(InputFile, FileMode.Open); FileStream frde = new FileStream(OutputFile, FileMode.Create); CryptoStream Defr = AES_DecryptStream(fr, decryptKey);byte bytearrayoutput = new byte1024;int m_count = 0;do m_co

35、unt = Defr.Read(bytearrayoutput, 0, bytearrayoutput.Length); frde.Write(bytearrayoutput, 0, m_count);if (m_count bytearrayoutput.Length)break; while (true); Defr.Close(); fr.Close(); frde.Close(); catch /文件异常return false; return true; #endregion 3、 Base64加密解密算法 #region Base64加密解密 / / Base64是一種使用64基的

36、位置計數法。它使用2的最大次方來代表僅可列印的ASCII 字元。/ 這使它可用來作為電子郵件的傳輸編碼。在Base64中的變數使用字元A-Z、a-z和0-9 ,/ 這樣共有62個字元,用來作為開始的64個數字,最後兩個用來作為數字的符號在不同的/ 系統中而不同。/ Base64加密/ / / public static string Base64Encrypt(string str) byte encbuff = System.Text.Encoding.UTF8.GetBytes(str);return Convert.ToBase64String(encbuff); / / Base64解

37、密/ / / public static string Base64Decrypt(string str) byte decbuff = Convert.FromBase64String(str);return System.Text.Encoding.UTF8.GetString(decbuff); #endregion 4、 MD5加密及验证 MD5的加密处理应用还是比较多,由于破解难度很大,基本上大型网站或者软件商,密码加密一般采用这种方式居多。 而MD5可以用来获得32、 16、8等全部部分内容加密内容,也可以获取其加密后的哈希值。 / / 获得32位的MD5加密/ / / publi

38、c static string GetMD5_32(string input) System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();byte data = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(input); StringBuilder sb = new StringBuilder();for (int i = 0; i data.Length; i+) sb.Append(datai.ToString(x2); r

39、eturn sb.ToString(); / / 获得16位的MD5加密/ / / public static string GetMD5_16(string input) return GetMD5_32(input).Substring(8, 16); / / 获得8位的MD5加密/ / / public static string GetMD5_8(string input) return GetMD5_32(input).Substring(8, 8); / / 获得4位的MD5加密/ / / public static string GetMD5_4(string input) return GetMD5_32(input).Substring(8, 4); public static string MD5EncryptHash(String input) MD5 md5 = new MD5CryptoServiceProvider();/the GetBytes method returns byte array equavalent of a stringbyte res = md

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论