I have a RTB in VB.NET, and put an event handler to save the formatted text into a file after encrypting it. However, I can't figure out how to reload the formatting - when I open it, it shows the symbols of formatting instead of formatted text. Here's my code:
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()
The above code is for saving, and the below code is for opening.
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()
Where is the problem?
You need RichTextBox1.SaveFile(SomeStream, RichTextBoxStreamType) I think StreamWriter is stuffing it up.
Oh and seeing as you just gave the world the keys for your encryption you might want to come up with a new one...
Added after comment not proven though.
Replace these two lines in your save routine
Dim sWriter As New StreamWriter(cStream)
sWriter.WriteLine(RichTextBox1.Rtf)
with
RichTextBox1.SaveFile(cStream,RichTextBoxStreamType.RichText);
and get rid of swriter.Close()
I think.
Related
I am writing code for add a password in a pdf (I have an array of byte, not a file in a dir) but i have problem with outputStream (I need another array but protect from password): it is 15 bite and not contains the start pdf. I use a itexsharp 5.5 version, is not young but not very old, how is the error?
thi is my code:
Dim PasswordUser As String = ""
Dim UserPassByte = Encoding.Unicode.GetBytes(PasswordUser)
Dim ownerPassword As String = "1"
Dim ownerPassByte = Encoding.Unicode.GetBytes(ownerPassword)
Dim reader As New PdfReader("c:\tmp\pdfx.pdf")
Dim array As Byte()
Using outputStream As New MemoryStream
Dim PdfStamper As New PdfStamper(reader, outputStream)
PdfStamper.SetEncryption(UserPassByte, ownerPassByte, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128)
PdfStamper.FormFlattening = True
array = outputStream.ToArray()
End Using
I've got the below code on a button click which work great. It adds an image to each existing .pdf and then combines several of the new .pdf's together and creates one .pdf. Again, this part of it works great.
The problem I'm having is now I want to keep the existing code but add some text to each page at the same point in the code where the image gets added. I've read a bunch of examples on how to add text to an existing .pdf but due to my inexperience in the area I cant figure out how to make any of the examples work with my existing code. Using VB.net.
I want to add a simple line of text "Example Of text" and position it on the page (300, 300).
Any help would greatly appreciated.
Dim tempFilename = IO.Path.GetTempFileName()
Dim tempFile As New IO.FileStream(tempFilename, IO.FileMode.Create)
' Set up iTextSharp document to hold merged PDF
Dim mergedDocument As New iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER)
Dim copier As New iTextSharp.text.pdf.PdfCopy(mergedDocument, tempFile)
mergedDocument.Open()
Dim pic1 As String = "C:\xxx\xxxx\xxx.png"
Using inputPdfStream As IO.Stream = New IO.FileStream(Server.MapPath(".") + "/xxx.pdf", IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
Using inputImageStream As IO.Stream = New IO.FileStream(pic1, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
Using outputPdfStream As IO.Stream = New IO.FileStream(Server.MapPath(".") + "/xxx2.pdf", IO.FileMode.Create, IO.FileAccess.ReadWrite, IO.FileShare.None)
Dim reader1 = New iTextSharp.text.pdf.PdfReader(inputPdfStream)
Dim stamper = New iTextSharp.text.pdf.PdfStamper(reader1, outputPdfStream)
Dim pdfContentByte = stamper.GetOverContent(1)
Dim image__1 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(inputImageStream)
image__1.SetAbsolutePosition(527, 710)
image__1.ScaleAbsolute(60, 60)
pdfContentByte.AddImage(image__1)
stamper.Close()
reader1.Close()
outputPdfStream.Close()
inputImageStream.Close()
inputPdfStream.Close()
outputPdfStream.Dispose()
End Using
End Using
End Using
Dim reader As New iTextSharp.text.pdf.PdfReader(New iTextSharp.text.pdf.RandomAccessFileOrArray(Server.MapPath(".") + "/yyy/xxx3".pdf", True), Nothing)
For pageNum = 1 To reader.NumberOfPages
copier.AddPage(copier.GetImportedPage(reader, pageNum))
Next
PTime = PTime + 1
Loop
mergedDocument.Close()
tempFile.Dispose()
After a lot of trial and error I got it to work by adding the following code.
Dim bf As iTextSharp.text.pdf.BaseFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED)
pdfContentByte.SetColorFill(iTextSharp.text.BaseColor.DARK_GRAY)
pdfContentByte.SetFontAndSize(bf, 8)
pdfContentByte.BeginText()
Dim strX As String = "Here"
pdfContentByte.ShowTextAligned(1, strX, 500, 500, 0)
pdfContentByte.EndText()
I would like to have a list of websites (each on a different line) that the web browser will check when navigated and if it is the current url then it will navigate back. Below is some code that I have currently have created and fails because it doesn't check each line of text and below that is some code that also check text with current text.
Using sr As New StreamReader("website_lists.txt")
Dim lined As String
lined = sr.ReadToEnd()
Dim scanbox As New TextBox
scanbox.Multiline = True
Dim buff As StringBuilder = New StringBuilder
For Each line In lined
If TextBox1.Text.Contains(scanbox.Text) Then
webview.GoBack()
MsgBox("Infected website! For your own sake stay away!")
End If
Next
End Using
End Sub
Some code that kinda does the same thing:
Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
Dim f As FileStream = New FileStream(ListBox1.SelectedItem, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
f = New FileStream(ListBox1.SelectedItem, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
md5.ComputeHash(f)
Dim hash As Byte() = md5.Hash
Dim buff As StringBuilder = New StringBuilder
Dim hashByte As Byte
For Each hashByte In hash
buff.Append(String.Format("{0:X2}", hashByte))
Next
If scanbox.Text.Contains(buff.ToString) Then
Have any suggestions or help? Thanks in advance.
I would suggest File.Readlines... More information here on this method. Also your declaring a new textbox but the text is never set which means the contains wont work... Another suggestion, turn Option Strict On its your friend.
' Loop over lines in file.
For Each line As String In File.ReadLines("yourfile")
If TextBox1.Text.Contains(line) Then
webview.GoBack()
MsgBox("Infected website! For your own sake stay away!")
Exit For
End If
Next
I'm trying to create a program in VB.NET 2010 that has a simple functionality.
It has to compare thousands of MD5 Hashes stored in a text file to current file MD5 Hash that program is automatically calculating while opening the file.
Like a antivirus scanner.
Actually my program use ReadAllText system to add all hashes from text file to a textbox and then it compare them.
Everyting is okay when Md5 database (that text file with hashes) is small but when the file get bigger, my program simply freezes after opening so I decided to use ReadLine instead of ReadAllText.
Now I can't use textbox any longer so give me a way how I can compare them.
I tried a way but it don't seem to work.
Here is my code using ReadLine.
Problem is on If Read contains(buff.tostring) it used to be
If textbox.text contains (buff.toString)
Here is the code
Try
TextBox2.Text = e.FullPath
ListBox3.Items.Add(TextBox2.Text.ToString)
Me.OpenFileDialog1.FileName = ""
Dim reader As StreamReader = New StreamReader("def.txt")
Dim read = reader.ReadLine()
Dim md5 As New MD5CryptoServiceProvider
Dim f As New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
f = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
md5.ComputeHash(f)
Dim hash As Byte() = md5.Hash
Dim buff As New StringBuilder
Dim hashByte As Byte
For Each hashByte In hash
buff.Append(String.Format("{0:X2}", hashByte))
Next
f.Close()
If read.Contains(buff.ToString) Then
Me.OpenFileDialog1.FileName = e.FullPath
Form2.ShowDialog()
End If
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim ex As Exception = exception1
ProjectData.ClearProjectError()
End Try
I would first create functions to split the functionality. It's a lot easier to understand the code afterward.
Store the hashes in a list, this list can then be cached is needed.
Try
TextBox2.Text = e.FullPath
ListBox3.Items.Add(TextBox2.Text.ToString)
Me.OpenFileDialog1.FileName = ""
Dim allHash As List(Of String) = GetAllHash()
Dim curHash As String = GetFileHash(e.FullPath)
If allHash.Contains(curHash) Then
Me.OpenFileDialog1.FileName = e.FullPath
Form2.ShowDialog()
End If
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim ex As Exception = exception1
ProjectData.ClearProjectError()
End Try
Function GetAllHash() As List(Of String)
' Store the data in a list instead
Return System.IO.File.ReadAllLines("def.txt").ToList()
End Function
Function GetFileHash(ByVal filename As String) As String
Dim md5 As New MD5CryptoServiceProvider
' Only open the file once
Dim f As New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
md5.ComputeHash(f)
Dim hash As Byte() = md5.Hash
Dim buff As New StringBuilder
For Each hashByte As Byte In hash
buff.Append(String.Format("{0:X2}", hashByte))
Next
f.Close()
Return buff.ToString()
End Function
I didn't compile this code, this is just a example to show you what can be done.
I've been tasked with making an encryption program and part of the task is to Encrypt/Decrypt a folder.
The encryption works exactly how I want but when I try to decrypt it gives me an error:
"Length of the data to decrypt is invalid".
All I can say I've really changed is that I'm using DES.CreateDecryptor instead of the DES.CreateEncryptor in my encryption code. Though it looks like there's more to it than that for it to work.
Just wondering how I fix this really. I'll leave the relevant portion of my code below.
Dim folderinfo As New DirectoryInfo(folderpath)
For Each File In Directory.GetFiles(folderinfo.FullName)
Dim outputFile As String
outputFile = File
Dim fsInput As New FileStream(File, FileMode.Open, FileAccess.Read)
Dim bytearrayinput(fsInput.Length) As Byte
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
fsInput.Close()
Dim skey As String
skey = Encrypt
Dim fsDecrypted As New FileStream(File, FileMode.Create, FileAccess.Write)
Dim DES As New DESCryptoServiceProvider
DES.Key = ASCIIEncoding.ASCII.GetBytes(skey)
DES.IV = ASCIIEncoding.ASCII.GetBytes(skey)
Dim desdecrypt As ICryptoTransform
desdecrypt = DES.CreateDecryptor()
Dim cryptostream As New CryptoStream(fsDecrypted, desdecrypt, CryptoStreamMode.Write)
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
cryptostream.Close()
fsDecrypted.Close()
txtDecrypt.Text = "All files decrypted"
You've done a classic mistake when instantiating the array. An array should be instantiated with the size (n - 1) where n is the number of elements. To set the right size, all you have to do is to subtract 1 from the fsInput length.
Dim bytearrayinput(fsInput.Length - 1) As Byte