How to make Rijndael CBC mode work in vb.net - vb.net

I'm trying to make rijndael work in CBC mode. I'm not exactly sure how should I do it. I think problem in my current code is that the stream is initialized every time in the beginning of encryption, so no avalanche effect occurs (same data is encrypted twice and the output of those two encryption is the same which it should not be).
I tried to initialize the cryptostream only once but then my coded crashed because the canwrite property of cryptostream goes to false after the first write to the cryptostream.
Here is the code what I have now:
Sub Main()
Dim rij As New RijndaelManaged
Dim iv(15) As Byte
Dim key(15) As Byte
Dim secret() As Byte = {59, 60, 61}
Dim cs As ICryptoTransform
Dim cstream As CryptoStream
Dim out() As Byte
Dim NewRandom As New RNGCryptoServiceProvider()
NewRandom.GetBytes(iv)
NewRandom.GetBytes(key)
rij = New RijndaelManaged()
rij.KeySize = 128
rij.Padding = PaddingMode.PKCS7
rij.Mode = CipherMode.CBC
rij.IV = iv
rij.Key = key
cs = rij.CreateEncryptor()
Dim ms_in As New MemoryStream
cstream = New CryptoStream(ms_in, cs, CryptoStreamMode.Write)
Using cstream
cstream.Write(secret, 0, 3)
End Using
out = ms_in.ToArray
Console.WriteLine(ArrayToString(out, out.Length))
Erase out
ms_in = New MemoryStream
cstream = New CryptoStream(ms_in, cs, CryptoStreamMode.Write)
Using cstream
cstream.Write(secret, 0, 3)
End Using
out = ms_in.ToArray
Console.WriteLine(ArrayToString(out, out.Length))
End Sub
and the conversion function to convert an array to string
Public Function ArrayToString(ByVal bytes() As Byte, ByVal length As Integer) As String
If bytes.Length = 0 Then Return String.Empty
Dim sb As New System.Text.StringBuilder(length)
Dim k As Integer = length - 1
Dim i As Integer
For i = 0 To k
sb.Append(Chr(bytes(i)))
Next
Return sb.ToString()
End Function
This is what I need:
cs = rij.CreateEncryptor()
Dim ms_in As New MemoryStream
cstream = New CryptoStream(ms_in, cs, CryptoStreamMode.Write)
Using cstream
cstream.Write(secret, 0, 3) 'encrypt
End Using
out = ms_in.ToArray
Console.WriteLine(ArrayToString(out, out.Length)) 'see the encrypted message
Erase out
Using cstream
cstream.Write(secret, 0, 3) 'encrypt, this will crash here and this is the problem I'm trying to solve
End Using
out = ms_in.ToArray
Console.WriteLine(ArrayToString(out, out.Length)) 'see the encrypted message this should not be the same as the first one

