How to Decode Rijndael in Ruby (Encoded in Vb.Net)

Rijndael encryption in Ruby

I pretty much doubt they really use Rijndael. They likely use AES (which is is subset of Rijndael with fixed 128 bits block size and only three standardized key size (128,192 and 256 bits)). Without them supplying blocksize and keysize you cannot be sure anyway, you can likely assume AES-128, but their spec is incomplete at best.

The IV is not related to the cipher algorithm but to the chaining mode, in your case CBC.

AES, CBC, PKCS7 are available in Ruby via OpenSSL, shouldn't be too much trouble.

Edit: people thinking they use Rijndael make me think they use .NET, in which case that question should solve your issues : How to decode Rijndael in ruby (encoded in VB.net)

Migrating C# .Net encrypt/decrypt algorithm to Ruby

Finally, I found a library that exactly match the C# code and is ruby-mcrypt (https://github.com/kingpong/ruby-mcrypt). And the encryption/decryption code i used is this:

require 'mcrypt'

module Crypt
def Crypt.m_encrypt(data, key)
crypto = Mcrypt.new(:rijndael_128, :ecb, key, nil, :zeros)
encrypted_data = crypto.encrypt(data)
encrypted_data
end

def Crypt.m_decrypt(data, key)
crypto = Mcrypt.new(:rijndael_128, :ecb, key, nil, :zeros)
crypto.decrypt(data)
end
end

IMPORTANT: This C# Rjandael library doesn't uses the IV, for that reason i'm giving nil parameter as IV to Mcrypt.

This is only the encryption/decryption section, i'm also converting string to hexa format. Hope it can help somebody else!

Rijndael/AES decryption C# to PHP conversion

Your C# code doesn't really look secure, so if you can change it, see below for some tips.
Here is your given PHP code modified to look like it could be equivalent to the C# code given.

function decryptAES128CBC($content,$iv, $key) {

// AES is Rijndael-128
$rijndael = 'rijndael-128';

// key size is 128 bit = 16 bytes
$ks = 16;

// CBC mode, not OFB
$cp = mcrypt_module_open($rijndael, '', 'cbc', '');

// pad key and IV by zeros (this is not a good idea)
$key = str_pad($key, $ks, "\0");
$iv = str_pad($key, $iv, "\0");

// initialize the decryptor with key and IV
mcrypt_generic_init($cp, $key, $iv);

// the actual work
$decrypted = mdecrypt_generic($cp, $content);

// clean up
mcrypt_generic_deinit($cp);
mcrypt_module_close($cp);

// remove padding, see below
return unpad($decrypted);
}

The last unpad is there to remove the padding which was likely appended by the encryption function to enlarge the message size to a full number of blocks. The default padding used by RijndaelManaged is PKCS7-padding, which appends a number of bytes (between 1 to 16), each of which being equal to the number of bytes appended. In a real implementation, you would check after decryption that the padding is valid (i.e. that all these bytes have the same value), but for "quick and dirty" you can simply use something which checks the last byte and removes that many bytes. See the comments to mcrypt_decrypt for an example.

If you can change your C# code:

  • Note that it is usually not a good idea to use a fixed value (per key) as an initialization vector, and using the key itself as initialization vector is not good, either. Use a random initialization vector (sent together with the message), or see the next point.

  • Also, you normally don't want to use a (rather short) password directly as a key, but instead use a longer passphrase, and hash it with a salt (included with in the message) to derive the key. If you do this, you can also derive the initialization vector from the same two pieces of data (but in a way that they'll be different, using a key derivation function).
    To avoid brute-forcing your password from the encrypted file, use a slow hash function here (PBKDF-2 or bcrypt).

AES Encrypt String in VB.NET

A quick Google Search brings up the following functions: I've not tested whether the output they produce is correct, however they do appear to compile correctly.

Public Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim encrypted As String = ""
Try
Dim hash(31) As Byte
Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
Array.Copy(temp, 0, hash, 0, 16)
Array.Copy(temp, 0, hash, 15, 16)
AES.Key = hash
AES.Mode = Security.Cryptography.CipherMode.ECB
Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
Return encrypted
Catch ex As Exception
End Try
End Function

Public Function AES_Decrypt(ByVal input As String, ByVal pass As String) As String
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim decrypted As String = ""
Try
Dim hash(31) As Byte
Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
Array.Copy(temp, 0, hash, 0, 16)
Array.Copy(temp, 0, hash, 15, 16)
AES.Key = hash
AES.Mode = Security.Cryptography.CipherMode.ECB
Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
Dim Buffer As Byte() = Convert.FromBase64String(input)
decrypted = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
Return decrypted
Catch ex As Exception
End Try
End Function

Sourced from: http://www.l33thackers.com/Thread-VB-NET-AES-Encryption



Related Topics



Leave a reply



Submit