天天看點

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

下一篇: 日期

繼續閱讀