基本上我只是在使用提供的Protect数据示例以及如何加密文本文件以及不加密文本文件。我设法做了一些修改,这就是我所拥有的: 以下是我的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>
以下是代码
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
到目前为止,我点击商店并存储,当我想加载时,只需输入我保存信息的名称,然后加载。 我遇到的问题是,一旦我点击加载它就会将该文本文件中的所有内容加载到其他所有文本框中。这张照片将更好地说明我的观点:http://puu.sh/5T4mc.png
正如您所看到的,它为每个文本框加载了所有文本框中的所有内容。 我想做的是例如对于用户名文本框,我希望第一行只显示,密码文本框我希望该文本文件的第3行只显示。我如何实现这一目标?
如果可能,VB代码会很好,但C#也可以。我可以转换它。
由于
答案 0 :(得分:3)
获得字符串后,只需使用Split
检索每一行,然后将它们分配到所需的文本框。在C#中它将是:
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
我的VB生锈了,但我相信它会是这样的:
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)