473,583 Members | 3,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 4590

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

Similar topics

1
7946
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 there is a method which can generate a same with the same input string? There is a need to transfer file between to site,and the customer wish...
4
28276
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
0
1405
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, ccNumber, ccDate) VALUES (ENCRYPT('fred smith', 'mykey'),ENCRYPT('12345', 'mykey'),ENCRYPT('01.03', 'mykey')) And they're read out using something...
1
3956
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 following code, it works. I encrypt with the public key which is in the certificate. I decrypt with the private key. But why, the crypted message is...
7
26763
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) string again to a human readable form. Can't be that difficult :). Could you send me please into the right direction. Thanks in advance. --
8
8165
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, just for a simple Encryption and Decryption function. thanks, Gidi.
8
4265
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 TripleDES and returns a Base64-encoded string. The decryption function does the reverse, i.e. Base64-decodes the string, decrypts it with the same Key and...
4
4200
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 HttpUtitlity.UrlEncode() but i got bad data Exception.. Is there anyway to work around this??
3
8241
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 build with these methods, will appear to encrypt and decrypt, but the resulting decrypted file will be corrupted. I tried encrypting a .bmp file and...
0
7894
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7821
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8320
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8190
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6577
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5697
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3814
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1424
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.