We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Decrypt Rijndael

Medium Priority
3,319 Views
Last Modified: 2013-11-07
Hi,
I am using PHP 's mbcrypt (in the code) to encrypt a string. I want to decrypt that string in VB.net. I have seen some code to do Rijndael decryption in VB.net but it does not work for me. Could you help?


Many thanks,

John
$iv = "1111111111111111"; // 16 or warning
$key = "jdlskajdlkasdjalksjdalksdjlka";
$text = "Meet me at 11 o'clock behind the monument.";
echo strlen($text) . "<br>";
 
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv);
echo strlen($crypttext) . "\n";
echo base64_encode($crypttext);

Open in new window

Comment
Watch Question

Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
The mbcript-function. In what encryption does it encrypt ?

Rijndael Encryption in VB.NET
http://www.freevbcode.com/ShowCode.Asp?ID=4520

How To: Encrypt and Decrypt Data Using a Symmetric (Rijndael) Key (C#/VB.NET)
http://www.obviex.com/samples/Encryption.aspx

Author

Commented:
I had tried to implement the code in the second link before posting, but I could not get it right. Using the IV string with 16 "1" prompts an errorthat the IV size is not correct in VB. When I put a string with 8 letters, it did not have a compilation error, but the decrytpion was wrong.

Commented:
i think this is an example from the .net framework :

http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged(VS.71).aspx

for the php i have used successfully the code below, but with AutoIt on the client side :
$data = "Meet me at 11 o'clock behind the monument.";
$pass = "password"
 
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_ECB, '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $pass, $iv);
$encrypted = mcrypt_generic($td, $data);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
 
echo base64_encode($encrypted);

Open in new window

Author

Commented:
Thank you for the reference but it is generic and my problem is mostly how to apply this code in the specific situaltion.
Use both a 256bit IV as well as a 256bit key in the PHP as follows:

<?php

$iv = "11111111111111111111111111111111"; // 32 * 8 = 256 bit iv
$key = "jdlskajdlkasdjalksjdalksdjlkappo"; // 32 * 8 = 256 bit key

$text = "Meet me at 11 o'clock behind the monument.";
 
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv);

echo base64_encode($crypttext);

?>

In the VB algorithm set the Padding, Mode, KeySize, and BlockSize properties of a RijndaelManaged instance as in the code snippet below. In this example there is a TexBox1 text box to paste the encrypted base64 encoded string and a Label1 label to display the decrypted string.

Imports:

Imports System.IO
Imports System.Security.Cryptography
Imports System.Text

Useful References:

http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged(VS.71).aspx
http://blogs.msdn.com/shawnfa/archive/2006/10/09/The-Differences-Between-Rijndael-and-AES.aspx

        Dim sEncryptedString As String = TextBox1.Text
 
        Dim myRijndael As New RijndaelManaged
        myRijndael.Padding = PaddingMode.Zeros
        myRijndael.Mode = CipherMode.CBC
        myRijndael.KeySize = 256
        myRijndael.BlockSize = 256
 
        Dim toEncrypt() As Byte
        Dim encrypted() As Byte
 
        Dim IV() As Byte = System.Text.Encoding.ASCII.GetBytes("11111111111111111111111111111111")
        Dim key() As Byte = System.Text.Encoding.ASCII.GetBytes("jdlskajdlkasdjalksjdalksdjlkappo")
 
 
        Dim decryptor As ICryptoTransform = myRijndael.CreateDecryptor(key, IV)
 
        Dim sEncrypted As Byte() = Convert.FromBase64String(sEncryptedString)
 
        Dim fromEncrypt() As Byte = New Byte(sEncrypted.Length) {}
 
        Dim msDecrypt As New MemoryStream(sEncrypted)
        Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
 
        csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)
 
        Label1.Text = System.Text.Encoding.ASCII.GetString(fromEncrypt)

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
LordOfPorts,

How would you do this in reverse?

Encrypt a string in VB.net and encode in base64, using the same parameters above.
I have placed an example in the code snippet below inspired by the example at http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged(VS.71).aspx The example assumes you have one text box TextBox1 to enter the text to be encrypted and a TextBox2 to display the encrypted, base 64 encoded string.

You can then take the encoded string and pass it to PHP. Now, right now I don't have access to a computer with the mcrypt library so I cannot 100% verify this will work but it should:

<?php

// Decode the base 64 encoded string, could be coming from a text area during a POSt
$sEncryptedText = base64_decode("PydAMRxJEswoU/9VNV2MmRp5IpTZM4uAt5b49qss7eKme4tcPxOUOW49JaZhwbgV/1t3PHXmumpRqi3y/po9qw==");

$iv = "11111111111111111111111111111111"; // 32 * 8 = 256 bit iv
$key = "jdlskajdlkasdjalksjdalksdjlkappo"; // 32 * 8 = 256 bit key

$text = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $sEncryptedText, MCRYPT_MODE_CBC, $iv);

echo $text;

?>

Let me know if this works for you or if you need any help I will test it during the upcoming weekend.
        Dim sToEncrypt As String = TextBox1.Text
 
        Dim myRijndael As New RijndaelManaged
        myRijndael.Padding = PaddingMode.Zeros
        myRijndael.Mode = CipherMode.CBC
        myRijndael.KeySize = 256
        myRijndael.BlockSize = 256
 
        Dim encrypted() As Byte
        Dim toEncrypt() As Byte
 
        Dim IV() As Byte = System.Text.Encoding.ASCII.GetBytes("11111111111111111111111111111111")
        Dim key() As Byte = System.Text.Encoding.ASCII.GetBytes("jdlskajdlkasdjalksjdalksdjlkappo")
 
        Dim encryptor As ICryptoTransform = myRijndael.CreateEncryptor(key, IV)
 
        Dim msEncrypt As New MemoryStream()
        Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
 
        toEncrypt = System.Text.Encoding.ASCII.GetBytes(sToEncrypt)
 
        csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)
        csEncrypt.FlushFinalBlock()
 
        encrypted = msEncrypt.ToArray()
 
        TextBox2.Text = Convert.ToBase64String(encrypted)

Open in new window

Author

Commented:
LordOfPorts, That was fantastic. Thank you very very much.

Author

Commented:
Thank you very much!!!!!!!!!!!!!!!
You are very welcome, webspirit. Thank you for the grade and the points.

Commented:
Just to save you some trouble LordOfPorts, this works great both ways, but if Encrypted in VB.net and Decrypted in PHP you'll want to use this line of code before echo $text;  to remove nulls.

$text = rtrim($text, "\0\4");
Dear Experts,

how can i encrypt and decrypt a string that contains empty lines / paragraphs?

"This is an example

with empty lines and paragraphs

END of test"

I want to keep this empty lines / paragraphs after decryption, but i receive only one line:

"This is an example with empty lines and paragraphs END of test"

Anyone has an idea? :)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.