« Greetings from Birmin… | Home | Just two weeks until … »

Encryption Interoperability


Sometimes it can be difficult to get things working in several development tools. For example if you encrypt something in a server shell script using openssl, you may want to decrypt in a Real Studio app. For example this command line:

openssl aes-128-cbc -in input.txt -out input_enc.txt -k mypassword -p

Runs AES 128bit CBC Mode encryption on an input.txt file. Result is written to input_enc.txt file and we use the given password. Also we didn't disable salting, so the encryption will get some salt.

So how we decrypt with MBS Plugin?
Well, we first locate the file and read it. If first 8 bytes are "Salted__", than the next 8 bytes are the salt. This is how OpenSSL adds flavor. Next we need to build key and initial vector using password, key and salt and pass them through MD5 to make them more random.

Now we can set key to AESMBS class. With passing key as string, the length of the key (16 bytes) will define that we use AES-128. We built memoryblocks for input and output and pass them Through DecryptCBC. Finally we need to remove PKCS7 padding. In result variable, we keep the result. If you encrypted text, this is the time where you can add a DefineEncoding call to mark it as UTF-8 or so.

Below you see the complete code:
dim f as FolderItem = SpecialFolder.Desktop.Child("input_enc.txt") dim b as BinaryStream = BinaryStream.Open(f) dim s as string = b.Read(b.Length) dim salt as string dim password as string = "mypassword" dim s8 as string = leftb(s,8) if s8 = "salted__" then salt = midb(s,9,8) s = midb(s,17) end if dim pkey as string = password + salt dim Key as string = MD5MBS(pkey) dim pIV as string = Key + password + salt dim IV as string = MD5MBS(piv) dim a as new AESMBS if a.SetKey(key) then dim Input as MemoryBlock = s dim output as new MemoryBlock(input.size) a.DecryptCBC input, input.size, iv, output // remove padding dim len as integer = output.Size dim n as integer = output.Int8Value(len-1) if n>=1 and n<=15 then dim isPadding as Boolean = true for i as integer = n downto 1 if output.Int8Value(len-i) <> n then isPadding = false end if next if isPadding then len = len - n end if end if dim result as string = output.StringValue(0,len) Break end if
02 11 12 - 22:44
No comments

  
Remember personal info?

Emoticons / Textile


Notify:
Hide email:

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.