日期:2014-05-17  浏览次数:20667 次

新时尚Windows8开发(9):加密和解密数据

老周的博客http://blog.csdn.net/tcjiaan,转载请注明原作者和出处。

 

对于一些需要特列保护的数据,举个例子,刚从服务器上取到的一堆JSON数据,并保存到本地文件中,你知道,JSON数据是文本,如果我不希望别人查看数据,可以对文件进行加密,今天,我们来看一种比较简单的数据加密和解密。

 

实现这一构想,我们需要用到Windows.Security.Cryptography.DataProtection命名空间下的DataProtectionProvider类,各位可以打开“对象浏览器”查看一下。

 

使用方法如下:

DataProtectionProvider类有两个构造函数,即

1、

public DataProtectionProvider()


 

2、

public DataProtectionProvider(
  string protectionDescriptor
)


 

在加密数据的时候,使用第二个构造函数,即带一个参数的,参数为字符串类型,但是,这个字符串不是乱写的,不信你试试,随便写个字符串进去,加密的时候就会抛出异常。所以,这个参数应取下面这些值。

/****************************************************************************

Example Protection Descriptors:

  "SID=S-1-5-21-4392301 AND SID=S-1-5-21-3101812"
  "SDDL=O:S-1-5-5-0-290724G:SYD:(A;;CCDC;;;S-1-5-5-0-290724)(A;;DC;;;WD)"
  "LOCAL=user"
  "LOCAL=machine"
  "WEBCREDENTIALS=MyPasswordName"
  "WEBCREDENTIALS=MyPasswordName,myweb.com"

****************************************************************************/


 

而对于本地加密,只有两个可以用,其他的都会发生异常,哪两个呢?你猜,看他们的名字,本地使用的嘛,肯定带有“local”字样的,看看,上面的各值,哪些是带“local”的?

对,就是这两个

LOCAL=user

LOCAL=machine

那么它们有啥区别呢?看它们的值,懂了吗?一个是用户级别的加密,另一个呢?哈,当然是机器级别的。

我估计是这样的,有兴趣的朋友可以自己做做实验。

对于user级别,例如,我以用户名“dog”登陆了当前系统,然后我运了程序App,我在App中将文件kill加了密,如果我要将加密后的文件解密还原到kill的内容,当前电脑必须用“dog”的用户登陆才能完成操作。

而machine级别就好理解了,就是本机,拿到其他电脑上就解不了密,虽然SDK文档没有明地说明,但我估计应该是这样的。

 

虽然这种方式不能算是十分安全,但是对于一般数据就足够了。

 

接下来,我们通过一个实例来说一下如何使用。

1、启动VS,新建项目。

2、页面的XAML如下。

<Page
    x:Class="App2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App2"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <StackPanel>
            <Button Name="btnPickInputFile" Content="输入文件..." Margin="5,14,0,10" Click="onInputFile"/>
            <Button Name="btnPickOutputFile" Content="输出文件..." Margin="5,8,0,10" Click="onOutputFile"/>
            
            <Line Margin="0,3,0,12" Stroke="LightGray" StrokeThickness="3"/>
            
            <Button Content="保护文件" Click="onProtect"/>
            <Button Content="解除保护" Click="onUnProtect"/>
            
            <TextBlock Name="msgLabel" Margin="2,12,0,0" FontSize="20"/>
        </StackPanel>
    </Grid>
</Page>


 

3、转到代码视图,完成后面的代码,自己看看。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Security.Cryptography.DataProtection;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.Storage.Pickers;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=23423