Decrypt Rijndael

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

webspiritAsked:
Who is Participating?
 
LordOfPortsConnect With a Mentor Commented:
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

0
 
DhaestCommented:
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
0
 
webspiritAuthor 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.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
psadacCommented:
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

0
 
webspiritAuthor Commented:
Thank you for the reference but it is generic and my problem is mostly how to apply this code in the specific situaltion.
0
 
neta1oCommented:
LordOfPorts,

How would you do this in reverse?

Encrypt a string in VB.net and encode in base64, using the same parameters above.
0
 
LordOfPortsCommented:
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

0
 
webspiritAuthor Commented:
LordOfPorts, That was fantastic. Thank you very very much.
0
 
webspiritAuthor Commented:
Thank you very much!!!!!!!!!!!!!!!
0
 
LordOfPortsCommented:
You are very welcome, webspirit. Thank you for the grade and the points.
0
 
neta1oCommented:
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");
0
 
justanickCommented:
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? :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.