מדריך Windows Phone – שמירת מידע בצורה מוצפנת
בפרק זה נראה כיצד ניתן לשמור מידע סודי כגון סיסמאות בצורה מוצפנת בסביבת Windows Phone.
שמירת מידע סודי (כגון סיסמא) ב Isolated Storage של הפלאפון איננה בטוחה, שכן המידע הסודי ניתן לחילוץ בקלות. גם אם נצפין את המידע הסודי שלנו, אך את מפתח ההצפנה נשמור ב Isolated Storage יהיה ניתן לחלץ את מפתח ההצפנה ואז גם את המידע הסודי.
לשם הצפנה בטוחה של מידע סודי ניתן להשתמש במחלקה ProtectedData ובפונקציות Protect ו Unprotect כדי לבצע הצפנה של מידע תוך שימוש במפתחות הצפנה סודיים שנוצרים ע"י מערכת ההפעלה באופן אוטומטי ותלוי אפליקציה.
בדוגמא הבאה נבנה אפליקציה שמקבלת מידע סודי מהמשתמש ושומרת אותו בצורה מוצפנת בלחיצת כפתור. בנוסף לחיצה על כפתור נוסף פותחת את המידע שנשמר בצורה מוצפנת.
ראשית נגדיר את ה XAML של האפליקציה:
<StackPanel>
<TextBlock Text="Enter secret data:" />
<TextBox x:Name="textBoxSecretData"
Height="75"
Width="300" />
<Button Content="Store"
Click="BtnStore_Click" />
<Button Content="Retrieve"
Click="BtnRetrieve_Click" />
</StackPanel>
כעת נראה כיצד לממש את שמירת המידע המוצפן ופתיחתו.
שמירת מידע בצורה מוצפנת ב Isolated Storage
נבצע את שמירת המידע המוצפן ב Isolated Storage בשני שלבים. ראשית יש לקחת את המידע שהמשתמש הכניס ולהצפין אותו. זאת נעשה ע"י שימוש בפונקציה Protect של האובייקט ProtectedData. שימו לב שפונקציה זו עובדת רק עם מערך של בתים, ולכן יש להמיר את המחרוזת למערך שכזה.
הקוד הבא מבצע זאת:
private void BtnStore_Click(object sender, RoutedEventArgs e)
{
// Convert the secret data to a byte[].
byte[] secretDataByte = Encoding.UTF8.GetBytes(textBoxSecretData.Text);
// Encrypt the secret data by using the Protect() method.
byte[] protectedSecretDataByte = ProtectedData.Protect(secretDataByte, null);
// Store the encrypted secret data in isolated storage.
this.WriteSecretDataToFile(protectedSecretDataByte);
textBoxSecretData.Text = "";
}
בשלב השני נבצע את השמירת המידע המוצפן בתוך ה Isolated Storage בעזרת מימוש פונקציית עזר בשם WriteSecretDataToFile:
private void WriteSecretDataToFile(byte[] secretData)
{
// Create a file in the application's isolated storage.
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream writestream =
new IsolatedStorageFileStream("mySecretDataFile", FileMode.Create, FileAccess.Write, file);
// Write textBoxSecretData to the file.
Stream writer = new StreamWriter(writestream).BaseStream;
writer.Write(secretData, 0, secretData.Length);
writer.Close();
writestream.Close();
}
כעת המידע יושב בצורה מוצפנת ב Isolated Storage.
פתיחת מידע שנשמר בצורה מוצפנת ב Isolated Storage
לצורך פתיחת המידע המוצפן מתוך ה Isolated Storage נבצע שוב שני שלבים, אבל הפעם בסדר הפוך. ראשית יש לקרוא את המידע מתוך ה Isolated Storage, נעשה זאת ע"י שימוש בפונקציית עזר ReadSecretDataFromFile:
private byte[] ReadSecretDataFromFile()
{
// Access the file in the application's isolated storage.
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream readstream =
new IsolatedStorageFileStream("mySecretDataFile", FileMode.Open, FileAccess.Read, file);
// Read the secret data from the file.
Stream reader = new StreamReader(readstream).BaseStream;
byte[] secretDataArray = new byte[reader.Length];
reader.Read(secretDataArray, 0, secretDataArray.Length);
reader.Close();
readstream.Close();
return secretDataArray;
}
בשלב שני נראה כיצד ניתן לקחת את המידע המוצפן ולפתוח אותו באמצעות הפונקציה Unprotect של האובייקט ProtectedData:
private void BtnRetrieve_Click(object sender, RoutedEventArgs e)
{
// Retrieve the secret data from isolated storage.
byte[] protectedSecretDataByte = this.ReadSecretDataFromFile();
// Decrypt the secret data by using the Unprotect method.
byte[] secretDataByte = ProtectedData.Unprotect(protectedSecretDataByte, null);
// Convert the secret data from byte to string and display it in the text box.
textBoxSecretData.Text = Encoding.UTF8.GetString(secretDataByte, 0, secretDataByte.Length);
}
תוצאת ריצת האפליקציה הנ"ל תראה כך:
תגובות בפייסבוק