天天看点

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

如果需要在服务器端和和客户端传递类似MD5、HMAC_MD5或是解析服务器端传递的DES、TripleDES[3DES]数据是发现现在Windows Phone针对数据加密和解析并没有对应的支持.其实这些问题.原来都在我们团队实际Coding过程都碰到过.也算是找到一些比较成熟的解决方案.如下.

本篇幅针对MD5,HMAC_MD5,DES,TripleDES[3DES]算法给出Windows Phone移植版本.

首先来说说MD5.

Silverlight MD5 Implementation:

HMAC_MD5.

使用 MD5 哈希函数计算基于哈希值的消息验证代码 [HMAC].

HMACMD5 是从 MD5 哈希函数构造的一种键控哈希算法,被用作基于哈希的消息验证代码 [HMAC].此 HMAC 进程将密钥与消息数据混合,使用哈希函数对混合结果进行哈希计算,将所得哈希值与该密钥混合,然后再次应用哈希函数。输出的哈希值长度为 128 位.

HMACMD5 For Silverlight/Windows Phone: <a href="http://hmacmd5.codeplex.com/">http://hmacmd5.codeplex.com/</a>

其他第三方方式均没有验证过.考虑该算法核心并不复杂.于是自己动手重写一个基于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: using System.Windows.Media;9: using System.Windows.Media.Animation; 10: using System.Windows.Shapes; 11:   12: namespace DataEncryptBuildDemo.DataEncryptCommon 13: { 14: /// &lt;summary&gt; 15: /// HMACMD Data Encrypt Operator 16: /// Author:chenkai Data:6/7/2011 17: /// &lt;/summary&gt; 18: public class HMACMD5DataEncrypt 19: { 20: /// &lt;summary&gt; 21: /// HMAC_MD5 DataEncrypt 22: /// &lt;/summary&gt; 23: /// &lt;param name="original"&gt;明文&lt;/param&gt; 24: /// &lt;param name="key"&gt;密钥&lt;/param&gt; 25: /// &lt;returns&gt;返回加密的字符串&lt;/returns&gt; 26: public static string HMAC_MD5(string original, string key) 27: { 28: byte[] b_tmp; 29: byte[] b_tmp1; 30: if (key == null) 31: { 32: return null; 33: } 34: byte[] digest = new byte[512]; 35: byte[] k_ipad = newbyte[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 &lt; 64; i++) 42: {43: k_ipad[i] = 0 ^ 0x36; 44: k_opad[i] = 0 ^ 0x5c; 45: } 46:   47: try 48: { 49: if (source.Length &gt; 64) 50: { 51: //shainner = new MD5CryptoServiceProvider(); 52: source = MD5Core.GetHash(source);//shainner.ComputeHash(source); 53: } 54:  55: for (int i = 0; i &lt; 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: /// &amp;lt;summary&gt; 81: /// 填充byte 82: /// &lt;/summary&gt; 83: /// &lt;param name="a"&gt;&lt;/param&gt; 84: /// &lt;param name="b"&gt;&lt;/param&gt; 85: /// &lt;returns&gt;&lt;/returns&gt; 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: /// &lt;summary&gt; 95: /// Byte To String 96: /// &lt;/summary&gt; 97: /// &lt;param name="buff"&gt;&lt;/param&gt; 98: /// &lt;returns&gt;&lt;/returns&gt; 99: private static string ByteToString(byte[] buff) 100: { 101: string sbinary = ""; 102:   103: for (int i = 0; i &lt; buff.Length; i++) 104: { 105: sbinary += buff[i].ToString("X2"); // hex format 106: } 107: return (sbinary); 108: } 109: } 110: }

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

TripleDES[3DES]

Triple DES又称3DES,是DES加密算法的一种模式.[TDEA,Triple Data Encryption Algorithm]块密码的通称。它相当于是对每个数据块应用三次DES加密算法.现在计算机运算能力的增强,原版DES密码的密钥长度变得容易被暴力破解;3DES即是设计用来提供一种相对简单的方法,即通过增加DES的密钥长度来避免类似的攻击,而不是设计一种全新的块密码算法.

一开始我大概看了DES 算法在C下面的实现很简单.费了点时间很快就移植Windows Phone DES算法版本. DES算法的核心采用位运算的. 每次8个字节也就是64位内容.密钥key也为64位.然后经过16轮置换. 可惜 TripleDES[3DES]始终没有移植成功.重复造轮子无果后.果断寻求第三方解决方案.

SSH.NET Library: <a href="http://sshnet.codeplex.com/">http://sshnet.codeplex.com/</a>

SSH.NET Library库移植灵感是来源于Java版本.不过SSH。NET是完全基于.NET 没有采用任何第三方组件和引用. 其中实现也包含同步和异步的封装. Socket通信库. HTTP代等.其中最为重要的是基于.NET 实现DES和TripleDES[3DES] 两种核心算法. 并且支持.NET 3.5 、Silverlight、Windows Phone.

<a href="http://blog.humann.info/post/2011/09/02/TripleDES-cryptography-on-silverlight-and-Windows-Phone.aspx">TripleDES Cryptography On Silverlight And Windows Phone</a>

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: using System.Windows.Media; 9: using System.Windows.Media.Animation; 10: using System.Windows.Shapes; 11:   12: using System.Text; 13: using System.Security.Cryptography; 14: using DataEncryptBuildDemo.DataEncryptCommon.DESDataEncrypt; 15:   16: namespace DataEncryptBuildDemo.DataEncryptCommon 17: { 18: /// &amp;lt;summary&gt; 19: /// Des And TripleDES DataEncrypt Operator 20: /// Author:chenkai Date:14/5 2012 21: /// &lt;/summary&gt; 22: public class Des_DataEncrypt 23: { 24: /// &lt;summary&gt;25: /// TripleDes Data Encrypt With Ot Encrypt Key Operator 26: /// &lt;/summary&gt; 27: /// &lt;param name="sourceContent"&gt;Source Need to TripleDes Encrpt Data&lt;/param&gt; 28: /// &lt;returns&gt;Encrypt Data Byte[] String&lt;/returns&gt; 29: public staticbyte[] TripleDesEncryptWithOutKey(string sourceContent) 30: { 31: if (string.IsNullOrEmpty(sourceContent)) 32: return null;33:   34: var toEncryptSourceStr = Encoding.UTF8.GetBytes(sourceContent); 35: TripleDESCryptoServiceProvider tripleDesEncryptProvider = new TripleDESCryptoServiceProvider(); 36: ICryptoTransform encryptTransform=tripleDesEncryptProvider.CreateEncryptor(); 37: byte[] encryptToBytes = encryptTransform.TransformFinalBlock(toEncryptSourceStr, 0, toEncryptSourceStr.Length); 38:   39: return encryptToBytes; 40: } 41:   42: /// &lt;summary&gt; 43: /// TripleDes Data DeEncrypt With Out Encrypt Key Operator 44: /// &lt;/summary&gt; 45: /// &lt;param name="encryptBytes"&gt;Encrypt Byte Array&lt;/param&gt; 46:/// &lt;returns&gt;DeEncrypt SourceContent String&lt;/returns&gt; 47: public static string TripleDesDeEncryptWithOutKey(byte[] encryptBytes) 48: { 49: if (encryptBytes == null || encryptBytes.Length &lt;= 0) 50: return string.Empty; 51:   52: TripleDESCryptoServiceProvider tripleDesProvider = new TripleDESCryptoServiceProvider(); 53: ICryptoTransform deEncryptTransform = tripleDesProvider.CreateDecryptor(); 54: var deEncryptBytes = deEncryptTransform.TransformFinalBlock(encryptBytes, 0, encryptBytes.Length); 55: var deEncryptFormatStr = Encoding.UTF8.GetString(deEncryptBytes, 0, deEncryptBytes.Length); 56:   57: return deEncryptFormatStr; 58: } 59:   60: /// &amp;lt;summary&gt; 61: /// TripleDes Data Encrypt Use IVKey Operator 62: /// &lt;/summary&gt; 63: /// &lt;param name="sourceContent"&gt;Source Content&lt;/param&gt; 64: /// &lt;param name="encryptKey"&gt;Encrypt Key&lt;/param&gt; 65: /// &lt;returns&gt;Encrypt Bytes Array&lt;/returns&gt; 66: public static byte[] TripleDesEncryptUseIvKey(string sourceContent, byte[] encryptIVKey) 67: { 68: if (string.IsNullOrEmpty(sourceContent) || encryptIVKey == null || encryptIVKey.Length &lt;= 0) 69: return null; 70:   71: var toEncryptSourceStr = Encoding.UTF8.GetBytes(sourceContent); 72:TripleDESCryptoServiceProvider tripleDesProvider = new TripleDESCryptoServiceProvider(); 73:   74: //No Seting Pading 76: var key = tripleDesProvider.Key; //Save Key 77: IsolatedStorageCommon.IsolatedStorageSettingHelper.AddIsolateStorageObj("EncryptKey", key); 78: ICryptoTransform encryptTransform = tripleDesProvider.CreateEncryptor(key, encryptIVKey); 79: var encryptBytes = encryptTransform.TransformFinalBlock(toEncryptSourceStr, 0, toEncryptSourceStr.Length); 80:   81: return encryptBytes; 82: } 83:   84:   85:   86: /// &amp;lt;summary&gt; 87: /// Triple Des DeEncrypt Operator Use IvKey88: /// &lt;/summary&gt; 89: /// &lt;param name="encryptKey"&gt;Encrypt key can be null&lt;/param&gt; 90: /// &lt;param name="ivKey"&gt;Iv&lt;/param&gt; 91: /// &lt;param name="encryptBytes"&gt;EncryptBytes&lt;/param&gt; 92: /// &lt;returns&gt;Return String &lt;/returns&gt; 93: publicstatic string TripleDesDeEncryptUseIvKey(byte[] encryptKey, byte[] ivKey, byte[] encryptBytes) 94: { 95: if (encryptBytes == null || encryptBytes.Length &amp;lt;= 0) 96: return string.Empty; 97:   98: TripleDESCryptoServiceProvider tripleDesProvider = new TripleDESCryptoServiceProvider(); 99:   100: if (encryptKey == null) 101: encryptKey = IsolatedStorageCommon.IsolatedStorageSettingHelper.GetIsolateStorageByObj("EncryptKey") as byte[]; 102: ICryptoTransform deEncryptTransform = tripleDesProvider.CreateDecryptor(encryptKey, ivKey); 103: var DecryptBytes = deEncryptTransform.TransformFinalBlock(encryptBytes, 0, encryptBytes.Length); 104: string unDecryptFomatStr = Encoding.UTF8.GetString(DecryptBytes, 0, DecryptBytes.Length); 105:   106: return unDecryptFomatStr; 107: } 108: } 109: }

至此关于应用开发常见的MD5、MAC_MD5、DES、3DES Windows Phone移植版本算法可用库如上.如上代码均实际项目中验证通过.对于还在苦苦重复造轮子同学.如果觉得为了使用效率.还是值得参考的.

参考链接:

<a href="http://www.broccoliproducts.com/softnotebook/desblowfish/desblowfish.php">DES, TripleDES and BlowFish in Silverlight</a>

本文转自chenkaiunion 51CTO博客,原文链接:http://blog.51cto.com/chenkai/863896

下一篇: 日期

继续阅读