Try this:
Public Sub Run()
Dim key() As Byte = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
Dim plaintext1 As Byte() = {59, 60, 61}
Dim plaintext2 As Byte() = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, _
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, _
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, _
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 _
}
Roundtrip(plaintext1, key)
System.Console.WriteLine()
Roundtrip(plaintext2, key)
End Sub
Public Sub Roundtrip(ByRef plaintext As Byte(), ByRef key As Byte())
Dim rij As New RijndaelManaged
Dim iv(15) As Byte
Dim encryptor As ICryptoTransform
Dim decryptor As ICryptoTransform
Dim out() As Byte
'Dim NewRandom As New RNGCryptoServiceProvider()
'NewRandom.GetBytes(iv)
'NewRandom.GetBytes(key)
Console.WriteLine("Original:")
Console.WriteLine(ArrayToString(plaintext))
System.Console.WriteLine()
rij = New RijndaelManaged()
rij.KeySize = key.Length * 8 ' 16 byte key == 128 bits
rij.Padding = PaddingMode.PKCS7
rij.Mode = CipherMode.CBC
rij.IV = iv
rij.Key = key
encryptor = rij.CreateEncryptor()
Using msIn = New MemoryStream
Using cstream = New CryptoStream(msIn, encryptor, CryptoStreamMode.Write)
cstream.Write(plaintext, 0, plaintext.Length)
End Using
out = msIn.ToArray
Console.WriteLine("Encrypted:")
Console.WriteLine("{0}", ArrayToString(out))
System.Console.WriteLine()
End Using
decryptor = rij.CreateDecryptor()
Using msIn = New MemoryStream
Using cstream = New CryptoStream(msIn, decryptor, CryptoStreamMode.Write)
cstream.Write(out, 0, out.Length)
End Using
out = msIn.ToArray
Console.WriteLine("Decrypted: ")
Console.WriteLine("{0}", ArrayToString(out))
System.Console.WriteLine()
End Using
End Sub
Public Shared Function ArrayToString(ByVal bytes As Byte()) As String
Dim sb As New System.Text.StringBuilder()
Dim i As Integer
For i = 0 To bytes.Length-1
if (i <> 0 AND i mod 16 = 0) Then
sb.Append(Environment.NewLine)
End If
sb.Append(System.String.Format("{0:X2} ", bytes(i)))
Next
Return sb.ToString().Trim()
End Function
I made these basic changes to get it to work:
create a Decryptor
properly manage buffers and streams (see the Using clauses I added)
I also re-organized a little, and modified your code slightly to use a constant IV (all zeros) and use a constant key. This is so that you can get repeatable results from one run to the next. In a real app you would use a randomized IV and use a password-derived key. (See Rfc2898DeriveBytes)
ok, that allows the code to compile. I think you want to see the effect of chaining. That is not so easy, but maybe something like this will show you what you want to see:
For i As Integer = 1 To 2
Using ms = New MemoryStream
Using cstream = New CryptoStream(ms, encryptor, CryptoStreamMode.Write)
For j As Integer = 1 To i
cstream.Write(plaintext, 0, plaintext.Length)
Next j
End Using
out = ms.ToArray
Console.WriteLine("Encrypted (cycle {0}):", i)
Console.WriteLine("{0}", ArrayToString(out))
System.Console.WriteLine()
End Using
Next i

Related

Rijndael Encryption and Decryption does not work on another computer

