472,378 Members | 1,150 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

Encrypt in VB.NET and Decrypt in PHP and Vice Versa


My requirement is that a string is encrypted in VB.NET and sent to PHP, PHP decrypts the string (till here the logic is working) and then the PHP should encrtyp (where i am having problems) and send the data to VB.NET application. The proble is in encrypting in PHP side.

PHP Code
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //$syscode=$_REQUEST['syscode'];
  3. //The actual string is "blueberry" which is encrypted in VB.NET and sent to PHP
  4. $syscode = "8yN73RDmMFuXo9ux8QKC6w=="; //This is the encrypted string as received from VB.NET
  5. echo "Original Encrypted String Received from VB.NET: <br>".$syscode;
  6. echo "<br><br>";
  7. Decrypt($syscode);
  8. echo "<br><br>";
  9. Encrypt("blueberry");
  12. function Decrypt($strToDecrypt){
  13. global $strD;
  14. $key64 = "cPSQAC05GBXzMhRRz7tm8cqg+vHdHyN5";
  15. $iv64 = "jIShBJVBfXo=";
  16. $encryptedString64 = $strToDecrypt;
  17. $keybytes = base64_decode($key64);
  18. $ivbytes = base64_decode($iv64);
  20. $encryptedStringbytes = base64_decode($encryptedString64);
  22. // use mcrypt library for encryption
  23. $decryptRaw = mcrypt_decrypt(MCRYPT_3DES, $keybytes, $encryptedStringbytes, MCRYPT_MODE_CBC, $ivbytes);
  24. $decryptString=trim($decryptRaw,"\x00..\x1F");
  25. print "Decrypted by PHP:<br>$decryptString<br/>"; //The decrypted string should be "blueberry"
  26. }
  28. function Encrypt($strToEncrypt){
  29. $key64 = "cPSQAC05GBXzMhRRz7tm8cqg+vHdHyN5";
  30. $iv64 = "jIShBJVBfXo=";
  31. $keybytes = base64_decode($key64);
  32. $ivbytes = base64_decode($iv64);
  34. // use mcrypt library for encryption
  35. $encryptRaw = mcrypt_encrypt(MCRYPT_3DES, $keybytes, $strToEncrypt, MCRYPT_MODE_CBC, $ivbytes);
  36. $encryptString=(base64_encode(trim($encryptRaw)));
  38. print "Encrypted in PHP:<br>$encryptString<br/>"; //This where the PHP encrypted result is not matching the VB.NET encryption result.
  39. }
  40. ?>


Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.Text
  3. Imports System.Security.Cryptography
  4. Imports System.IO
  6. Module Crypto
  7.     Public Function Decrypt(ByVal strToDecrypt As String) As String
  8.         Try
  10.             'initialize our key
  11.             Dim tripleDESKey As SymmetricAlgorithm = SymmetricAlgorithm.Create("TripleDES")
  12.             tripleDESKey.Key = Convert.FromBase64String("cPSQAC05GBXzMhRRz7tm8cqg+vHdHyN5")
  13.             tripleDESKey.IV = Convert.FromBase64String("jIShBJVBfXo=")
  15.             'load our encrypted value into a memory stream
  16.             Dim encryptedValue As String = strToDecrypt
  17.             Dim encryptedStream As MemoryStream = New MemoryStream()
  18.             encryptedStream.Write(Convert.FromBase64String(encryptedValue), 0, Convert.FromBase64String(encryptedValue).Length)
  19.             encryptedStream.Position = 0
  21.             'set up a stream to do the decryption
  22.             Dim cs As CryptoStream = New CryptoStream(encryptedStream, tripleDESKey.CreateDecryptor, CryptoStreamMode.Read)
  23.             Dim decryptedStream As MemoryStream = New MemoryStream()
  24.             Dim buf() As Byte = New Byte(2048) {}
  25.             Dim bytesRead As Integer
  27.             'keep reading from encrypted stream via the crypto stream
  28.             'and store that in the decrypted stream
  29.             bytesRead = cs.Read(buf, 0, buf.Length)
  30.             While (bytesRead > 0)
  31.                 decryptedStream.Write(buf, 0, bytesRead)
  32.                 bytesRead = cs.Read(buf, 0, buf.Length)
  33.             End While
  35.             'reassemble the decrypted stream into a string    
  36.             Dim decryptedValue As String = Encoding.ASCII.GetString(decryptedStream.ToArray())
  39.             Return (decryptedValue.ToString())
  41.         Catch ex As Exception
  42.             MsgBox(ex.Message)
  43.         End Try
  44.     End Function
  46.     Public Function Encrypt(ByVal strToEncrypt As String) As String
  47.         Dim sa As SymmetricAlgorithm = SymmetricAlgorithm.Create("TripleDES")
  48.         sa.Key = Convert.FromBase64String("cPSQAC05GBXzMhRRz7tm8cqg+vHdHyN5")
  49.         sa.IV = Convert.FromBase64String("jIShBJVBfXo=")
  51.         Dim inputByteArray() As Byte = Encoding.ASCII.GetBytes(strToEncrypt)
  52.         Dim mS As MemoryStream = New MemoryStream()
  54.         Dim trans As ICryptoTransform = sa.CreateEncryptor
  55.         Dim buf() As Byte = New Byte(2048) {}
  56.         Dim cs As CryptoStream = New CryptoStream(mS, trans, CryptoStreamMode.Write)
  57.         cs.Write(inputByteArray, 0, inputByteArray.Length)
  58.         cs.FlushFinalBlock()
  60.         Return Convert.ToBase64String(mS.ToArray).ToString
  61.     End Function
  62. End Module
Aug 26 '09 #1
0 4304

Sign in to post your reply or Sign up for a free account.

Similar topics

by: wqhdebian | last post by:
As far as I know,when encrypt or decrypt ,a key must first be got,and the key is first generate by a tool or from SecurityRandom,that means I can not generate the same key with the same input.Does...
by: Spikinsson | last post by:
I'm looking for a good decrypt/encrypt function, all I want is a function in this form: char* encrypt(char* normal) { ... return encrypted; } and
by: Mark Hanford | last post by:
I've been setting up a new MySQL/PHP site which will contain store some CC details, and have been wondering how to pass the keys. CC's are written in a similar way to: INSERT INTO cc (ccName,...
by: Benoît | last post by:
Hi, I have generated two keys : "C:>openssl req -nodes -new -x509 -keyout ben.key -out ben.crt -days 3650" I try to encrypt/decrypt a string like "JOHN" with these asymetrics keys. With the...
by: Matthias S. | last post by:
Hi, I had a look at the vast information on encryption in the MSDN and got pretty confused. All I want to do is to encrypt a string into an encrypted string and later decrypt that (encrypted)...
by: Gidi | last post by:
Hi, Is there Buid-In fuction in C# that Encrypt and Decrypt strings? i have a textbox which i'm writing into file, and i want to encrypt it before writing, i'm not looking for something fancy,...
by: toupeira23 | last post by:
Hello, I'm trying to encrypt passwords in my app. After discovering that there's no simple function to do this, I wrote a wrapper class which decodes a string using UTF8, encrypts it with...
by: Islamegy® | last post by:
I give up.. I tried everything to encrypt querystring and decrypt it back but this never success.. i use RSA encryption. I always get excption when Convert fromBase64String so i tried...
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.