Error in encryption / decryption files

When I decrypt an encrypted file; it does not have the same size in bytes as the original file, and the hash of the file is different.

I get the bytes of the file using File.ReadAllBytes and send to EncryptBytes with a password. Same thing with DecryptBytes.

When I get bytes encrypted or decrypted, I save them using File.WriteAllBytes.

I need the decrypted file and the source file to have the same hash bytes.

Please, help

This is 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 
+4
source share
2 answers

Try the following:

 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 
0
source

Your DecryptBytes () method does not work. You are not using the return value of csDecrypt.Read (), it says that you have a lot of decrypted bytes. It will not be like fromEncrypt.Length. It is also very difficult for you to guess how a large array of bytes go to this function.

Consider changing the function to return a MemoryStream. Call Read () in a loop and write what was read in the memory stream. Exit the loop when Read () returns 0.

+1
source

Source: https://habr.com/ru/post/1299893/


All Articles