First of all, if i do encryption and decryption on the same computer, it works fine. Bu when i want to decrypt a file which encrypted on another computer with same key, it does not work. Error tells 'Parameter is invalid'. Here are the defines;
Dim cspp As New CspParameters
Dim rsa As New RSACryptoServiceProvider
Dim bVis As New baseVisitor
Dim encDec As New EncryptDecrypt
Public encFolder As String = ""
Public decFolder As String = ""
Public keyName As String = ""
Here is how can i define key;
Public Function GetKey() As Boolean
Dim sonuc As Boolean = False
Try
keyName = "keyName25*-"
If keyName.Length > 4 Then
cspp.KeyContainerName = keyName
rsa = New RSACryptoServiceProvider(cspp)
rsa.PersistKeyInCsp = True
sonuc = True
End If
Catch ex As Exception
sonuc = False
End Try
Return sonuc
End Function
After getting key here is the encryption code;
Public Sub EncryptFile(ByVal decFile As String)
Dim rjndl As RijndaelManaged = New RijndaelManaged()
rjndl.KeySize = 256
rjndl.BlockSize = 256
rjndl.Mode = CipherMode.CBC
Dim transform As ICryptoTransform = rjndl.CreateEncryptor()
Dim keyEncrypted As Byte() = rsa.Encrypt(rjndl.Key, False)
Dim LenK As Byte() = New Byte(3) {}
Dim LenIV As Byte() = New Byte(3) {}
Dim lKey As Integer = keyEncrypted.Length
LenK = BitConverter.GetBytes(lKey)
Dim lIV As Integer = rjndl.IV.Length
LenIV = BitConverter.GetBytes(lIV)
Dim startFileName As Integer = decFile.LastIndexOf("\") + 1
Dim outFile As String = decFile
Using outFs As FileStream = New FileStream(outFile, FileMode.Create)
outFs.Write(LenK, 0, 4)
outFs.Write(LenIV, 0, 4)
outFs.Write(keyEncrypted, 0, lKey)
outFs.Write(rjndl.IV, 0, lIV)
Using outStreamEncrypted As CryptoStream = New CryptoStream(outFs, transform, CryptoStreamMode.Write)
Dim count As Integer = 0
Dim offset As Integer = 0
Dim blockSizeBytes As Integer = rjndl.BlockSize / 8
Dim data As Byte() = New Byte(blockSizeBytes - 1) {}
Dim bytesRead As Integer = 0
Using inFs As FileStream = New FileStream(decFile, FileMode.Open)
Do
count = inFs.Read(data, 0, blockSizeBytes)
offset += count
outStreamEncrypted.Write(data, 0, count)
bytesRead += blockSizeBytes
Loop While count > 0
inFs.Close()
End Using
outStreamEncrypted.FlushFinalBlock()
outStreamEncrypted.Close()
End Using
outFs.Close()
End Using
End Sub
Encryption works for all computers. But Decription code which is below is the problem;
Public Sub DecryptFile(ByVal enFile As String)
Dim rjndl As RijndaelManaged = New RijndaelManaged()
rjndl.KeySize = 256
rjndl.BlockSize = 256
rjndl.Mode = CipherMode.CBC
Dim LenK As Byte() = New Byte(3) {}
Dim LenIV As Byte() = New Byte(3) {}
Dim outFile As String = enFile
Using inFs As FileStream = New FileStream(enFile, FileMode.Open)
inFs.Seek(0, SeekOrigin.Begin)
inFs.Seek(0, SeekOrigin.Begin)
inFs.Read(LenK, 0, 3)
inFs.Seek(4, SeekOrigin.Begin)
inFs.Read(LenIV, 0, 3)
Dim lenK32 As Integer = BitConverter.ToInt32(LenK, 0)
Dim lenIV32 As Integer = BitConverter.ToInt32(LenIV, 0)
Dim startC As Integer = lenK32 + lenIV32 + 8
Dim lenC As Integer = CInt(inFs.Length) - startC
Dim KeyEncrypted As Byte() = New Byte(lenK32 - 1) {}
Dim IV As Byte() = New Byte(lenIV32 - 1) {}
inFs.Seek(8, SeekOrigin.Begin)
inFs.Read(KeyEncrypted, 0, lenK32)
inFs.Seek(8 + lenK32, SeekOrigin.Begin)
inFs.Read(IV, 0, lenIV32)
Dim KeyDecrypted As Byte() = rsa.Decrypt(KeyEncrypted, False)
Dim transform As ICryptoTransform = rjndl.CreateDecryptor(KeyDecrypted, IV)
Using outFs As FileStream = New FileStream(outFile, FileMode.Create)
Dim count As Integer = 0
Dim offset As Integer = 0
Dim blockSizeBytes As Integer = rjndl.BlockSize / 8
Dim data As Byte() = New Byte(blockSizeBytes - 1) {}
inFs.Seek(startC, SeekOrigin.Begin)
Using outStreamDecrypted As CryptoStream = New CryptoStream(outFs, transform, CryptoStreamMode.Write)
Do
count = inFs.Read(data, 0, blockSizeBytes)
offset += count
outStreamDecrypted.Write(data, 0, count)
Loop While count > 0
outStreamDecrypted.FlushFinalBlock()
outStreamDecrypted.Close()
End Using
outFs.Close()
End Using
inFs.Close()
End Using
End Sub
Using same key must solve the problem but it error all the time. I searched everywhere. Is there anybody to help me?
NOTE : I just want to encrypt-decrypt jpg files

VB.Net Encryption Function Not Decrypting Without Using Base64Encode

I am having a issue with decrypting, my goal is to be able to encrypt/decrypt with/without base64 encoding on the encrypted string. As of now I can encrypt/decrypt with base64 and encrypt without it but not decrypt without it. I get errors regarding the padding being incorrect.
Thanks in advance!
Here is my encryption/decryption function:
Public Function DoCryptWork(Type As String, Data As String) As String
Dim Pass As String = Hasher.TextBoxPassword.Text
Dim Salt As String = Hasher.TextBoxSalt.Text
Dim Vect As String = Hasher.TextBoxIntVector.Text
Select Case Type
Case "e"
Try
Dim PassPhrase As String = Pass
Dim SaltValue As String = Salt
Dim HashAlgorithm As String = My.Settings.HashAlgorithm
Dim PasswordIterations As Integer = 2
Dim InitVector As String = Vect
Dim KeySize As Integer = 256
Dim InitVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitVector)
Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(SaltValue)
Dim PlainTextBytes As Byte() = Encoding.UTF8.GetBytes(Data)
Dim Password As New PasswordDeriveBytes(PassPhrase, SaltValueBytes, HashAlgorithm, PasswordIterations)
Dim KeyBytes As Byte() = Password.GetBytes(KeySize \ 8)
Dim SymmetricKey As New RijndaelManaged()
SymmetricKey.Mode = CipherMode.CBC
Dim Encryptor As ICryptoTransform = SymmetricKey.CreateEncryptor(KeyBytes, InitVectorBytes)
Dim MemoryStream As New MemoryStream()
Dim CryptoStream As New CryptoStream(MemoryStream, Encryptor, CryptoStreamMode.Write)
CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length)
CryptoStream.FlushFinalBlock()
Dim CipherTextBytes As Byte() = MemoryStream.ToArray()
MemoryStream.Close()
CryptoStream.Close()
Dim CipherText As String = Nothing
If My.Settings.Base64EncodeMD5Hash = True Then
CipherText = Convert.ToBase64String(CipherTextBytes)
Return CipherText
Else
Dim TextCipher As New StringBuilder()
For n As Integer = 0 To CipherTextBytes.Length - 1
TextCipher.Append(CipherTextBytes(n).ToString("X2"))
Next n
CipherText = TextCipher.ToString()
Return CipherText
End If
Catch ex As Exception
MsgBox("Encryption was unsuccessfull!", MsgBoxStyle.Critical, "Error")
Return "Encryption was unsuccessfull!"
End Try
Case "d"
Try
Dim PassPhrase As String = Pass
Dim SaltValue As String = Salt
Dim HashAlgorithm As String = My.Settings.HashAlgorithm
Dim PasswordIterations As Integer = 2
Dim InitVector As String = Vect
Dim KeySize As Integer = 256
Dim InitVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitVector)
Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(SaltValue)
Dim CipherTextBytes As Byte() = Nothing
If My.Settings.Base64EncodeMD5Hash = True Then
CipherTextBytes = Convert.FromBase64String(Data)
Else
Dim bytedata As Byte() = Encoding.UTF8.GetBytes(Data)
CipherTextBytes = bytedata
End If
Dim Password As New PasswordDeriveBytes(PassPhrase, SaltValueBytes, HashAlgorithm, PasswordIterations)
Dim KeyBytes As Byte() = Password.GetBytes(KeySize \ 8)
Dim SymmetricKey As New RijndaelManaged()
SymmetricKey.Mode = CipherMode.CBC
Dim Decryptor As ICryptoTransform = SymmetricKey.CreateDecryptor(KeyBytes, InitVectorBytes)
Dim MemoryStream As New MemoryStream(CipherTextBytes)
Dim CryptoStream As New CryptoStream(MemoryStream, Decryptor, CryptoStreamMode.Read)
Dim PlainTextBytes As Byte() = New Byte(CipherTextBytes.Length - 1) {}
Dim DecryptedByteCount As Integer = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length)
MemoryStream.Close()
CryptoStream.Close()
Dim PlainText As String = Encoding.UTF8.GetString(PlainTextBytes, 0, DecryptedByteCount)
Return PlainText
Catch Ex As Exception
MsgBox("Decryption was unsuccessfull!" & vbNewLine & vbNewLine & Ex.ToString(), MsgBoxStyle.Critical, "Error")
Return "Decryption was unsuccessfull!"
End Try
Case Else
Return "Error! Invalid Case Selected We should never see this but just to be safe we'll show this message if the wrong case is selected!"
End Select
Return True
End Function
I call the function as so:
TextBoxOutput.Text = Encryption.DoCryptWork("e", TextBoxInput.Text) ' encrypt data.
TextBoxOutput.Text = Encryption.DoCryptWork("d", TextBoxInput.Text) ' decrypt data.
When you convert the bytes to hex, you output two hex digits per byte. When you convert that hex back to bytes, you're converting every hex digit to a byte instead of every pair of hex digits.
Actually, I just took another look and noticed that you're not even keeping the earlier bytes. This loop:
For n As Integer = 0 To Data.Length - 1
CipherTextBytes = Convert.ToByte(Data(n))
Next n
sets CipherTextBytes on each iteration so you're going to replace the previous byte each time, so you only end up keeping the byte from the last digit.

