?
Solved

Decrypt Rijndael

Posted on 2009-02-08
12
Medium Priority
?
2,767 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

0
Comment
Question by:webspirit
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 23585276
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
 

Author Comment

by:webspirit
ID: 23585304
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
 
LVL 14

Expert Comment

by:psadac
ID: 23585478
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:webspirit
ID: 23585524
Thank you for the reference but it is generic and my problem is mostly how to apply this code in the specific situaltion.
0
 
LVL 19

Accepted Solution

by:
LordOfPorts earned 2000 total points
ID: 23586132
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
 

Expert Comment

by:neta1o
ID: 23608531
LordOfPorts,

How would you do this in reverse?

Encrypt a string in VB.net and encode in base64, using the same parameters above.
0
 
LVL 19

Expert Comment

by:LordOfPorts
ID: 23616490
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
 

Author Comment

by:webspirit
ID: 23617114
LordOfPorts, That was fantastic. Thank you very very much.
0
 

Author Closing Comment

by:webspirit
ID: 31544304
Thank you very much!!!!!!!!!!!!!!!
0
 
LVL 19

Expert Comment

by:LordOfPorts
ID: 23617850
You are very welcome, webspirit. Thank you for the grade and the points.
0
 

Expert Comment

by:neta1o
ID: 23618759
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
 

Expert Comment

by:justanick
ID: 26833174
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

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question