Windows Phone开发之客户端本地简单填充加密解密

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; 
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值