System.Security.Cryptography.CryptographicException: Bad Data

I am having this bad data problem when I tried to open it. Any idea how to solve it? When I debug it shows that CryptoStream.FlushFinalBlock() is having a problem. See codes below.
Public Class Encryption
Public Function Encrypt(ByVal plainText As String) As Byte()
Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
Dim inputInBytes() As Byte = utf8encoder.GetBytes(plainText)
Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
' The ICryptTransform interface uses the TripleDES
' crypt provider along with encryption key and init vector
' information
Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateEncryptor(Me.key, Me.iv)
Dim encryptedStream As MemoryStream = New MemoryStream()
Dim cryptStream As CryptoStream = New CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write)
cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
cryptStream.FlushFinalBlock()
encryptedStream.Position = 0
Dim result(encryptedStream.Length - 1) As Byte
encryptedStream.Read(result, 0, encryptedStream.Length)
cryptStream.Close()
Return result
End Function
Public Function Decrypt(ByVal inputInBytes() As Byte) As String
' UTFEncoding is used to transform the decrypted Byte Array
' information back into a string.
Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
' As before we must provide the encryption/decryption key along with
' the init vector.
Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateDecryptor(Me.key, Me.iv)
' Provide a memory stream to decrypt information into
Dim decryptedStream As MemoryStream = New MemoryStream()
Dim cryptStream As CryptoStream = New CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write)
cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
cryptStream.FlushFinalBlock()
decryptedStream.Position = 0
' Read the memory stream and convert it back into a string
Dim result(decryptedStream.Length - 1) As Byte
decryptedStream.Read(result, 0, decryptedStream.Length)
cryptStream.Close()
Dim myutf As UTF8Encoding = New UTF8Encoding()
Return myutf.GetString(result)
End Function
End Class
After the cryptStream.Write you can close it and return the MemoryStream data
Public Function Encrypt(ByVal plainText As String) As Byte()
Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
Dim inputInBytes() As Byte = utf8encoder.GetBytes(plainText)
Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
' The ICryptTransform interface uses the TripleDES
' crypt provider along with encryption key and init vector
' information
Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateEncryptor(Me.key, Me.iv)
Dim encryptedStream As MemoryStream = New MemoryStream()
Dim cryptStream As CryptoStream = New CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write)
cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
cryptStream.Close()
encryptedStream.Position = 0
Return encryptedStream.ToArray()
End Function
Update: Decrypt Method
Public Function Decrypt(ByVal inputInBytes() As Byte) As String
Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
' As before we must provide the encryption/decryption key along with
' the init vector.
Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateDecryptor(Me.key, Me.iv)
' Provide a memory stream to decrypt information into
Dim decryptedStream As MemoryStream = New MemoryStream()
Dim cryptStream As CryptoStream = New CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write)
cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
cryptStream.FlushFinalBlock()
Return System.Text.Encoding.Unicode.GetString(decryptedStream.ToArray)
End Function
Public Function Decrypt(ByVal inputInBytes() As Byte) As String
' UTFEncoding is used to transform the decrypted Byte Array
' information back into a string.
Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
' As before we must provide the encryption/decryption key along with
' the init vector.
Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateDecryptor(Me.key, Me.iv)
' Provide a memory stream to decrypt information into
Dim decryptedStream As MemoryStream = New MemoryStream()
Dim cryptStream As CryptoStream = New CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write)
cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
cryptStream.FlushFinalBlock()
decryptedStream.Position = 0
' Read the memory stream and convert it back into a string
Dim result(decryptedStream.Length - 1) As Byte
decryptedStream.Read(result, 0, decryptedStream.Length)
cryptStream.Close()
Dim myutf As UTF8Encoding = New UTF8Encoding()
Return myutf.GetString(result)
End Function
End Class

