webspirit
asked on
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
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);
ASKER
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.
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 :
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);
ASKER
Thank you for the reference but it is generic and my problem is mostly how to apply this code in the specific situaltion.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
LordOfPorts,
How would you do this in reverse?
Encrypt a string in VB.net and encode in base64, using the same parameters above.
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("PydAMRxJEsw oU/9VNV2Mm Rp5IpTZM4u At5b49qss7 eKme4tcPxO UOW49JaZhw bgV/1t3PHX mumpRqi3y/ po9qw==");
$iv = "1111111111111111111111111 1111111"; // 32 * 8 = 256 bit iv
$key = "jdlskajdlkasdjalksjdalksd jlkappo"; // 32 * 8 = 256 bit key
$text = mcrypt_decrypt(MCRYPT_RIJN DAEL_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.
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("PydAMRxJEsw
$iv = "1111111111111111111111111
$key = "jdlskajdlkasdjalksjdalksd
$text = mcrypt_decrypt(MCRYPT_RIJN
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)
ASKER
LordOfPorts, That was fantastic. Thank you very very much.
ASKER
Thank you very much!!!!!!!!!!!!!!!
You are very welcome, webspirit. Thank you for the grade and the points.
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");
$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? :)
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? :)
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