P
Paulywog0667
Guest
I am having an issue with the best approach. I am able to populate a textbox with what seems actual content from the SQL query. I included the class and two button commands. Basically I run text through a encrypt and save to SQL. The decrypt coming out is flagging bad data though.
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim full_file_name As String
full_file_name = TextBox1.Text
Dim plaintext As String = My.Computer.FileSystem.ReadAllText("C:/TestFile/sample.docx")
Dim password As String = "112913"
Dim wrapper As New Simple3Des(password)
Dim ciphertext As String = wrapper.EncryptData(plaintext)
Dim conn As SqlConnection = New SqlConnection("Server=localhost;Database=test_streams;Trusted_Connection=True;")
Dim cmd5 As SqlCommand = New SqlCommand
cmd5.Connection = conn
cmd5.CommandText = "INSERT INTO tablefiles (rename, datafill) VALUES (@re, @dat)"
With cmd5.Parameters
.AddWithValue("@re", TextBox3.Text)
.AddWithValue("@dat", ciphertext)
.AddWithValue("@file", full_file_name)
.AddWithValue("@desc", TextBox2.Text)
End With
conn.Open()
cmd5.ExecuteNonQuery()
conn.Close()
TextBox1.Clear()
FetchFiledescriptions()
MessageBox.Show("File Saved!")
End Sub
'/////////////////////////////////
Public Sub writefiletext()
Dim SQLDA112913 As SqlDataAdapter
Dim querystring As String = "SELECT (datafill) FROM tablefiles WHERE rename ='" & TextBox4.Text & "'"
Dim connection As New SqlConnection("Server=localhost;Database=test_streams;Trusted_Connection=True;")
connection.Open()
Dim command As New SqlCommand(querystring, connection)
' Dim reader4 As SqlDataReader = command.ExecuteReader()
SQLDA112913 = New SqlDataAdapter(command)
Dim SQLDS112913 = New DataSet
If SQLDS112913 IsNot Nothing Then
SQLDS112913.Clear()
End If
SQLDA112913.Fill(SQLDS112913)
For Each i As Object In SQLDS112913.Tables(0).Rows
TextBox5.Text = i.Item("datafill")
Next
' connection.Close()
Dim ciphertext As String = TextBox5.Text
Dim password As String = "112913"
Dim wrapper As New Simple3Des(ciphertext)
Dim plaintext As String = wrapper.DecryptData(ciphertext)
Dim sample As XDocument =
<?xml version="1.0"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument
xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
<w:body>
<w>
<w:r>
<w:t><%= plaintext %></w:t>
</w:r>
</w>
</w:body>
</w:wordDocument>
sample.Save("C:\TestFile\test10.xml")
End Sub
'//////////////////////////
Imports System.Security.Cryptography
Public NotInheritable Class Simple3Des
Private TripleDes As New TripleDESCryptoServiceProvider
Private Function TruncateHash(
ByVal key As String,
ByVal length As Integer) As Byte()
Dim sha1 As New SHA1CryptoServiceProvider
' Hash the key.
Dim keyBytes() As Byte =
System.Text.Encoding.Unicode.GetBytes(key)
Dim hash() As Byte = sha1.ComputeHash(keyBytes)
' Truncate or pad the hash.
ReDim Preserve hash(length - 1)
Return hash
End Function
Sub New(ByVal key As String)
' Initialize the crypto provider.
TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8)
TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8)
End Sub
Public Function EncryptData(
ByVal plaintext As String) As String
' Convert the plaintext string to a byte array.
Dim plaintextBytes() As Byte =
System.Text.Encoding.Unicode.GetBytes(plaintext)
' Create the stream.
Dim ms As New System.IO.MemoryStream
' Create the encoder to write to the stream.
Dim encStream As New CryptoStream(ms,
TripleDes.CreateEncryptor(),
System.Security.Cryptography.CryptoStreamMode.Write)
' Use the crypto stream to write the byte array to the stream.
encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
encStream.FlushFinalBlock()
' Convert the encrypted stream to a printable string.
Return Convert.ToBase64String(ms.ToArray)
End Function
Public Function DecryptData(
ByVal encryptedtext As String) As String
' Convert the encrypted text string to a byte array.
Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)
' Create the stream.
Dim ms As New System.IO.MemoryStream
' Create the decoder to write to the stream.
Dim decStream As New CryptoStream(ms,
TripleDes.CreateDecryptor(),
System.Security.Cryptography.CryptoStreamMode.Write)
' Use the crypto stream to write the byte array to the stream.
decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
decStream.FlushFinalBlock()
' Convert the plaintext stream to a string.
Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
End Function
End Class
Continue reading...
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim full_file_name As String
full_file_name = TextBox1.Text
Dim plaintext As String = My.Computer.FileSystem.ReadAllText("C:/TestFile/sample.docx")
Dim password As String = "112913"
Dim wrapper As New Simple3Des(password)
Dim ciphertext As String = wrapper.EncryptData(plaintext)
Dim conn As SqlConnection = New SqlConnection("Server=localhost;Database=test_streams;Trusted_Connection=True;")
Dim cmd5 As SqlCommand = New SqlCommand
cmd5.Connection = conn
cmd5.CommandText = "INSERT INTO tablefiles (rename, datafill) VALUES (@re, @dat)"
With cmd5.Parameters
.AddWithValue("@re", TextBox3.Text)
.AddWithValue("@dat", ciphertext)
.AddWithValue("@file", full_file_name)
.AddWithValue("@desc", TextBox2.Text)
End With
conn.Open()
cmd5.ExecuteNonQuery()
conn.Close()
TextBox1.Clear()
FetchFiledescriptions()
MessageBox.Show("File Saved!")
End Sub
'/////////////////////////////////
Public Sub writefiletext()
Dim SQLDA112913 As SqlDataAdapter
Dim querystring As String = "SELECT (datafill) FROM tablefiles WHERE rename ='" & TextBox4.Text & "'"
Dim connection As New SqlConnection("Server=localhost;Database=test_streams;Trusted_Connection=True;")
connection.Open()
Dim command As New SqlCommand(querystring, connection)
' Dim reader4 As SqlDataReader = command.ExecuteReader()
SQLDA112913 = New SqlDataAdapter(command)
Dim SQLDS112913 = New DataSet
If SQLDS112913 IsNot Nothing Then
SQLDS112913.Clear()
End If
SQLDA112913.Fill(SQLDS112913)
For Each i As Object In SQLDS112913.Tables(0).Rows
TextBox5.Text = i.Item("datafill")
Next
' connection.Close()
Dim ciphertext As String = TextBox5.Text
Dim password As String = "112913"
Dim wrapper As New Simple3Des(ciphertext)
Dim plaintext As String = wrapper.DecryptData(ciphertext)
Dim sample As XDocument =
<?xml version="1.0"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument
xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
<w:body>
<w>
<w:r>
<w:t><%= plaintext %></w:t>
</w:r>
</w>
</w:body>
</w:wordDocument>
sample.Save("C:\TestFile\test10.xml")
End Sub
'//////////////////////////
Imports System.Security.Cryptography
Public NotInheritable Class Simple3Des
Private TripleDes As New TripleDESCryptoServiceProvider
Private Function TruncateHash(
ByVal key As String,
ByVal length As Integer) As Byte()
Dim sha1 As New SHA1CryptoServiceProvider
' Hash the key.
Dim keyBytes() As Byte =
System.Text.Encoding.Unicode.GetBytes(key)
Dim hash() As Byte = sha1.ComputeHash(keyBytes)
' Truncate or pad the hash.
ReDim Preserve hash(length - 1)
Return hash
End Function
Sub New(ByVal key As String)
' Initialize the crypto provider.
TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8)
TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8)
End Sub
Public Function EncryptData(
ByVal plaintext As String) As String
' Convert the plaintext string to a byte array.
Dim plaintextBytes() As Byte =
System.Text.Encoding.Unicode.GetBytes(plaintext)
' Create the stream.
Dim ms As New System.IO.MemoryStream
' Create the encoder to write to the stream.
Dim encStream As New CryptoStream(ms,
TripleDes.CreateEncryptor(),
System.Security.Cryptography.CryptoStreamMode.Write)
' Use the crypto stream to write the byte array to the stream.
encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
encStream.FlushFinalBlock()
' Convert the encrypted stream to a printable string.
Return Convert.ToBase64String(ms.ToArray)
End Function
Public Function DecryptData(
ByVal encryptedtext As String) As String
' Convert the encrypted text string to a byte array.
Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)
' Create the stream.
Dim ms As New System.IO.MemoryStream
' Create the decoder to write to the stream.
Dim decStream As New CryptoStream(ms,
TripleDes.CreateDecryptor(),
System.Security.Cryptography.CryptoStreamMode.Write)
' Use the crypto stream to write the byte array to the stream.
decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
decStream.FlushFinalBlock()
' Convert the plaintext stream to a string.
Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
End Function
End Class
Continue reading...