Overflow error for encryption

I am getting an overflow error in the following code:
Public Shared Function AESFileEncrypt(inputFilename As String, outputFilename As String) As Boolean
'Create SymmetricAlgorithm object and specify the Key and IV.
Dim AES As RijndaelManaged = New RijndaelManaged
AES.Key = objKeys.aesKey
AES.IV = objKeys.aesIV
'Create an ICryptoTransform (Encryptor) object.
Dim Encryptor As ICryptoTransform
Encryptor = AES.CreateEncryptor
'Read the unencrypted file.
Dim InputFileStream As FileStream
InputFileStream = New FileStream(inputFilename, FileMode.Open, FileAccess.Read)
Dim InputFileData(CType(InputFileStream.Length, Integer)) As Byte
InputFileStream.Read(InputFileData, 0, CType(InputFileStream.Length, Integer))
Dim OutputFileStream As FileStream
OutputFileStream = New FileStream(outputFilename, FileMode.Create, FileAccess.Write)
'Create a CryptoStream object using the Stream and ICryptoTransform objects.
Dim EncryptCryptoStream As CryptoStream
EncryptCryptoStream = New CryptoStream(OutputFileStream, Encryptor, CryptoStreamMode.Write)
EncryptCryptoStream.Write(InputFileData, 0, InputFileData.Length)
EncryptCryptoStream.FlushFinalBlock()
'Clear any sensitive data from the cyptographic object.
AES.Clear()
'Close stream objects.
EncryptCryptoStream.Close()
InputFileStream.Close()
OutputFileStream.Close()
Return True
'Catch ex As Exception
'Debug.Print("AESFileEncrypt", ex.Message)
'Return False
'End Try
End Function
The error is at this line:
Dim InputFileData(CType(InputFileStream.Length, Integer)) As Byte
This happens on large files. I know it is because the byte array cannot be that large. Can I please have some help to modify this code to get it working?
Thanks
EDIT
This is my current work on this function:
Public Function AESFileEncrypt(inputFilename As String, outputFilename As String, Password As String, Salt As String) As Boolean
Dim AES As RijndaelManaged = New RijndaelManaged
Dim HashAlgorithm As String = "SHA1" 'Can be SHA1 or MD5
Dim PasswordIterations As String = 2
Dim InitialVector As String = "WinStorePassword" 'This should be a string of 16 ASCII characters.
Dim KeySize As Integer = 256 'Can be 128, 192, or 256.
Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector)
Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(Salt)
Dim DerivedPassword As PasswordDeriveBytes = New PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations)
Dim KeyBytes As Byte() = DerivedPassword.GetBytes(KeySize / 8)
Dim Encryptor As ICryptoTransform
Encryptor = AES.CreateEncryptor(KeyBytes, InitialVectorBytes)
Dim InputFileStream As FileStream
InputFileStream = New FileStream(inputFilename, FileMode.Open, FileAccess.Read)
Dim OutputFileStream As FileStream
OutputFileStream = New FileStream(outputFilename, FileMode.Create, FileAccess.Write)
Dim EncryptCryptoStream As CryptoStream
EncryptCryptoStream = New CryptoStream(OutputFileStream, Encryptor, CryptoStreamMode.Write)
Const BUFFER_SIZE As Integer = 4096
Dim buffer(BUFFER_SIZE - 1) As Byte
Dim Position As Long
Do
If (Position + BUFFER_SIZE) > InputFileStream.Length Then
InputFileStream.Read(buffer, Position, InputFileStream.Length - Position)
EncryptCryptoStream.Write(buffer, Position, BUFFER_SIZE)
Exit Do
Else
InputFileStream.Read(buffer, Position, BUFFER_SIZE)
EncryptCryptoStream.Write(buffer, Position, BUFFER_SIZE)
End If
Position += BUFFER_SIZE
Loop
EncryptCryptoStream.FlushFinalBlock()
AES.Clear()
EncryptCryptoStream.Close()
InputFileStream.Close()
OutputFileStream.Close()
Return True
End Function
Is this correct? I did a test an a small file, and the output was a lot larger than i expected?

