EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Crypt/Decrypt a file AES

Posted: 09/23/2013 17:57:02
by Joan Castro (Basic support level)
Joined: 09/23/2013
Posts: 3


I'm trying to encrypt/decrypt a file with AES, I read one post that you recommend using a Class called TElSymmetricCrypto, I've changed the Eldos sample code in VB.NET based on the example on Delphi in a post and I can say the encrypt function does it well. But when I try to decrypt I can't figure out how to do it.

The sample use a function SBEncoding.Unit.Base64Decode to decode the crypt file, in the examples there is a function called Base64DecodeArray that recive a string, but in this case I'm trying to decode a file and I don't know what to send I mean, I kwow is a Byte() parameter but that's it, nothing else. In help there isn't information about Base64Decode.

Could you help me with some information about Base64Decode function to decrypt a file. Or if there is an specific function to decode a file.

Sorry for my bad english.
Have a nice day.
Posted: 09/24/2013 01:36:08
by Vsevolod Ievgiienko (Team)

Thank you for contacting us.

As encryption operation produces binary data as an output, so we use Base64 encoding to convert this data into readable form. Base64 encoding is performed with functions from SBEncoding namespace. If you want to encrypt a file then most likelly you don't need Base64 encoding, so can simply omit using SBEncoding.Unit.Base64Encode/Base64Decode. However if you need them you should call Base64Decode twice: the 1st time it will return a number of bytes needed to store decoded result; after that you should allocate a buffer of that size; and the 2nd time it will decode the data and put it into the allocated buffer.
Posted: 09/26/2013 10:28:50
by Joan Castro (Basic support level)
Joined: 09/23/2013
Posts: 3

Hi, I appreciate your helpful answer. It helped me a lot, I would never figure out how to do it, I'm kinda new in this. Well, I tried to ommit using SBEncoding.Unit.Base64Encode/Base64Decode I tried to change the sample about encoding like I said, but when I try to encrypt/decrypt an Excel (xlsx) file, for example. When I open the decrypted file it says something about: "Excel has found unreadable content in the file "testfile.xlsx", would you like to recover the content of the book. If you trust in the book's content, recover it press Yes"
I don't know if it is because of this line outBuf = New Byte(outSize - 1) {} or maybe not, or it is needed to crypt a file. It is normal, or what I'm doing wrong?
Posted: 09/27/2013 02:16:24
by Vsevolod Ievgiienko (Team)

Most likelly some junk is added to decrypted copy as a result of incorrect decryption operations. Its hard to say exactly what is wrong without your modified source code.
Posted: 10/02/2013 18:45:10
by Joan Castro (Basic support level)
Joined: 09/23/2013
Posts: 3

Sorry, I've been busy these days.
It's kinda weird, because I had the same doubt. If the way I'm loading the file that I want to crypt makes the file with some kind of junk. But, in the same way I'm opening the file, I'm saving it (without any crypt or decrypt process) and it worked, it opens without problem and without junk. But I can't figure out what I'm doing wrong.

I don't think the crypt process might be wrong so I think it's the decrypt process and this is what I have. What I've changed of the sample code.

      Dim fs As FileStream = File.OpenRead("D:\UTF8SBenc.txt") ' crypted file
        Dim fsOut As FileStream = File.Create("D:\UTF8SBdec.txt") ' new decrypt file (testing)
        Dim ms As New MemoryStream()
        fs.Read(ms.GetBuffer(), 0, CInt(Fix(fs.Length)))

            Dim crypto As TElSymmetricCrypto = _
                factory.CreateInstance(SBConstants.Unit.SB_ALGORITHM_CNT_AES256, _
            crypto.KeyMaterial = PasswordToKeyMaterial(textPassword.Text)

            Dim outBuf() As Byte = Nothing
            Dim outSize As Integer = 0

            crypto.Decrypt(ms.GetBuffer, 0, ms.Length, outBuf, 0, outSize)
            outBuf = New Byte(outSize - 1) {}
            crypto.Decrypt(ms.GetBuffer, 0, ms.Length, outBuf, 0, outSize)

            fsOut.Write(outBuf, 0, outBuf.Length)
Posted: 10/03/2013 01:30:07
by Vsevolod Ievgiienko (Team)

Why do you allocate an output buffer this way?

outBuf = New Byte(outSize - 1) {}

This code will allocate a buffer that is 1 byte shorter than needed.

TElSymmetricCrypto has Encrypt/Decrypt methods that accept streams as input and output. You should better use them instead of methods for buffers. In this case the code will look this way:

crypto.Decrypt(fs, fsOut, fs.Length); //decryption is one step



Topic viewed 1679 times

Number of guests: 1, registered members: 0, in total hidden: 0


Back to top

As of July 15, 2016 EldoS business operates as a division of /n software, inc. For more information, please read the announcement.

Got it!