Question

Basically I was just playing around with the provided sample of the Protect data and how you can encrypt your text file and what not. I managed to do a bit of modification and this is what i have soo far: Below is my XAML

<Grid x:Name="LayoutRoot" Background="Transparent">


    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock x:Name="ApplicationTitle" Text="Secure DATA" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock x:Name="PageTitle" Text="Encrypt" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Margin="12,0,12,0" Height="768" VerticalAlignment="Top">


        <TextBlock Margin="10,333,22,405" >
            <Run Text="Enter your "/>
            <Run Text="ACCOUNT PASSWORD:"/>
        </TextBlock>
        <TextBox x:Name="TBPin" Height="75" Width="444" HorizontalAlignment="Left" Margin="0,355,0,338" InputScope="Number" />
        <Button x:Name="BtnStore" Content="Save" Click="BtnStore_Click" Margin="0,585,222,103" FontSize="21.333" FontFamily="Segoe WP SemiLight" RenderTransformOrigin="0.495,0.481" />
        <Button x:Name="BtnRetrieve" Content="Load" Click="BtnRetrieve_Click" Margin="221,585,12,103" FontSize="21.333" FontFamily="Segoe WP SemiLight" />
        <TextBlock Margin="10,157,22,580" >
            <Run Text="Enter your "/>
            <Run Text="ACCOUNT NAME"/>
            <Run Text=":"/>
        </TextBlock>
        <TextBox x:Name="AccName" Width="444" HorizontalAlignment="Left" Margin="0,177,0,516" Height="75" InputScope="Chat" Text="LoL" />
        <TextBlock Margin="10,244,22,494" >
            <Run Text="Enter your "/>
            <Run Text="ACCOUNT USERNAME:"/>
        </TextBlock>
        <TextBox x:Name="AccUser" Height="75" Width="444" HorizontalAlignment="Left" Margin="0,266,0,427" />
        <TextBlock Margin="10,423,22,315" Text="Notes about this account:" />
        <TextBox x:Name="AccNotes" Width="444" HorizontalAlignment="Left" Margin="0,445,0,170" InputScope="Chat" />
    </Grid>
</Grid>

Below is the Code

Imports System
Imports System.Threading
Imports System.Windows.Controls
Imports Microsoft.Phone.Controls
Imports Microsoft.Phone.Shell
Imports System.IO
Imports System.IO.IsolatedStorage
Imports System.Text
Imports System.Security.Cryptography
Partial Public Class MainPage
Inherits PhoneApplicationPage
Private FilePath As String = "Pinfile"

Public Sub New()
    InitializeComponent()
    SupportedOrientations = SupportedPageOrientation.Portrait Or SupportedPageOrientation.Landscape
End Sub
Private Sub WritePinToFile(pinData As Byte())
    ' Create a file in the application's isolated storage.
    Dim file As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
    Dim writestream As New IsolatedStorageFileStream(AccName.Text, System.IO.FileMode.Create, System.IO.FileAccess.Write, file)
    ' Write pinData to the file.
    Dim writer As Stream = New StreamWriter(writestream).BaseStream
    writer.Write(pinData, 0, pinData.Length)
    writer.Close()
    writestream.Close()
End Sub
Private Function ReadPinFromFile() As Byte()
    ' Access the file in the application's isolated storage.
    Dim file As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
    Dim readstream As New IsolatedStorageFileStream(AccName.Text, System.IO.FileMode.Open, FileAccess.Read, file)
    ' Read the PIN from the file.
    Dim reader As Stream = New StreamReader(readstream).BaseStream
    Dim pinArray As Byte() = New Byte(reader.Length - 1) {}
    reader.Read(pinArray, 0, pinArray.Length)
    reader.Close()
    readstream.Close()
    Return pinArray
End Function
Private Sub BtnStore_Click(sender As Object, e As RoutedEventArgs)
    ' Convert the PIN to a byte[].
    Dim PinByte As Byte() = Encoding.UTF8.GetBytes(AccName.Text & vbNewLine & AccUser.Text & vbNewLine & TBPin.Text & vbNewLine & AccNotes.Text)
    ' Encrypt the PIN by using the Protect() method.
    Dim ProtectedPinByte As Byte() = ProtectedData.Protect(PinByte, Nothing)
    ' Store the encrypted PIN in isolated storage.
    Me.WritePinToFile(ProtectedPinByte)
    TBPin.Text = ""
End Sub
Private Sub BtnRetrieve_Click(sender As Object, e As RoutedEventArgs)
    ' Retrieve the PIN from isolated storage.
    Dim ProtectedPinByte As Byte() = Me.ReadPinFromFile()
    ' Decrypt the PIN by using the Unprotect method.
    Dim PinByte As Byte() = ProtectedData.Unprotect(ProtectedPinByte, Nothing)
    ' Convert the PIN from byte to string and display it in the text box.
    AccName.Text = Encoding.UTF8.GetString(PinByte, 0, PinByte.Length)
    AccUser.Text = Encoding.UTF8.GetString(PinByte, 0, PinByte.Length)
    TBPin.Text = Encoding.UTF8.GetString(PinByte, 0, PinByte.Length)
    AccNotes.Text = Encoding.UTF8.GetString(PinByte, 0, PinByte.Length)

End Sub
End Class

Soo far I click store and it stores and when I want to load i just type the name i saved the information with and it loads. The problem I'm having is that once i click Load it loads everything in that one textfile into every other textbox. This picture will illustrate my point better: http://puu.sh/5T4mc.png

As you can see it loads everything in one textbox for each and every textbox there is. What I want to do is e.g. For the Username textbox I want the first line only to show, Password textbox I want the 3rd line of that textfile only to show. How can i achieve this?

If possible VB code would be good but C# would also be fine. I could just convert it.

Thanks

Was it helpful?

Solution

Once you get the string, just use Split to retrieve each line, then assign them to the textbox you want. In C# it would be:

var PinByte = ProtectedData.Unprotect(ProtectedPinByte, null);
var content = Encoding.UTF8.GetString(PinByte, 0, PinByte.Length);
var lines = content.Split('\n');

AccName.Text = lines[0]; // Take the first line
TBPin.Text = lines[2]; // Take the third line
// and so on

My VB is rusted, but I believe it would be something like:

Dim ProtectedPinByte As Byte() = Me.ReadPinFromFile()
Dim PinByte As Byte() = ProtectedData.Unprotect(ProtectedPinByte, Nothing)
Dim content As String = Encoding.UTF8.GetString(PinByte, 0, PinByte.Length)
Dim lines As String() = content.Split(vbLf)

AccName.Text = lines(0)
TBPin.Text = lines(2)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top