Error on encryption/decryption files

When I decrypt an encrypted file; it doesn't have the same size in bytes as the original file and the the hash of the file is different.
I get the bytes of the file using File.ReadAllBytes and send to EncryptBytes with the password. Also the same with DecryptBytes.
When I receive the bytes encrypted or decrypted i save them using File.WriteAllBytes.
I need that the decrypted file and original file have the same hash an bytes.
Please help
This my code:
Public Function EncryptBytes(ByVal pass As String, ByVal bytes() As Byte)
Dim myRijndael As New RijndaelManaged
myRijndael.Padding = PaddingMode.Zeros
myRijndael.KeySize = 256
myRijndael.BlockSize = 256
Dim encrypted() As Byte
Dim key() As Byte = CreateKey(pass)
Dim IV() As Byte = CreateIV(pass)
Dim encryptor As ICryptoTransform = myRijndael.CreateEncryptor(key, IV)
Dim msEncrypt As New MemoryStream()
Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
csEncrypt.Write(bytes, 0, bytes.Length)
csEncrypt.FlushFinalBlock()
encrypted = msEncrypt.ToArray()
Return encrypted
End Function
Public Function DecryptBytes(ByVal pass As String, ByVal bytes() As Byte)
Dim myRijndael As New RijndaelManaged
myRijndael.Padding = PaddingMode.Zeros
myRijndael.KeySize = 256
myRijndael.BlockSize = 256
Dim key() As Byte = CreateKey(pass)
Dim IV() As Byte = CreateIV(pass)
Dim decryptor As ICryptoTransform = myRijndael.CreateDecryptor(key, IV)
Dim fromEncrypt() As Byte = New Byte(bytes.Length) {}
Dim msDecrypt As New MemoryStream(bytes)
Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)
Return fromEncrypt
End Function
Private Function CreateKey(ByVal strPassword As String) As Byte()
Dim chrData() As Char = strPassword.ToCharArray
Dim intLength As Integer = chrData.GetUpperBound(0)
Dim bytDataToHash(intLength) As Byte
For i As Integer = 0 To chrData.GetUpperBound(0)
bytDataToHash(i) = CByte(Asc(chrData(i)))
Next
Dim SHA512 As New System.Security.Cryptography.SHA512Managed
Dim bytResult As Byte() = SHA512.ComputeHash(bytDataToHash)
Dim bytKey(31) As Byte
For i As Integer = 0 To 31
bytKey(i) = bytResult(i)
Next
Return bytKey
End Function
Private Function CreateIV(ByVal strPassword As String) As Byte()
Dim chrData() As Char = strPassword.ToCharArray
Dim intLength As Integer = chrData.GetUpperBound(0)
Dim bytDataToHash(intLength) As Byte
For i As Integer = 0 To chrData.GetUpperBound(0)
bytDataToHash(i) = CByte(Asc(chrData(i)))
Next
Dim SHA512 As New System.Security.Cryptography.SHA512Managed
Dim bytResult As Byte() = SHA512.ComputeHash(bytDataToHash)
Dim bytIV(31) As Byte
For i As Integer = 32 To 47
bytIV(i - 32) = bytResult(i)
Next
Return bytIV
End Function
Your DecryptBytes() method is broken. You are not using the return value of csDecrypt.Read(), it tells you have many bytes were decrypted. That will not be the same as fromEncrypt.Length. You'd also have a very hard time guessing how large a byte array to pass to this function.
Consider changing the function to return a MemoryStream. Call Read() in a loop and write what was read to the memory stream. Exit the loop when Read() returns 0.
Try this:
Public Function EncryptBytes(ByVal pass As String, ByVal bytes() As Byte)
Dim myRijndael As New RijndaelManaged
myRijndael.Padding = PaddingMode.PKCS7
myRijndael.Mode = CipherMode.CBC
myRijndael.KeySize = 256
myRijndael.BlockSize = 256
Dim encrypted() As Byte
Dim key() As Byte = CreateKey(pass)
Dim IV() As Byte = CreateIV(pass)
Dim encryptor As ICryptoTransform = myRijndael.CreateEncryptor(key, IV)
Dim msEncrypt As New MemoryStream()
Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
csEncrypt.Write(bytes, 0, bytes.Length)
csEncrypt.FlushFinalBlock()
encrypted = msEncrypt.ToArray()
Return encrypted
msEncrypt.Close()
csEncrypt.Close()
End Function
Public Function DecryptBytes(ByVal pass As String, ByVal bytes() As Byte)
Dim myRijndael As New RijndaelManaged
myRijndael.Padding = PaddingMode.PKCS7
myRijndael.Mode = CipherMode.CBC
myRijndael.KeySize = 256
myRijndael.BlockSize = 256
Dim decrypted() As Byte
Dim key() As Byte = CreateKey(pass)
Dim IV() As Byte = CreateIV(pass)
Dim decryptor As ICryptoTransform = myRijndael.CreateDecryptor(key, IV)
Dim msDecrypt As New MemoryStream()
Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write)
csDecrypt.Write(bytes, 0, bytes.Length)
csDecrypt.FlushFinalBlock()
decrypted = msDecrypt.ToArray()
Return decrypted
msDecrypt.Close()
csDecrypt.Close()
End Function