469,364 Members | 2,332 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,364 developers. It's quick & easy.

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

Hi,

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");
  10.  
  11.  
  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);
  19.  
  20. $encryptedStringbytes = base64_decode($encryptedString64);
  21.  
  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. }
  27.  
  28. function Encrypt($strToEncrypt){
  29. $key64 = "cPSQAC05GBXzMhRRz7tm8cqg+vHdHyN5";
  30. $iv64 = "jIShBJVBfXo=";
  31. $keybytes = base64_decode($key64);
  32. $ivbytes = base64_decode($iv64);
  33.  
  34. // use mcrypt library for encryption
  35. $encryptRaw = mcrypt_encrypt(MCRYPT_3DES, $keybytes, $strToEncrypt, MCRYPT_MODE_CBC, $ivbytes);
  36. $encryptString=(base64_encode(trim($encryptRaw)));
  37.  
  38. print "Encrypted in PHP:<br>$encryptString<br/>"; //This where the PHP encrypted result is not matching the VB.NET encryption result.
  39. }
  40. ?>
  41.  


VB.NET Code

Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.Text
  3. Imports System.Security.Cryptography
  4. Imports System.IO
  5.  
  6. Module Crypto
  7.     Public Function Decrypt(ByVal strToDecrypt As String) As String
  8.         Try
  9.  
  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=")
  14.  
  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
  20.  
  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
  26.  
  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
  34.  
  35.             'reassemble the decrypted stream into a string    
  36.             Dim decryptedValue As String = Encoding.ASCII.GetString(decryptedStream.ToArray())
  37.  
  38.  
  39.             Return (decryptedValue.ToString())
  40.  
  41.         Catch ex As Exception
  42.             MsgBox(ex.Message)
  43.         End Try
  44.     End Function
  45.  
  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=")
  50.  
  51.         Dim inputByteArray() As Byte = Encoding.ASCII.GetBytes(strToEncrypt)
  52.         Dim mS As MemoryStream = New MemoryStream()
  53.  
  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()
  59.  
  60.         Return Convert.ToBase64String(mS.ToArray).ToString
  61.     End Function
  62. End Module
  63.  
Aug 26 '09 #1
0 3490

Post your reply

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

Similar topics

4 posts views Thread by Spikinsson | last post: by
1 post views Thread by Benoît | last post: by
7 posts views Thread by Matthias S. | last post: by
8 posts views Thread by Gidi | last post: by
8 posts views Thread by toupeira23 | last post: by
4 posts views Thread by Islamegy® | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.