對於一些需要特列保護的數據,舉個例子,剛從服務器上取到的一堆JSON數據,並保存 到本地文件中,你知道,JSON數據是文本,如果我不希望別人查看數據,可以對文件進 行加密,今天,我們來看一種比較簡單的數據加密和解密。 實現這一構想,我們需要用到Windows.Security.Cryptography.DataProtection命名空間 下的DataProtectionProvider類,各位可以打開“對象瀏覽器”查看一下。 使用方法如下: DataProtectionProvider類有兩個構造函數,即
public DataProtectionProvider(
string protectionDescriptor )
而對於本地加密,只有兩個可以用,其他的都會發生異常,哪兩個呢?你猜,看他們的名字 ,本地使用的嘛,肯定帶有“local”字樣的,看看,上面的各值,哪些是帶“local”的? 對,就是這兩個LOCAL=user LOCAL=machine 那麼它們有啥區別呢?看它們的值,懂了嗎?一個是用戶級別的加密,另一個呢?哈,當然 是機器級別的。 我估計是這樣的,有興趣的朋友可以自己做做實驗。對於user級別,例如,我以用戶名“dog”登陸了當前系統,然後我運了程序App,我在App中 將文件kill加了密,如果我要將加密後的文件解密還原到kill的內容,當前電腦必須用“dog”的 用戶登陸才能完成操作。 而machine級別就好理解了,就是本機,拿到其他電腦上就解不了密,雖然SDK文檔沒有明 地說明,但我估計應該是這樣的。 雖然這種方式不能算是十分安全,但是對於一般數據就足夠了。 接下來,我們通過一個實例來說一下如何使用。 1、啓動VS,新建項目。 2、頁面的XAML如下。
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; { /// <summary> /// 可用於自身或導航至 Frame 內部的空白頁。 /// </summary> public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } /// 在此頁將要在 Frame 中顯示時進行調用。 /// </summary> /// <param name="e">描述如何訪問此頁的事件數據。Parameter /// 屬性通常用於配置頁。</param> protected override void OnNavigatedTo(NavigationEventArgs 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; } } { 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; } } { if (inputFile == null || outputFile == null) return; IRandomAccessStream outputstr = await outputFile.OpenAsync(FileAccessMode.ReadWrite); await dp.ProtectStreamAsync(inputstr, outputstr); this.msgLabel.Text = "完成數據加密。"; outputFile = null; ClearDisplay(); } { if (inputFile == null || outputFile == null) { return; } IRandomAccessStream outputstr = await outputFile.OpenAsync(FileAccessMode.ReadWrite); await dp.UnprotectStreamAsync(inputstr, outputstr); this.msgLabel.Text = "解密數據完成。"; outputFile = null; ClearDisplay(); } { this.btnPickInputFile.Content = "輸入文件..."; this.btnPickOutputFile.Content = "輸出文件..."; //this.msgLabel.Text = string.Empty; } } } 代碼不算複雜,主要是DataProtectionProvider類的兩個方法:ProtectStreamAsync——對數據進行保護,第一個參數是輸入流,即要加密的數據,第二個參數爲輸出流,也就是加密後的數據。 UnprotectStreamAsync——解除對數據的保護,即解密。 上面兩個方法是針對流操作的,如果是針對字節緩衝區,即使用IBuffer的,可以用這兩個方法: ProtectAsync UnprotectAsync 實現的效果是一樣的,只是針對不同的對象而設計罷了。 現在,運行一下程序。 1、在桌面上新建一個txt文件,並輸入一些內容,保存。 2、把鼠標移到左上角,你會看到剛纔運行的應用程序,點一下,就切回到應用程序。 3、選擇剛纔新建的文本文件作爲輸入文件,並選擇一個輸出文件。 4、點擊保護文件,完成加密。這時我們用記事本打開加密後的文件,看到的是亂碼。說明已加密。 5、回到應用程序,把剛纔加密後的文件作爲輸入文件,另外選取一個輸入文件。點擊解除保護按鈕,完成後打開解密後的文件,對比一下原來的文件,看到了吧,解密成功。 |
win8開發(9)——加密和解密數據
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.