View Full Version : VB encryption program problem
Th3Doc7or
12-19-2009, 06:21 AM
Hey guys, im pretty new to visual basic (started earlier this year) but i put together a basic encryption program using an ASCII encryption method.
Encryption works perfectly for encrypting and decrypting plain text files.
When i try to encrypt something like a wmv file it encrypts fine but when it decrypts the file size increases and the file becomes corrputed.
Is there any reason for this? I would have though because it uses non-standard symbols when you view it as text.
If you want i can paste the code im using
thanks
HazXod3d
12-19-2009, 12:26 PM
Hey guys, im pretty new to visual basic (started earlier this year) but i put together a basic encryption program using an ASCII encryption method.
Encryption works perfectly for encrypting and decrypting plain text files.
When i try to encrypt something like a wmv file it encrypts fine but when it decrypts the file size increases and the file becomes corrputed.
Is there any reason for this? I would have though because it uses non-standard symbols when you view it as text.
If you want i can paste the code im using
thanks
Try another encrypting method. or make a own crypting method xD
th4natos
12-19-2009, 09:06 PM
It would help to see your code. How do you expect us to help you
Th3Doc7or
12-19-2009, 11:01 PM
It would help to see your code. How do you expect us to help you
My code for encrypting/decrypting
Sub EncryptFile(ByVal sInputFilename As String, _
ByVal sOutputFilename As String, _
ByVal sKey As String)
Try
If Form1.TextBox2.Text <> "" Then
Dim fsInput As New FileStream(sInputFilename, _
FileMode.Open, FileAccess.Read)
Dim fsEncrypted As New FileStream(sOutputFilename, _
FileMode.Create, FileAccess.Write)
Dim DES As New DESCryptoServiceProvider()
'Set secret key for DES algorithm.
'A 64-bit key and an IV are required for this provider.
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
'Set the initialization vector.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
'Create the DES encryptor from this instance.
Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
'Create the crypto stream that transforms the file stream by using DES encryption.
Dim cryptostream As New CryptoStream(fsEncrypted, _
desencrypt, _
CryptoStreamMode.Write)
'Read the file text to the byte array.
Dim bytearrayinput(fsInput.Length - 1) As Byte
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
'Write out the DES encrypted file.
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
cryptostream.Close()
fsEncrypted.Close()
fsInput.Close()
End If
Catch ex As ArgumentNullException
MessageBox.Show("A null argument was specified")
Catch ex As ArgumentException
MessageBox.Show("Specified pathname contains invalid characters")
Catch ex As DriveNotFoundException
MessageBox.Show("Invalid drive")
Catch ex As FileNotFoundException
MessageBox.Show("File not found")
Catch ex As PathTooLongException
MessageBox.Show("Pathname too long")
Catch ex As IOException
MessageBox.Show("I/O error")
Catch ex As PlatformNotSupportedException
MessageBox.Show("You're trying to execute this " & _
"action onto a non Windows NT operating system")
Catch ex As NotSupportedException
MessageBox.Show("Current file system is not NTFS")
Catch ex As UnauthorizedAccessException
MessageBox.Show("You're not authorized to execute the selected action")
End Try
End Sub
Sub DecryptFile(ByVal sInputFilename As String, _
ByVal sOutputFilename As String, _
ByVal sKey As String)
Try
If Form1.TextBox3.Text <> "" Then
Dim DES As New DESCryptoServiceProvider()
'A 64-bit key and an IV are required for this provider.
'Set the secret key for the DES algorithm.
DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
'Set the initialization vector.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
'Create the file stream to read the encrypted file back.
Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
'Create the DES decryptor from the DES instance.
Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
'Create the crypto stream set to read and to do a DES decryption transform on incoming bytes.
Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
'Print out the contents of the decrypted file.
Dim fsDecrypted As New StreamWriter(sOutputFilename)
fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
fsDecrypted.Flush()
fsDecrypted.Close()
fsread.Close()
End If
Catch ex As ArgumentNullException
MessageBox.Show("A null argument was specified")
Catch ex As ArgumentException
MessageBox.Show("Specified pathname contains invalid characters")
Catch ex As DriveNotFoundException
MessageBox.Show("Invalid drive")
Catch ex As FileNotFoundException
MessageBox.Show("File not found")
Catch ex As PathTooLongException
MessageBox.Show("Pathname too long")
Catch ex As IOException
MessageBox.Show("I/O error")
Catch ex As PlatformNotSupportedException
MessageBox.Show("You're trying to execute this " & _
"action onto a non Windows NT operating system")
Catch ex As NotSupportedException
MessageBox.Show("Current file system is not NTFS")
Catch ex As UnauthorizedAccessException
MessageBox.Show("You're not authorized to execute the selected action")
End Try
End Sub
Powered by vBulletin® Version 4.1.12 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.