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 - <?php
-
//$syscode=$_REQUEST['syscode'];
-
//The actual string is "blueberry" which is encrypted in VB.NET and sent to PHP
-
$syscode = "8yN73RDmMFuXo9ux8QKC6w=="; //This is the encrypted string as received from VB.NET
-
echo "Original Encrypted String Received from VB.NET: <br>".$syscode;
-
echo "<br><br>";
-
Decrypt($syscode);
-
echo "<br><br>";
-
Encrypt("blueberry");
-
-
-
function Decrypt($strToDecrypt){
-
global $strD;
-
$key64 = "cPSQAC05GBXzMhRRz7tm8cqg+vHdHyN5";
-
$iv64 = "jIShBJVBfXo=";
-
$encryptedString64 = $strToDecrypt;
-
$keybytes = base64_decode($key64);
-
$ivbytes = base64_decode($iv64);
-
-
$encryptedStringbytes = base64_decode($encryptedString64);
-
-
// use mcrypt library for encryption
-
$decryptRaw = mcrypt_decrypt(MCRYPT_3DES, $keybytes, $encryptedStringbytes, MCRYPT_MODE_CBC, $ivbytes);
-
$decryptString=trim($decryptRaw,"\x00..\x1F");
-
print "Decrypted by PHP:<br>$decryptString<br/>"; //The decrypted string should be "blueberry"
-
}
-
-
function Encrypt($strToEncrypt){
-
$key64 = "cPSQAC05GBXzMhRRz7tm8cqg+vHdHyN5";
-
$iv64 = "jIShBJVBfXo=";
-
$keybytes = base64_decode($key64);
-
$ivbytes = base64_decode($iv64);
-
-
// use mcrypt library for encryption
-
$encryptRaw = mcrypt_encrypt(MCRYPT_3DES, $keybytes, $strToEncrypt, MCRYPT_MODE_CBC, $ivbytes);
-
$encryptString=(base64_encode(trim($encryptRaw)));
-
-
print "Encrypted in PHP:<br>$encryptString<br/>"; //This where the PHP encrypted result is not matching the VB.NET encryption result.
-
}
-
?>
-
VB.NET Code - Imports System
-
Imports System.Text
-
Imports System.Security.Cryptography
-
Imports System.IO
-
-
Module Crypto
-
Public Function Decrypt(ByVal strToDecrypt As String) As String
-
Try
-
-
'initialize our key
-
Dim tripleDESKey As SymmetricAlgorithm = SymmetricAlgorithm.Create("TripleDES")
-
tripleDESKey.Key = Convert.FromBase64String("cPSQAC05GBXzMhRRz7tm8cqg+vHdHyN5")
-
tripleDESKey.IV = Convert.FromBase64String("jIShBJVBfXo=")
-
-
'load our encrypted value into a memory stream
-
Dim encryptedValue As String = strToDecrypt
-
Dim encryptedStream As MemoryStream = New MemoryStream()
-
encryptedStream.Write(Convert.FromBase64String(encryptedValue), 0, Convert.FromBase64String(encryptedValue).Length)
-
encryptedStream.Position = 0
-
-
'set up a stream to do the decryption
-
Dim cs As CryptoStream = New CryptoStream(encryptedStream, tripleDESKey.CreateDecryptor, CryptoStreamMode.Read)
-
Dim decryptedStream As MemoryStream = New MemoryStream()
-
Dim buf() As Byte = New Byte(2048) {}
-
Dim bytesRead As Integer
-
-
'keep reading from encrypted stream via the crypto stream
-
'and store that in the decrypted stream
-
bytesRead = cs.Read(buf, 0, buf.Length)
-
While (bytesRead > 0)
-
decryptedStream.Write(buf, 0, bytesRead)
-
bytesRead = cs.Read(buf, 0, buf.Length)
-
End While
-
-
'reassemble the decrypted stream into a string
-
Dim decryptedValue As String = Encoding.ASCII.GetString(decryptedStream.ToArray())
-
-
-
Return (decryptedValue.ToString())
-
-
Catch ex As Exception
-
MsgBox(ex.Message)
-
End Try
-
End Function
-
-
Public Function Encrypt(ByVal strToEncrypt As String) As String
-
Dim sa As SymmetricAlgorithm = SymmetricAlgorithm.Create("TripleDES")
-
sa.Key = Convert.FromBase64String("cPSQAC05GBXzMhRRz7tm8cqg+vHdHyN5")
-
sa.IV = Convert.FromBase64String("jIShBJVBfXo=")
-
-
Dim inputByteArray() As Byte = Encoding.ASCII.GetBytes(strToEncrypt)
-
Dim mS As MemoryStream = New MemoryStream()
-
-
Dim trans As ICryptoTransform = sa.CreateEncryptor
-
Dim buf() As Byte = New Byte(2048) {}
-
Dim cs As CryptoStream = New CryptoStream(mS, trans, CryptoStreamMode.Write)
-
cs.Write(inputByteArray, 0, inputByteArray.Length)
-
cs.FlushFinalBlock()
-
-
Return Convert.ToBase64String(mS.ToArray).ToString
-
End Function
-
End Module
-
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...
| |