0

我在 VB.NET 中有一个 RTB,并在加密后放置一个事件处理程序以将格式化文本保存到文件中。但是,我不知道如何重新加载格式 - 当我打开它时,它显示格式的符号而不是格式化的文本。这是我的代码:

Dim FileName As String = TextBox1.Text
File.Delete(FileName)
Dim EncryptElement As New TripleDESCryptoServiceProvider
            EncryptElement.Key = {AscW("B"c), AscW("A"c), AscW("1"c), AscW("R"c), AscW("3"c), AscW("9"c), AscW("G"c), AscW("V"c), AscW("5"c), AscW("S"c), AscW("P"c), AscW("0"c), AscW("L"c), AscW("Z"c), AscW("4"c), AscW("M"c)} '128 bit Key
            EncryptElement.IV = {AscW("N"c), AscW("B"c), AscW("5"c), AscW("3"c), AscW("G"c), AscW("L"c), AscW("2"c), AscW("Q"c)} ' 64 bit Initialization Vector

Dim fStream As FileStream = File.Open(FileName, FileMode.OpenOrCreate)
Dim cStream As New CryptoStream(fStream, New TripleDESCryptoServiceProvider().CreateEncryptor(EncryptElement.Key, EncryptElement.IV), CryptoStreamMode.Write)

Dim sWriter As New StreamWriter(cStream)

sWriter.WriteLine(RichTextBox1.Rtf)

sWriter.Close()
cStream.Close()
fStream.Close()

上面的代码是用来保存的,下面的代码是用来打开的。

Dim FileName As String = TextBox1.Text
Dim DecryptElement As New TripleDESCryptoServiceProvider
            DecryptElement.Key = {AscW("B"c), AscW("A"c), AscW("1"c), AscW("R"c), AscW("3"c), AscW("9"c), AscW("G"c), AscW("V"c), AscW("5"c), AscW("S"c), AscW("P"c), AscW("0"c), AscW("L"c), AscW("Z"c), AscW("4"c), AscW("M"c)}
            DecryptElement.IV = {AscW("N"c), AscW("B"c), AscW("5"c), AscW("3"c), AscW("G"c), AscW("L"c), AscW("2"c), AscW("Q"c)}

 Dim fStream As FileStream = File.Open(FileName, FileMode.OpenOrCreate)

 Dim cStream As New CryptoStream(fStream, New TripleDESCryptoServiceProvider().CreateDecryptor(DecryptElement.Key, DecryptElement.IV), CryptoStreamMode.Read)

 Dim sReader As New StreamReader(cStream)

 Dim DecryptedData As String = ""

 DecryptedData = sReader.ReadToEnd

 RichTextBox1.AppendText(DecryptedData)
 RichTextBox1.Enabled = True
 Button1.Text = "OK"

 sReader.Close()
 cStream.Close()
 fStream.Close()

问题出在哪里?

4

1 回答 1

0

您需要 RichTextBox1.SaveFile(SomeStream, RichTextBoxStreamType) 我认为 StreamWriter 正在填充它。

哦,看到您刚刚向世界提供了加密密钥,您可能想想出一个新的...

在未证实的评论后添加。

在您的保存例程中替换这两行

Dim sWriter As New StreamWriter(cStream)
sWriter.WriteLine(RichTextBox1.Rtf)

RichTextBox1.SaveFile(cStream,RichTextBoxStreamType.RichText);

并摆脱swriter.Close()

我认为。

于 2013-08-24T09:09:46.567 回答