win8開發(9)——加密和解密數據

對於一些需要特列保護的數據,舉個例子,剛從服務器上取到的一堆JSON數據,並保存

到本地文件中,你知道,JSON數據是文本,如果我不希望別人查看數據,可以對文件進

行加密,今天,我們來看一種比較簡單的數據加密和解密。


實現這一構想,我們需要用到Windows.Security.Cryptography.DataProtection命名空間

下的DataProtectionProvider類,各位可以打開“對象瀏覽器”查看一下。


使用方法如下:
DataProtectionProvider類有兩個構造函數,即


  • public DataProtectionProvider()   


    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=234238 上有介紹


namespace App2
{
    /// <summary>
    /// 可用於自身或導航至 Frame 內部的空白頁。
    /// </summary>
    public sealed partial class MainPage : Page
    {


        StorageFile inputFile, outputFile;
        public MainPage()
        {
            this.InitializeComponent();
        }


        /// <summary>
        /// 在此頁將要在 Frame 中顯示時進行調用。
        /// </summary>
        /// <param name="e">描述如何訪問此頁的事件數據。Parameter
        /// 屬性通常用於配置頁。</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }


        private async void onInputFile(object sender, RoutedEventArgs e)
        {
            FileOpenPicker opPicker = new FileOpenPicker();
            opPicker.SuggestedStartLocation = PickerLocationId.Desktop;
            opPicker.FileTypeFilter.Add(".txt");
            opPicker.FileTypeFilter.Add(".data");
            this.inputFile = await opPicker.PickSingleFileAsync();
            Button btn = sender as Button;
            if (btn != null && inputFile != null)
            {
                btn.Content = inputFile.Path;
            }
        }


        private async void onOutputFile(object sender, RoutedEventArgs e)
        {
            FileSavePicker fsPicker = new FileSavePicker();
            fsPicker.FileTypeChoices.Add("加密文件", new string[] { ".data" });
            fsPicker.FileTypeChoices.Add("文件文件", new string[] { ".txt" });
            fsPicker.SuggestedStartLocation = PickerLocationId.Desktop;
            this.outputFile = await fsPicker.PickSaveFileAsync();
            Button btn = sender as Button;
            if (btn != null && outputFile != null)
            {
                btn.Content = outputFile.Path;
            }
        }


        private async void onProtect(object sender, RoutedEventArgs e)
        {
            if (inputFile == null || outputFile == null) return;


            IRandomAccessStream inputstr = await inputFile.OpenAsync(FileAccessMode.Read);
            IRandomAccessStream outputstr = await outputFile.OpenAsync(FileAccessMode.ReadWrite);


            DataProtectionProvider dp = new DataProtectionProvider("LOCAL=user");
            await dp.ProtectStreamAsync(inputstr, outputstr);
            this.msgLabel.Text = "完成數據加密。";


            inputFile = null;
            outputFile = null;
            ClearDisplay();
        }


        private async void onUnProtect(object sender, RoutedEventArgs e)
        {
            if (inputFile == null || outputFile == null)
            {
                return;
            }


            IRandomAccessStream inputstr = await inputFile.OpenAsync(FileAccessMode.Read);
            IRandomAccessStream outputstr = await outputFile.OpenAsync(FileAccessMode.ReadWrite);


            DataProtectionProvider dp = new DataProtectionProvider();
            await dp.UnprotectStreamAsync(inputstr, outputstr);
            this.msgLabel.Text = "解密數據完成。";


            inputFile = null;
            outputFile = null;
            ClearDisplay();
        }


        private void ClearDisplay()
        {
            this.btnPickInputFile.Content = "輸入文件...";
            this.btnPickOutputFile.Content = "輸出文件...";
            //this.msgLabel.Text = string.Empty;
        }
    }
}


代碼不算複雜,主要是DataProtectionProvider類的兩個方法:ProtectStreamAsync——對數據進行保護,第一個參數是輸入流,即要加密的數據,第二個參數爲輸出流,也就是加密後的數據。

UnprotectStreamAsync——解除對數據的保護,即解密。

上面兩個方法是針對流操作的,如果是針對字節緩衝區,即使用IBuffer的,可以用這兩個方法:

ProtectAsync
UnprotectAsync
實現的效果是一樣的,只是針對不同的對象而設計罷了。

現在,運行一下程序。
1、在桌面上新建一個txt文件,並輸入一些內容,保存。

2、把鼠標移到左上角,你會看到剛纔運行的應用程序,點一下,就切回到應用程序。

3、選擇剛纔新建的文本文件作爲輸入文件,並選擇一個輸出文件。
1.png 

4、點擊保護文件,完成加密。這時我們用記事本打開加密後的文件,看到的是亂碼。說明已加密。

2.png 

5、回到應用程序,把剛纔加密後的文件作爲輸入文件,另外選取一個輸入文件。點擊解除保護按鈕,完成後打開解密後的文件,對比一下原來的文件,看到了吧,解密成功。

3.png 

4.png 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章