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) 
 
  
 
สร้างหน้าจอสำหรับการ 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;
        }
 
    }
}
 
 
  
 
ทดสอบการ Storeข้อมูล การเข้ารหัส (Encrypt) และสร้าง Text file 
 
  
 
แสดงการ Retrieve หรือ การถอดรหัส (Decrypt) จาก  Text file ที่อยู่ใน Isolated Storage 
 
  
 
เมื่อทดสอบการดึงข้อมูลมาไว้บน PC ด้วยคำสั่ง ISETool.exe ts xd 160bedc0-fd4b-431e-b602-b5350eb65903 "C:\data" 
 
  
 
ข้อมูลจาก Storage  
 
  
 
ข้อมูลที่ถูกดึงโดยไฟล์จะชื่อว่า pinfile 
 
  
 
จะเห็นว่าข้อมูลที่อยู่ใน pinfile จะถูกเข้ารหัสไว้ 
 
 
 
อ่านเพิ่มเติม 
              
  
              			
			  
								  
			  
  
                          
  | 
           
          
            
			  ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท 
              | 
           
          
 
       
		 
					
        
          
            
                
                   | 
                 
                
                  |   | 
                  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 |  
              | 
         
        
                        | 
          
		  
		   | 
         
         
          |             
		  
	
      
     | 
     
 
 
		  
         | 
		
          
		   
		  
              
      
     |