using System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using Microsoft.Phone.Controls;using System.Security.Cryptography;//建立人:vakin 時間:2011-12-15namespace MicroBlogForWP7.Classes.Util{ //將輸入的字串轉換為位元組數組 //然後使用ProtectedData以及預先定義的位元組數組進行加密 //加密後得到的也是一個位元組資料 //最後使用Convert.ToBase64String得到其對應的字串 public class Encrypt { //預先定義的位元組數組如下: byte[] opt = new byte[] { 1, 2, 4, 8, 16 }; /// <summary> /// 密碼的加密操作 /// </summary> /// <param name="userpassword">未加密的密碼內容</param> /// <returns></returns> private string EncryptPwd(string userpassword) { byte[] input = System.Text.Encoding.UTF8.GetBytes(userpassword); string result = Convert.ToBase64String(ProtectedData.Protect(input, opt)); return result; } /// <summary> /// 解密解碼。沒有加密的情況下返回null /// </summary> /// <param name="userpassword">加密後的密碼內容</param> /// <returns></returns> private string DecryptPwd(string userpassword) { if (string.IsNullOrEmpty(userpassword)) { //沒有加密的密碼 return null; } byte[] output = Convert.FromBase64String(userpassword); byte[] en = ProtectedData.Unprotect(output, opt); string result = System.Text.Encoding.UTF8.GetString(en, 0, en.Length); return result; } }}