P
Phillip Ian
Version: VS 2005
I took the sample code from help about encrypting and decrypting
strings, and changed it to work directly with byte arrays and get the
key and IV values from functions I've written to privide them. No
other changes were made...
Friend Function EncryptBytes(ByVal Data() As Byte, ByVal APassphrase
As String) As Byte()
Try
' Create a MemoryStream.
Dim mStream As New MemoryStream
' Create a CryptoStream using the MemoryStream
' and the passed key and initialization vector (IV).
Dim cStream As New CryptoStream(mStream, New
TripleDESCryptoServiceProvider().CreateEncryptor(GetKey(APassphrase),
GetIV(APassphrase)), CryptoStreamMode.Write)
' Write the byte array to the crypto stream and flush it.
cStream.Write(Data, 0, Data.Length)
cStream.FlushFinalBlock()
' Get an array of bytes from the
' MemoryStream that holds the
' encrypted data.
Dim ret As Byte() = mStream.ToArray()
' Close the streams.
cStream.Close()
mStream.Close()
' Return the encrypted buffer.
Return ret
Catch e As CryptographicException
Console.WriteLine("A Cryptographic error occurred: " & e.Message)
Return Nothing
End Try
End Function
Friend Function DecryptBytes(ByVal Data() As Byte, ByVal APassphrase
As String) As Byte()
Try
' Create a new MemoryStream using the passed
' array of encrypted data.
Dim msDecrypt As New MemoryStream(Data)
' Create a CryptoStream using the MemoryStream
' and the passed key and initialization vector (IV).
Dim csDecrypt As New CryptoStream(msDecrypt, New
TripleDESCryptoServiceProvider().CreateDecryptor(GetKey(APassphrase),
GetIV(APassphrase)), CryptoStreamMode.Read)
' Create buffer to hold the decrypted data.
Dim fromEncrypt(Data.Length) As Byte
' Read the decrypted data out of the crypto stream
' and place it into the temporary buffer.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)
'Convert the buffer into a string and return it.
Return fromEncrypt
Catch e As CryptographicException
Console.WriteLine("A Cryptographic error occurred: " & e.Message)
Return Nothing
End Try
End Function
When I encrypt and decrypt a file, the file ends up with extra blank
bytes on the end of it. My encrypt/decrypt routine is as follows:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim fin As New FileStream("test.jpg", FileMode.Open)
Dim mugshot(fin.Length) As Byte
fin.Read(mugshot, 0, fin.Length)
fin.Close()
MessageBox.Show(mugshot.Length)
Dim encrypted() As Byte
encrypted = EncryptBytes(mugshot, "This is a test...")
MessageBox.Show(encrypted.Length)
Dim decrypted() As Byte
decrypted = DecryptBytes(encrypted, "This is a test...")
MessageBox.Show(decrypted.Length)
Dim fout As New FileStream("testout.jpg", FileMode.Create)
fout.Write(decrypted, 0, decrypted.Length)
fout.Close()
End Sub
This doesn't seem to bother the jpeg files...they read just fine. But
what if it WASN'T a jpeg? Is it the encrypt/decrypt routine or the
file IO stuff that's adding the extra bytes?
Any help appreciated.
I took the sample code from help about encrypting and decrypting
strings, and changed it to work directly with byte arrays and get the
key and IV values from functions I've written to privide them. No
other changes were made...
Friend Function EncryptBytes(ByVal Data() As Byte, ByVal APassphrase
As String) As Byte()
Try
' Create a MemoryStream.
Dim mStream As New MemoryStream
' Create a CryptoStream using the MemoryStream
' and the passed key and initialization vector (IV).
Dim cStream As New CryptoStream(mStream, New
TripleDESCryptoServiceProvider().CreateEncryptor(GetKey(APassphrase),
GetIV(APassphrase)), CryptoStreamMode.Write)
' Write the byte array to the crypto stream and flush it.
cStream.Write(Data, 0, Data.Length)
cStream.FlushFinalBlock()
' Get an array of bytes from the
' MemoryStream that holds the
' encrypted data.
Dim ret As Byte() = mStream.ToArray()
' Close the streams.
cStream.Close()
mStream.Close()
' Return the encrypted buffer.
Return ret
Catch e As CryptographicException
Console.WriteLine("A Cryptographic error occurred: " & e.Message)
Return Nothing
End Try
End Function
Friend Function DecryptBytes(ByVal Data() As Byte, ByVal APassphrase
As String) As Byte()
Try
' Create a new MemoryStream using the passed
' array of encrypted data.
Dim msDecrypt As New MemoryStream(Data)
' Create a CryptoStream using the MemoryStream
' and the passed key and initialization vector (IV).
Dim csDecrypt As New CryptoStream(msDecrypt, New
TripleDESCryptoServiceProvider().CreateDecryptor(GetKey(APassphrase),
GetIV(APassphrase)), CryptoStreamMode.Read)
' Create buffer to hold the decrypted data.
Dim fromEncrypt(Data.Length) As Byte
' Read the decrypted data out of the crypto stream
' and place it into the temporary buffer.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)
'Convert the buffer into a string and return it.
Return fromEncrypt
Catch e As CryptographicException
Console.WriteLine("A Cryptographic error occurred: " & e.Message)
Return Nothing
End Try
End Function
When I encrypt and decrypt a file, the file ends up with extra blank
bytes on the end of it. My encrypt/decrypt routine is as follows:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim fin As New FileStream("test.jpg", FileMode.Open)
Dim mugshot(fin.Length) As Byte
fin.Read(mugshot, 0, fin.Length)
fin.Close()
MessageBox.Show(mugshot.Length)
Dim encrypted() As Byte
encrypted = EncryptBytes(mugshot, "This is a test...")
MessageBox.Show(encrypted.Length)
Dim decrypted() As Byte
decrypted = DecryptBytes(encrypted, "This is a test...")
MessageBox.Show(decrypted.Length)
Dim fout As New FileStream("testout.jpg", FileMode.Create)
fout.Write(decrypted, 0, decrypted.Length)
fout.Close()
End Sub
This doesn't seem to bother the jpeg files...they read just fine. But
what if it WASN'T a jpeg? Is it the encrypt/decrypt routine or the
file IO stuff that's adding the extra bytes?
Any help appreciated.