Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中加密未按预期工作_Php_Vb.net_Encryption - Fatal编程技术网

在PHP中加密未按预期工作

在PHP中加密未按预期工作,php,vb.net,encryption,Php,Vb.net,Encryption,我有一个非常愚蠢的情况,我从PHP中的VB.NET接收到一个加密字符串。我能解密密钥。但是当我想要加密结果并得到加密字符串时,我得到了一个不匹配的结果。谁能帮我一下吗 下面是PHP代码。 <?php //$syscode=$_REQUEST['syscode']; //The actual string is "blueberry" which is encrypted in VB.NET and sent to PHP $syscode = "8yN73RDmMFuXo9ux8QKC6

我有一个非常愚蠢的情况,我从PHP中的VB.NET接收到一个加密字符串。我能解密密钥。但是当我想要加密结果并得到加密字符串时,我得到了一个不匹配的结果。谁能帮我一下吗

下面是PHP代码。

<?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=trim($encryptRaw,"\x00..\x1F");
$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和PHP使用相同的iv加密?此外,尝试在加密后删除修剪-它不是必需的。您只需要在解密后进行修剪。

您是否对VB和PHP使用相同的iv加密?此外,尝试在加密后删除修剪-它不是必需的。解密后才需要修剪。

我在应用程序端使用的VB.NET代码如下

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

下面是我在应用程序端使用的VB.NET代码

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

实际的vb加密代码会很有帮助。我已经包括了下面的答案部分的vb.NET问题。实际的vb加密代码会很有帮助。我已经包括了下面的答案部分的vb.NET问题