RijndaelManaged cryptography algorithm problem

LukaszC89

Member
Joined
Dec 29, 2014
Messages
5
Programming Experience
1-3
I used this function to encrypt and decrypt any files in my aplication but i've got problem.
When I use this on .jpg, .png etc. (pictures) everything is allright but if i need to encode and decode .txt or .doc/.docx Ive got problem like:

when I put orginal .txt like:
SELECT TOP 1 @id = id, @active = active_b FROM active_b _replacement
I obtain after decryptionthis:
SELECT TOP 1 @id = id, @active = active_b FROM as˝˝lA)ć?????c


I have no idea why. Is there any unrecognised characters to this algorithm?
I can't use this for .doc, .docx too.

Any
ideas, suggestions, tips?

Used Function:


HTML:
Public Function EncryptOrDecryptFile(ByVal strInputFile As String, _
                              ByVal strOutputFile As String, _
                              ByVal Key As String, _
                              ByVal Direction As Integer)
        Try 'In case of errors.


            'Setup file streams to handle input and output.
            fsInput = New System.IO.FileStream(strInputFile, FileMode.Open, _
                                                  FileAccess.Read)
            fsOutput = New System.IO.FileStream(strOutputFile, _
                                                   FileMode.OpenOrCreate, _
                                                   FileAccess.Write)
            fsOutput.SetLength(0) 'make sure fsOutput is empty


            'Declare variables for encrypt/decrypt process.
            Dim bytBuffer(4096) As Byte 'holds a block of bytes for processing
            Dim lngBytesProcessed As Long = 0 'running count of bytes processed
            Dim lngFileLength As Long = fsInput.Length 'the input file's length
            Dim intBytesInCurrentBlock As Integer 'current bytes being processed
            Dim csCryptoStream As CryptoStream
            'Declare your CryptoServiceProvider.
            Dim cspRijndael As New System.Security.Cryptography.RijndaelManaged


            'Determine if ecryption or decryption and setup CryptoStream.
            Select Case Direction
                Case 1
                    csCryptoStream = New CryptoStream(fsOutput, _
                    cspRijndael.CreateEncryptor(CreateKey(Key), CreateIV(Key)), _
                    CryptoStreamMode.Write)


                Case 2
                    csCryptoStream = New CryptoStream(fsOutput, _
                    cspRijndael.CreateDecryptor(CreateKey(Key), CreateIV(Key)), _
                    CryptoStreamMode.Write)
            End Select


            'Use While to loop until all of the file is processed.
            While lngBytesProcessed < lngFileLength
                'Read file with the input filestream.
                intBytesInCurrentBlock = fsInput.Read(bytBuffer, 0, 4096)
                'Write output file with the cryptostream.
                csCryptoStream.Write(bytBuffer, 0, intBytesInCurrentBlock)
                'Update lngBytesProcessed
                lngBytesProcessed = lngBytesProcessed + _
                                        CLng(intBytesInCurrentBlock)
            End While


            fsInput.Close()


            'Close FileStreams and CryptoStream.
            csCryptoStream.Close()
            fsOutput.Close()
        Catch ex As Exception
            Try
                fsInput.Close()
            Catch
            End Try
            Try
                fsOutput.Close()
            Catch
            End Try
        End Try

    End Function
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,604
Location
Norway
Programming Experience
10+
There is no interpretation of content when you read/write files at byte level with FileStream, so it doesn't matter which type or format these files have. The only possible problem I can see in code is if CreateKey/CreateIV produces different results for same string value, you must use same key/IV when encrypting and decrypting.
 

LukaszC89

Member
Joined
Dec 29, 2014
Messages
5
Programming Experience
1-3
I give the same key in function attribute. You mean the function CreateKey which supplied the same attribute key string give another value and this might be a problem?
Hmm I thinking about difference beetween jpg and doc. Jpg have no problem with this key/IV (even if different), but doc and txt make troubles.. (only part of them are decrypted).
But I will try to change functions CreateKey and CreateIV to generate the same keys.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,604
Location
Norway
Programming Experience
10+
Can I use your example to encode doc files?
The code you already have can encrypt/decrypt anything. Just make sure to use exactly the same byte arrays for Key and IV.
 

LukaszC89

Member
Joined
Dec 29, 2014
Messages
5
Programming Experience
1-3
The code you already have can encrypt/decrypt anything. Just make sure to use exactly the same byte arrays for Key and IV.

I declared the same keys without generate by functions CreateIV() and CreateKey().
HTML:
            Dim bytes() As Byte = New Byte() {18, 75, 192, 127, 72, 62, 40, 214, 139, 141, 126, 66, 133, 83, 118, 174, 173, 121, 226, 143, 220, 249, 23, 148, 69, 187, 137, 150, 80, 13, 180, 76}            Dim bytesIV() As Byte = New Byte() {11, 81, 85, 49, 190, 52, 234, 120, 4, 14, 114, 156, 198, 59, 253, 247}

and use them in:

Select Case Direction
Case CryptoAction.ActionEncrypt
csCryptoStream = New CryptoStream(fsOutput, _
cspRijndael.CreateEncryptor(bytes, bytesIV), _
CryptoStreamMode.Write)


Case CryptoAction.ActionDecrypt
csCryptoStream = New CryptoStream(fsOutput, _
cspRijndael.CreateDecryptor(bytes, bytesIV), _
CryptoStreamMode.Write)
End Select


I've got the same problem.
byte and byteIV should be the same too?

Edit: If I use bytesIV twice it's decoding problem too.
(cspRijndael.CreateEncryptor(bytesIV, bytesIV), _)
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,604
Location
Norway
Programming Experience
10+
and use them in:
CreateEncryptor(bytes, bytesIV)
CreateDecryptor(bytes, bytesIV)
Key is 32 bytes (256bits) which is default maxsize and ok, IV is 16 bytes (128bits = BlockSize) and also default and ok.
That is correct usage, decryption will always produce same bytes as those encrypted. Your problem must related to something else.
 
Top Bottom