I've been trying various methods, but the .Net code seems to be converting the strings and keys to binary before decrypting and the's tripping me up. I've read over and over that AES is essentially Rijndael, so I've been using the open SSL library built into Ruby 1.9.3.
RijndaelManaged crypto = new RijndaelManaged();
crypto.Padding = PaddingMode.PKCS7;
crypto.Key = ASCIIEncoding.ASCII.GetBytes(secretKey);
crypto.IV = ASCIIEncoding.ASCII.GetBytes(initializationVector);
byte[] byteArray = new byte[stringToDecrypt.Length / 2];
for (int i = 0; i < stringToDecrypt.Length / 2; i++)
{
int index = (Convert.ToInt32(stringToDecrypt.Substring(i * 2, 2), 16));
byteArray[i] = (byte)index;
}
MemoryStream memStream = new MemoryStream();
CryptoStream cryStream = new CryptoStream(memStream, crypto.CreateDecryptor(), CryptoStreamMode.Write);
cryStream.Write(byteArray, 0, byteArray.Length);
cryStream.FlushFinalBlock();
StringBuilder stringBuilder = new StringBuilder();
foreach (byte dByte in memStream.ToArray())
{
stringBuilder.Append((char)dByte);
}
return stringBuilder.ToString();
Here's my current Ruby code which is not doing the trick. It runs (except for the last line) but gives me back garbage.
I'm fairly certain the issue is with the MemoryStream/CryptoStream functions, but I've not a clue where to start with Ruby on this.
text = Base64.encode64(cipher_text)
cypher = OpenSSL::Cipher::AES.new(256, :CBC)
cypher.decrypt
cypher.key = Base64.encode64("<cypher key>")
cypher.iv = Base64.encode64("<cypher iv>")
cypher.padding = 1
aes = cypher.update(cipher_text)
aes << cypher.final
I also tried this. I figured if this doesn't work the encryption is not simply Rijndael with PKCS7 padding but other obfuscation.
cypher = OpenSSL::Cipher::AES.new(256, :CBC)
cypher.decrypt
cypher.key = "<cypher key>"
cypher.iv = "<cypher iv>"
cypher.padding = 1
cypher.update(cipher_text)