WEB开发网
开发学院手机开发Windows Phone开发 Windows phone应用开发[16]-数据加密 阅读

Windows phone应用开发[16]-数据加密

 2012-05-15 16:04:51 来源:WEB开发网   
核心提示:其他第三方方式均没有验证过.考虑该算法核心并不复杂.于是自己动手重写一个基于Windows Phone 版本HMACMD5的实现[验证通过]. 核心类如下:1: ?using System; 2: using System.Net; 3: using System.Windows; 4: using System.Wi

其他第三方方式均没有验证过.考虑该算法核心并不复杂.于是自己动手重写一个基于Windows Phone 版本HMACMD5的实现[验证通过]. 核心类如下:

1: ?using System; 2: using System.Net; 3: using System.Windows; 4: using System.Windows.Controls; 5:using System.Windows.Documents; 6: using System.Windows.Ink; 7: using System.Windows.Input; 8: usingSystem.Windows.Media; 9: using System.Windows.Media.Animation; 10: using System.Windows.Shapes; 11:  12: namespace DataEncryptBuildDemo.DataEncryptCommon 13: 14: /// <summary> 15: /// HMACMD Data Encrypt Operator 16: /// Author:chenkai Data:6/7/2011 17: /// </summary> 18: public classHMACMD5DataEncrypt 19: 20: /// <summary> 21: /// HMAC_MD5 DataEncrypt 22: /// </summary> 23: /// <param name="original">明文</param> 24: /// <param name="key">密钥</param> 25: /// <returns>返回加密的字符串</returns> 26: public static string HMAC_MD5(string original, string key) 27: 28: byte[] b_tmp; 29: byte[] b_tmp1; 30: if (key == null31: 32: return null33: 34: byte[] digest = newbyte[512]; 35: byte[] k_ipad = new byte[64]; 36: byte[] k_opad = new byte[64]; 37:  38: byte[] source = System.Text.UTF8Encoding.UTF8.GetBytes(key); 39: //System.Security.Cryptography.MD5 shainner = new MD5CryptoServiceProvider(); 40:  41: for (int i = 0; i < 64; i++) 42: 43: k_ipad[i] = 0 ^ 0x36; 44:k_opad[i] = 0 ^ 0x5c; 45: 46:  47: try 48: 49: if (source.Length > 64) 50: 51: //shainner = new MD5CryptoServiceProvider(); 52: source = MD5Core.GetHash(source);//shainner.ComputeHash(source); 53: }54:  55: for (int i = 0; i < source.Length; i++) 56: 57: k_ipad[i] = (byte)(source[i] ^ 0x36); 58:k_opad[i] = (byte)(source[i] ^ 0x5c); 59: 60:  61: b_tmp1 = System.Text.UTF8Encoding.UTF8.GetBytes(original);//内容 62: b_tmp = Adding(k_ipad, b_tmp1); 63:  64: 65: //shainner = new MD5CryptoServiceProvider(); 66: digest = MD5Core.GetHash(b_tmp);//shainner.ComputeHash(b_tmp); 67: b_tmp = Adding(k_opad, digest); 68:  69:  70: //shainner = new MD5CryptoServiceProvider(); 71: digest = MD5Core.GetHash(b_tmp); //shainner.ComputeHash(b_tmp); 72:return ByteToString(digest); 73: 74: catch (Exception e) 75: 76: throw e; 77: 78: 79:  80:/// <summary> 81: /// 填充byte 82: /// </summary> 83: /// <param name="a"></param> 84: /// <param name="b"></param> 85: /// <returns></returns> 86: private static byte[] Adding(byte[] a, byte[] b) 87:88: byte[] c = new byte[a.Length + b.Length]; 89: a.CopyTo(c, 0); 90: b.CopyTo(c, a.Length); 91:return c; 92: 93:  94: /// <summary> 95: /// Byte To String 96: /// </summary> 97: /// <param name="buff"></param> 98: /// <returns></returns> 99: private static string ByteToString(byte[] buff)100: 101: string sbinary = ""102:  103: for (int i = 0; i < buff.Length; i++) 104: 105: sbinary += buff[i].ToString("X2"); // hex format 106: 107: return (sbinary); 108: 109: 110: }

上一页  1 2 3 4 5 6 7 8 9 10 11  下一页

Tags:Windows phone 应用开发

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接