Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Windows Phone Dev - สอนเขียน App บนโปรแกรม Windows Phone 7 , Windows Phone 8 > Windows Phone 8 and Storage - Encrypt Data for Windows Phone



Clound SSD Virtual Server

Windows Phone 8 and Storage - Encrypt Data for Windows Phone

Windows Phone and Storage - Encrypt Data for Windows Phone เพื่อลดความเสี่ยงของข้อมูลที่ถูกจัดเก็บลงใน Storage ของ Windows Phone 8 ที่อยู่ในรูปแบบต่าง ๆ ในกรณีที่ข้อมูลนั้น ๆ มีความสำคัญ และไม่ต้องการให้สามารถอ่านข้อมูลเหล่านั้นออกได้ ก่อนที่จะจัดเก็บข้อมูลเราสามารถใช้การเข้ารหัสข้อความ (Encrypt) โดยข้อความที่ถูกเข้ารหัสจะไม่สามารถอ่านออกได้ และ เมื่อต้องการอ่านข้อมูลนั้นก็ใช้ การถอดรหัสข้อความ (Decrypt) ก่อนนำไปใช้งาน จึงจะสามารถอ่านข้อมูลนั้น สามารถประยุกต์ได้ทั้งการจัดเก็บข้อมูลบน Isolated Storage , Local Data , Roaming Data และ Temprary Data

ตัวอย่างการ Encrypt และ Decrypt ข้อมูล

เรียกใช้งาน Class ของ System.Security.Cryptography
using System.Security.Cryptography;

การ Encrypt หรือเข้ารหัสข้อความ
            // Convert the PIN to a byte[].
            byte[] PinByte = Encoding.UTF8.GetBytes(TBPin.Text);

            // Encrypt the PIN by using the Protect() method.
            byte[] ProtectedPinByte = ProtectedData.Protect(PinByte, null);

การ Decrypt หรือถอดรหัสข้อความ
            // Retrieve the PIN from isolated storage.
            byte[] ProtectedPinByte = this.ReadPinFromFile();

            // Decrypt the PIN by using the Unprotect method.
            byte[] PinByte = ProtectedData.Unprotect(ProtectedPinByte, null);

            // Convert the PIN from byte to string and display it in the text box.
            TBPin.Text = Encoding.UTF8.GetString(PinByte, 0, PinByte.Length);


ลองมาดูตัวอย่างเพื่อความเข้าใจมากขึ้น

Example สร้าง Encrypt เข้ารหัสข้อความและจัดเก็บลงไว้ใน Isolated Storage (Text file)

Windows Phone and Storage - Encrypt Data for Windows Phone

สร้างหน้าจอสำหรับการ Input ข้อมูล และการ Encrypt เข้ารหัส และ และการ Read / Write ข้อมูลลงใน Text file

MainPage.xaml
        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition />
                <RowDefinition Height="auto" />
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>

            <TextBlock Grid.Row="0" Text="Pin" Margin="10" />
            <TextBox x:Name="TBPin" Height="75" Width="300" HorizontalAlignment="Left" Margin="83,22,0,44" Grid.Row="1" />
            <Button x:Name="BtnStore" Content="Store" Grid.Row="1" Click="BtnStore_Click" Margin="-6,123,6,58" Grid.RowSpan="3" />
            <Button x:Name="BtnRetrieve" Content="Retrieve" Grid.Row="3" Click="BtnRetrieve_Click" Margin="-6,111,6,72" Grid.RowSpan="2" />
        </Grid>









MainPage.xaml.cs
using System.IO;
using System.IO.IsolatedStorage;
using System.Text;
using System.Security.Cryptography;


namespace myPhoneApp
{

    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private string FilePath = "pinfile";

        private void BtnStore_Click(object sender, RoutedEventArgs e)
        {
            // Convert the PIN to a byte[].
            byte[] PinByte = Encoding.UTF8.GetBytes(TBPin.Text);

            // Encrypt the PIN by using the Protect() method.
            byte[] ProtectedPinByte = ProtectedData.Protect(PinByte, null);

            // Store the encrypted PIN in isolated storage.
            this.WritePinToFile(ProtectedPinByte);

            TBPin.Text = "";

        }

        private void WritePinToFile(byte[] pinData)
        {
            // Create a file in the application's isolated storage.
            IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream writestream = new IsolatedStorageFileStream(FilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, file);

            // Write pinData to the file.
            Stream writer = new StreamWriter(writestream).BaseStream;
            writer.Write(pinData, 0, pinData.Length);
            writer.Close();
            writestream.Close();
        }

        private void BtnRetrieve_Click(object sender, RoutedEventArgs e)
        {
            // Retrieve the PIN from isolated storage.
            byte[] ProtectedPinByte = this.ReadPinFromFile();

            // Decrypt the PIN by using the Unprotect method.
            byte[] PinByte = ProtectedData.Unprotect(ProtectedPinByte, null);

            // Convert the PIN from byte to string and display it in the text box.
            TBPin.Text = Encoding.UTF8.GetString(PinByte, 0, PinByte.Length);

        }


        private byte[] ReadPinFromFile()
        {
            // Access the file in the application's isolated storage.
            IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream readstream = new IsolatedStorageFileStream(FilePath, System.IO.FileMode.Open, FileAccess.Read, file);

            // Read the PIN from the file.
            Stream reader = new StreamReader(readstream).BaseStream;
            byte[] pinArray = new byte[reader.Length];

            reader.Read(pinArray, 0, pinArray.Length);
            reader.Close();
            readstream.Close();

            return pinArray;
        }

 
    }
}


Windows Phone and Storage - Encrypt Data for Windows Phone

ทดสอบการ Storeข้อมูล การเข้ารหัส (Encrypt) และสร้าง Text file

Windows Phone and Storage - Encrypt Data for Windows Phone

แสดงการ Retrieve หรือ การถอดรหัส (Decrypt) จาก Text file ที่อยู่ใน Isolated Storage

Windows Phone and Storage - Encrypt Data for Windows Phone

เมื่อทดสอบการดึงข้อมูลมาไว้บน PC ด้วยคำสั่ง ISETool.exe ts xd 160bedc0-fd4b-431e-b602-b5350eb65903 "C:\data"

Windows Phone and Storage - Encrypt Data for Windows Phone

ข้อมูลจาก Storage

Windows Phone and Storage - Encrypt Data for Windows Phone

ข้อมูลที่ถูกดึงโดยไฟล์จะชื่อว่า pinfile

Windows Phone and Storage - Encrypt Data for Windows Phone

จะเห็นว่าข้อมูลที่อยู่ใน pinfile จะถูกเข้ารหัสไว้








อ่านเพิ่มเติม


   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2014-07-27 19:05:59 / 2017-03-25 22:35:25
  Download : No files
 Sponsored Links / Related

 
Windows Phone 8 and Isolated Storage Tools
Rating :

 
Windows Phone 8 and Isolated Storage (Settings , Files and folders)
Rating :

 
Windows Phone 8 and Storage - Local Data (Application Data)
Rating :

 
Windows Phone 8 and Storage - Roaming Data (Application Data)
Rating :

 
Windows Phone 8 and Storage - Temporary Data (Application Data)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่