windows phone开发之客户端本地简单填充加密解密

来源:互联网 发布:r语言数据分类 编辑:程序博客网 时间:2024/06/06 08:34
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-15  
  
namespace 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;   
        }  
    }  
原创粉丝点击