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

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

 2012-05-15 16:04:51 来源:WEB开发网   
核心提示: DES And TripleDES[3DES] Component:TripleDES Cryptography On Silverlight And Windows Phone针对Nicolas Humann[Link in] 这个3DES版本移植ku.在其基础做了进一步的封装.为了是实现对TripleDESCr
  DES And TripleDES[3DES] Component:

TripleDES Cryptography On Silverlight And Windows Phone

针对Nicolas Humann[Link in] 这个3DES版本移植ku.在其基础做了进一步的封装.为了是实现对TripleDESCryptoServiceProvider 类[3DES核心实现类]对数据加密和解密的操作. 大概分为两种情况.一种是需要IV密钥Key 另外一种不需要密钥Key 数据基于3DES和DES 加密和解密的封装. 核心Code如下:

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: using System.Text; 13: using System.Security.Cryptography; 14: usingDataEncryptBuildDemo.DataEncryptCommon.DESDataEncrypt; 15:  16: namespaceDataEncryptBuildDemo.DataEncryptCommon 17: 18: /// <summary> 19: /// Des And TripleDES DataEncrypt Operator 20: /// Author:chenkai Date:14/5 2012 21: /// </summary> 22: public classDes_DataEncrypt 23: 24: /// <summary> 25: /// TripleDes Data Encrypt With Ot Encrypt Key Operator26: /// </summary> 27: /// <param name="sourceContent">Source Need to TripleDes Encrpt Data</param>28: /// <returns>Encrypt Data Byte[] String</returns> 29: public static byte[] TripleDesEncryptWithOutKey(string sourceContent) 30: 31: if (string.IsNullOrEmpty(sourceContent))32: return null33:  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: /// <summary> 43: /// TripleDes Data DeEncrypt With Out Encrypt Key Operator 44: /// </summary> 45: /// <param name="encryptBytes">Encrypt Byte Array</param> 46: /// <returns>DeEncrypt SourceContent String</returns> 47: public static string TripleDesDeEncryptWithOutKey(byte[] encryptBytes) 48: 49:if (encryptBytes == null || encryptBytes.Length <= 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: /// <summary> 61: /// TripleDes Data Encrypt Use IVKey Operator 62: /// </summary> 63: /// <param name="sourceContent">Source Content</param> 64: /// <param name="encryptKey">Encrypt Key</param> 65: /// <returns>Encrypt Bytes Array</returns> 66: public staticbyte[] TripleDesEncryptUseIvKey(string sourceContent, byte[] encryptIVKey) 67: 68: if(string.IsNullOrEmpty(sourceContent) || encryptIVKey == null || encryptIVKey.Length <= 0) 69: returnnull70:  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: /// <summary> 87: /// Triple Des DeEncrypt Operator Use IvKey 88: /// </summary> 89: /// <param name="encryptKey">Encrypt key can be null</param> 90: /// <param name="ivKey">Iv</param> 91: /// <param name="encryptBytes">EncryptBytes</param> 92: /// <returns>Return String </returns> 93: public staticstring TripleDesDeEncryptUseIvKey(byte[] encryptKey, byte[] ivKey, byte[] encryptBytes) 94: 95: if(encryptBytes == null || encryptBytes.Length <= 0) 96: return string.Empty; 97:  98:TripleDESCryptoServiceProvider tripleDesProvider = new TripleDESCryptoServiceProvider(); 99:  100: if(encryptKey == null101: 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: }

上一页  6 7 8 9 10 11 12 13  下一页

Tags:Windows phone 应用开发

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