3

我正在尝试在 PHP 和 VB.NET 中编写一个函数,该函数使用三重 DES在两个方向上传递加密数据。问题是,当我尝试使用 PHP 解密在 VB.NET 中加密的字符串时,我收到一条错误消息,指出 IV 的块大小必须匹配。

我在 VB.NET 中编写的类如下,并且功能齐全,因为它可以完美地加密和解密自己的块。

    Imports System
    Imports System.Text
    Imports System.IO
    Imports System.Security.Cryptography

    Public Class Cipher

        Dim method As TripleDESCryptoServiceProvider
        Dim key As Byte()

        Public Property Password() As String
            Get
                Return System.Text.Encoding.Unicode.GetString(Key)
            End Get
            Set(value As String)
                key = System.Text.Encoding.Unicode.GetBytes(value)
            End Set
        End Property

        Public Function Encrypt(data As String) As String
            Dim ms As New System.IO.MemoryStream

            ' Create the encoder to write to the stream.
            Dim dataBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(data)
            Dim encStream As New CryptoStream(ms, method.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)

            ' Use the crypto stream to write the byte array to the stream.
            encStream.Write(dataBytes, 0, dataBytes.Length)
            encStream.FlushFinalBlock()

            ' IV and Ciphered string are each Base64'd and seperated by a comma, then the whole result is Base64'd
            Return Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(Convert.ToBase64String(method.IV) & "," & Convert.ToBase64String(ms.ToArray)))
        End Function

        Public Function Decrypt(data As String) As String
            ' Convert the encrypted text string to a byte array.

            Dim partDecoded As String = System.Text.Encoding.Unicode.GetString(Convert.FromBase64String(data))
            Dim dataBytes() As Byte

            If InStr(partDecoded, ",") > 0 Then

                Dim parts() As String = Split(partDecoded, ",")

                ' Get IV from first part
                method.IV = Convert.FromBase64String(parts(0))

                ' Get ciphered data from second part
                dataBytes = Convert.FromBase64String(parts(1))

                ' Create the stream.
                Dim ms As New System.IO.MemoryStream
                ' Create the decoder to write to the stream.
                Dim decStream As New CryptoStream(ms, method.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write)

                ' Use the crypto stream to write the byte array to the stream.
                decStream.Write(dataBytes, 0, dataBytes.Length)
                decStream.FlushFinalBlock()

                ' Convert the plaintext stream to a string.
                Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
            Else
                Return False
            End If

        End Function

        Public Sub New()
            method = New TripleDESCryptoServiceProvider
            method.Mode = CipherMode.CFB
            method.GenerateIV()
        End Sub
    End Class

上述类的示例用法

    Dim c As New Cipher
    c.Password = "12345"

    Dim encrypted As String = c.Encrypt("hello")
    Debug.Print(encrypted)

    Dim decrypted As String = c.Decrypt(encrypted)
    Debug.Print(decrypted)

现在我也有以下 PHP 代码,它也可以工作(单独)

    class Cipher {
        private $iv;
        private $securekey;

        function __construct($key) {
            $this->securekey = $key;
        }

        function encrypt($string) {
            $this->iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_CFB),MCRYPT_DEV_RANDOM);
            $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_3DES, $this->securekey, $string, MCRYPT_MODE_CFB, $this->iv));
            return base64_encode(base64_encode($this->iv) . ',' . $encrypted);
        }

        function decrypt($string) {
            $decrypt = base64_decode($string);
            if(strpos($decrypt,',') > 0) {
                $decrypt = explode(',', $decrypt);
                $this->iv = base64_decode($decrypt[0]);
                return trim(mcrypt_decrypt(MCRYPT_3DES, $this->securekey, base64_decode($decrypt[1]), MCRYPT_MODE_CFB, $this->iv));
            } else {
                return false;
            }
        }
    }

PHP 示例用法

    $c = new Cipher("12345");
    $encrypted = $c->encrypt("hello");
    echo 'Encrypted: ' . $encrypted . '<br />';

    $decrypted = $c->decrypt($encrypted);
    echo 'Decrypted: ' . $decrypted . '<br />';

    $vb = "MwBOAEoAOQBjAEgAcQAyAC8ASABzAD0ALABmAEUAOQBaAHYAVwBzAFUAYQB3AFYARwBGAHUANABLAGUAVgB3AFcAaABRAD0APQA=";
    echo 'VB.NET Dec: ' . $c->decrypt($vb);

我在上面的 PHP 用法中包含的是用 VB.NET 制作的Base64字符串,它在 VB.NET 中完美解码为变量 $vb。

这让我非常生气,因为代码是正确的,而且在这两种情况下都可以正常工作——所以我错过了什么,你能指出/解决问题吗。我不希望使用Rijndael或探索其他密码方法,因为这种方法已经很成熟,可以在多种设备上运行(iOS、Android、Windows、Linux 等)。

4

2 回答 2

8

由于没有人能够提供功能齐全的 BI-DIRECTIONAL 解决方案,因此我冒昧地在本文中为社区提供了一个。

问题在于 PHP 不符合标准,因为它强制填充字符串以匹配。目前,如果在 .NET 和 PHP 之间随机生成,没有已知的可靠传递 IV 的方法(如果您确实发现了如何或这种变化,请随时修改)。

以下是使用三重 DES 以与 .NET 和 PHP 兼容的方式加密数据的完整解决方案,它允许双向三重 DES 加密通信。此方法还与 Java、Delphi、Objective-C 和许多其他语言兼容,但此处不会提供此类代码,因为这不是已发布问题的解决方案。


VB.NET 三重 DES 类

    Imports System
    Imports System.Text
    Imports System.IO
    Imports System.Security.Cryptography

    Public Class TripleDES

        Private bPassword As Byte()
        Private sPassword As String

        Public Sub New(Optional ByVal Password As String = "password")
            ' On Class Begin
            Me.Password = Password
        End Sub

        Public ReadOnly Property PasswordHash As String
            Get
                Dim UTF8 As System.Text.UTF8Encoding = New System.Text.UTF8Encoding
                Return UTF8.GetString(bPassword)
            End Get
        End Property

        Public Property Password() As String
            Get
                Dim UTF8 As System.Text.UTF8Encoding = New System.Text.UTF8Encoding
                Return sPassword
            End Get
            Set(value As String)
                Dim UTF8 As System.Text.UTF8Encoding = New System.Text.UTF8Encoding
                Dim HashProvider As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()
                bPassword = HashProvider.ComputeHash(UTF8.GetBytes(value))
                sPassword = value
            End Set
        End Property

    #Region "Encrypt"

        ' Encrypt using Password from Property Set (pre-hashed)
        Public Function Encrypt(ByVal Message As String) As String
            Dim Results() As Byte
            Dim UTF8 As System.Text.UTF8Encoding = New System.Text.UTF8Encoding
            Using HashProvider As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()
                Dim TDESKey() As Byte = bPassword
                Using TDESAlgorithm As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider() With {.Key = TDESKey, .Mode = CipherMode.ECB, .Padding = PaddingMode.PKCS7}
                    Dim DataToEncrypt() As Byte = UTF8.GetBytes(Message)
                    Try
                        Dim Encryptor As ICryptoTransform = TDESAlgorithm.CreateEncryptor
                        Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length)
                    Finally
                        TDESAlgorithm.Clear()
                        HashProvider.Clear()
                    End Try
                End Using
            End Using
            Return Convert.ToBase64String(Results)
        End Function

        ' Encrypt using Password as byte array
        Private Function Encrypt(ByVal Message As String, ByVal Password() As Byte) As String
            Dim Results() As Byte
            Dim UTF8 As System.Text.UTF8Encoding = New System.Text.UTF8Encoding
            Using HashProvider As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()
                Dim TDESKey() As Byte = HashProvider.ComputeHash(UTF8.GetBytes(UTF8.GetString(Password)))
                Using TDESAlgorithm As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider() With {.Key = TDESKey, .Mode = CipherMode.ECB, .Padding = PaddingMode.PKCS7}
                    Dim DataToEncrypt() As Byte = UTF8.GetBytes(Message)
                    Try
                        Dim Encryptor As ICryptoTransform = TDESAlgorithm.CreateEncryptor
                        Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length)
                    Finally
                        TDESAlgorithm.Clear()
                        HashProvider.Clear()
                    End Try
                End Using
            End Using
            Return Convert.ToBase64String(Results)
        End Function

        ' Encrypt using Password as string
        Public Function Encrypt(ByVal Message As String, ByVal Password As String) As String
            Dim Results() As Byte
            Dim UTF8 As System.Text.UTF8Encoding = New System.Text.UTF8Encoding
            ' Step 1. We hash the Passphrase using MD5
            ' We use the MD5 hash generator as the result is a 128 bit byte array
            ' which is a valid length for the Triple DES encoder we use below
            Using HashProvider As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()
                Dim TDESKey() As Byte = HashProvider.ComputeHash(UTF8.GetBytes(Password))

                ' Step 2. Create a new TripleDESCryptoServiceProvider object

                ' Step 3. Setup the encoder
                Using TDESAlgorithm As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider() With {.Key = TDESKey, .Mode = CipherMode.ECB, .Padding = PaddingMode.PKCS7}
                    ' Step 4. Convert the input string to a byte[]

                    Dim DataToEncrypt() As Byte = UTF8.GetBytes(Message)

                    ' Step 5. Attempt to encrypt the string
                    Try
                        Dim Encryptor As ICryptoTransform = TDESAlgorithm.CreateEncryptor
                        Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length)
                    Finally
                        ' Clear the Triple Des and Hashprovider services of any sensitive information
                        TDESAlgorithm.Clear()
                        HashProvider.Clear()
                    End Try
                End Using
            End Using

            ' Step 6. Return the encrypted string as a base64 encoded string
            Return Convert.ToBase64String(Results)
        End Function
    #End Region

    #Region "Decrypt"
        ' Decrypt using Password from Property (pre-hashed)
        Public Function Decrypt(ByVal Message As String) As String
            Dim Results() As Byte
            Dim UTF8 As System.Text.UTF8Encoding = New System.Text.UTF8Encoding
            Using HashProvider As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()
                Dim TDESKey() As Byte = Me.bPassword
                Using TDESAlgorithm As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider() With {.Key = TDESKey, .Mode = CipherMode.ECB, .Padding = PaddingMode.PKCS7}
                    Dim DataToDecrypt() As Byte = Convert.FromBase64String(Message)
                    Try
                        Dim Decryptor As ICryptoTransform = TDESAlgorithm.CreateDecryptor
                        Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length)
                    Finally
                        TDESAlgorithm.Clear()
                        HashProvider.Clear()
                    End Try
                End Using
            End Using
            Return UTF8.GetString(Results)
        End Function

        ' Decrypt using Password as Byte array
        Public Function Decrypt(ByVal Message As String, ByVal Password() As Byte) As String
            Dim Results() As Byte
            Dim UTF8 As System.Text.UTF8Encoding = New System.Text.UTF8Encoding
            Using HashProvider As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()
                Dim TDESKey() As Byte = HashProvider.ComputeHash(UTF8.GetBytes(UTF8.GetString(Password)))
                Using TDESAlgorithm As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider() With {.Key = TDESKey, .Mode = CipherMode.ECB, .Padding = PaddingMode.PKCS7}
                    Dim DataToDecrypt() As Byte = Convert.FromBase64String(Message)
                    Try
                        Dim Decryptor As ICryptoTransform = TDESAlgorithm.CreateDecryptor
                        Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length)
                    Finally
                        TDESAlgorithm.Clear()
                        HashProvider.Clear()
                    End Try
                End Using
            End Using
            Return UTF8.GetString(Results)
        End Function


        ' Decrypt using Password as string
        Public Function Decrypt(ByVal Message As String, ByVal Password As String) As String
            Dim Results() As Byte
            Dim UTF8 As System.Text.UTF8Encoding = New System.Text.UTF8Encoding

            ' Step 1. We hash the pass phrase using MD5
            ' We use the MD5 hash generator as the result is a 128-bit byte array
            ' which is a valid length for the Triple DES encoder we use below
            Using HashProvider As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()
                Dim TDESKey() As Byte = HashProvider.ComputeHash(UTF8.GetBytes(Password))

                ' Step 2. Create a new TripleDESCryptoServiceProvider object
                ' Step 3. Setup the decoder
                Using TDESAlgorithm As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider() With {.Key = TDESKey, .Mode = CipherMode.ECB, .Padding = PaddingMode.PKCS7}

                    ' Step 4. Convert the input string to a byte[]
                    Dim DataToDecrypt() As Byte = Convert.FromBase64String(Message)
                    ' Step 5. Attempt to decrypt the string
                    Try
                        Dim Decryptor As ICryptoTransform = TDESAlgorithm.CreateDecryptor
                        Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length)
                    Finally

                        ' Clear the Triple Des and Hash provider services of any sensitive information
                        TDESAlgorithm.Clear()
                        HashProvider.Clear()
                    End Try
                End Using
            End Using

            ' Step 6. Return the decrypted string in UTF8 format
            Return UTF8.GetString(Results)
        End Function

    #End Region

    End Class

VB.NET 三重 DES 类用法

    Dim tdes As New TripleDES("12345")
    Dim vbEncrypted = tdes.Encrypt("Encrypted using VB.NET")
    Dim phpEncrypted = "5Ittyr0+jiI7QQmPrvSVnMc9MEWQCjAN"

    Debug.Print("PHP Encrypted: " & phpEncrypted)
    Debug.Print("VB Encrypted: " & vbEncrypted)
    Debug.Print("PHP Encrypted (decrypted result): " & tdes.Decrypt(phpEncrypted))
    Debug.Print("VB Encrypted (decrypted result): " & tdes.Decrypt(vbEncrypted))

PHP 三重 DES 类

    class TripleDES {
        private $bPassword;
        private $sPassword;

        function __construct($Password) {
            $this->bPassword  = md5(utf8_encode($Password),TRUE);
            $this->bPassword .= substr($this->bPassword,0,8);
            $this->sPassword - $Password;
        }

        function Password($Password = "") {
            if($Password == "") {
                return $this->sPassword;
            } else {
                $this->bPassword  = md5(utf8_encode($Password),TRUE);
                $this->bPassword .= substr($this->bPassword,0,8);
                $this->sPassword - $Password;
            }
        }

        function PasswordHash() {
            return $this->bPassword;
        }

        function Encrypt($Message, $Password = "") {
            if($Password <> "") { $this->Password($Password); }
            $size=mcrypt_get_block_size('tripledes','ecb');
            $padding=$size-((strlen($Message)) % $size);
            $Message .= str_repeat(chr($padding),$padding);
            $encrypt  = mcrypt_encrypt('tripledes',$this->bPassword,$Message,'ecb');
            return base64_encode($encrypt);
        }

        function Decrypt($message, $Password = "") {
            if($Password <> "") { $this->Password($Password); }
            return trim(mcrypt_decrypt('tripledes', $this->bPassword, base64_decode($message), 'ecb'), ord(2));
        }

    }

PHP 三重 DES 类用法

    $tdes = new TripleDES("12345");

    $phpEncrypted = $tdes->encrypt("Encrypted using PHP");
    $vbEncrypted = "5Ittyr0+jiI7QQmPrvSVnP3s2CeoTJmF"; // Encrypted using VB.NET

    echo "PHP Encrypted: " . $phpEncrypted . '<br />';
    echo "VB Encrypted: " . $vbEncrypted . '<br />';
    echo "PHP Encrypted (decrypted result): " . $tdes->Decrypt($phpEncrypted) . '<br />';
    echo "VB Encrypted (decrypted result): " . $tdes->Decrypt($vbEncrypted) . '<br />';

我尽我所能使两个类的可用性级别与语言自然允许的相同。由于 PHP 不允许重载函数,我不得不使用密码作为可选参数,它是一个字符串值。VB.NET 解决方案有一个额外的覆盖,允许您在加密/解密函数上传递密码字符串的字节值。提供的示例代码展示了每个类的用法,展示了实例化对象的最简单形式,这两个类都允许在对象创建时设置密码。

对于那些绞尽脑汁试图为 Triple DES 找到可行的双向解决方案的其他人(并且不想被迫进入每个人似乎都指向的框 - Rijndael),那么这个解决方案适用于你,你可以停止用头撞墙。

添加了 VB.NET TripleDES 类的 C# 翻译

C# 类(已添加 [2017-01-11]

using System;
using System.Security.Cryptography;

public class TripleDES {

    private byte[] bPassword;

    private string sPassword;
    public TripleDES( string Password = "password" ) {
        // On Class Begin
        this.Password = Password;
    }

    public string PasswordHash {
        get {
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
            return UTF8.GetString( bPassword );
        }
    }

    public string Password {
        get {
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
            return sPassword;
        }
        set {
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            bPassword = HashProvider.ComputeHash( UTF8.GetBytes( value ) );
            sPassword = value;
        }
    }

    #region "Encrypt"

    // Encrypt using Password from Property Set (pre-hashed)
    public string Encrypt( string Message ) {
        byte[] Results = null;
        System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
        using ( MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider() ) {
            byte[] TDESKey = bPassword;
            using ( TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider { Key = TDESKey, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 } ) {
                byte[] DataToEncrypt = UTF8.GetBytes( Message );
                try {
                    ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
                    Results = Encryptor.TransformFinalBlock( DataToEncrypt, 0, DataToEncrypt.Length );
                } finally {
                    TDESAlgorithm.Clear();
                    HashProvider.Clear();
                }
            }
        }
        return Convert.ToBase64String( Results );
    }

    // Encrypt using Password as byte array
    private string Encrypt( string Message, byte[] Password ) {
        byte[] Results = null;
        System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
        using ( MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider() ) {
            byte[] TDESKey = HashProvider.ComputeHash( UTF8.GetBytes( UTF8.GetString( Password ) ) );
            using ( TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider { Key = TDESKey, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 } ) {
                byte[] DataToEncrypt = UTF8.GetBytes( Message );
                try {
                    ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
                    Results = Encryptor.TransformFinalBlock( DataToEncrypt, 0, DataToEncrypt.Length );
                } finally {
                    TDESAlgorithm.Clear();
                    HashProvider.Clear();
                }
            }
        }
        return Convert.ToBase64String( Results );
    }

    // Encrypt using Password as string
    public string Encrypt( string Message, string Password ) {
        byte[] Results = null;
        System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
        // Step 1. We hash the Passphrase using MD5
        // We use the MD5 hash generator as the result is a 128 bit byte array
        // which is a valid length for the Triple DES encoder we use below
        using ( MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider() ) {
            byte[] TDESKey = HashProvider.ComputeHash( UTF8.GetBytes( Password ) );

            // Step 2. Create a new TripleDESCryptoServiceProvider object

            // Step 3. Setup the encoder
            using ( TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider { Key = TDESKey, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 } ) {
                // Step 4. Convert the input string to a byte[]

                byte[] DataToEncrypt = UTF8.GetBytes( Message );

                // Step 5. Attempt to encrypt the string
                try {
                    ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
                    Results = Encryptor.TransformFinalBlock( DataToEncrypt, 0, DataToEncrypt.Length );
                } finally {
                    // Clear the Triple Des and Hashprovider services of any sensitive information
                    TDESAlgorithm.Clear();
                    HashProvider.Clear();
                }
            }
        }

        // Step 6. Return the encrypted string as a base64 encoded string
        return Convert.ToBase64String( Results );
    }
    #endregion

    #region "Decrypt"
    // Decrypt using Password from Property (pre-hashed)
    public string Decrypt( string Message ) {
        byte[] Results = null;
        System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
        using ( MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider() ) {
            byte[] TDESKey = this.bPassword;
            using ( TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider { Key = TDESKey, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 } ) {
                byte[] DataToDecrypt = Convert.FromBase64String( Message );
                try {
                    ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
                    Results = Decryptor.TransformFinalBlock( DataToDecrypt, 0, DataToDecrypt.Length );
                } finally {
                    TDESAlgorithm.Clear();
                    HashProvider.Clear();
                }
            }
        }
        return UTF8.GetString( Results );
    }

    // Decrypt using Password as Byte array
    public string Decrypt( string Message, byte[] Password ) {
        byte[] Results = null;
        System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
        using ( MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider() ) {
            byte[] TDESKey = HashProvider.ComputeHash( UTF8.GetBytes( UTF8.GetString( Password ) ) );
            using ( TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider { Key = TDESKey, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 } ) {
                byte[] DataToDecrypt = Convert.FromBase64String( Message );
                try {
                    ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
                    Results = Decryptor.TransformFinalBlock( DataToDecrypt, 0, DataToDecrypt.Length );
                } finally {
                    TDESAlgorithm.Clear();
                    HashProvider.Clear();
                }
            }
        }
        return UTF8.GetString( Results );
    }


    // Decrypt using Password as string
    public string Decrypt( string Message, string Password ) {
        byte[] Results = null;
        System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

        // Step 1. We hash the pass phrase using MD5
        // We use the MD5 hash generator as the result is a 128-bit byte array
        // which is a valid length for the Triple DES encoder we use below
        using ( MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider() ) {
            byte[] TDESKey = HashProvider.ComputeHash( UTF8.GetBytes( Password ) );

            // Step 2. Create a new TripleDESCryptoServiceProvider object
            // Step 3. Setup the decoder
            using ( TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider { Key = TDESKey, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 } ) {

                // Step 4. Convert the input string to a byte[]
                byte[] DataToDecrypt = Convert.FromBase64String( Message );
                // Step 5. Attempt to decrypt the string
                try {
                    ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
                    Results = Decryptor.TransformFinalBlock( DataToDecrypt, 0, DataToDecrypt.Length );

                } finally {
                    // Clear the Triple Des and Hash provider services of any sensitive information
                    TDESAlgorithm.Clear();
                    HashProvider.Clear();
                }
            }
        }

        // Step 6. Return the decrypted string in UTF8 format
        return UTF8.GetString( Results );
    }

    #endregion
}
于 2014-07-12T18:35:32.400 回答
0

对于 PHP 7+ mccrypt 不起作用,所以这里使用库phpseclib进行加密和解密的解决方案:

 $des = new \phpseclib\Crypt\TripleDES(\phpseclib\Crypt\TripleDES::MODE_ECB);
 $key = YOURKEY;
 $p  = md5(utf8_encode($key),TRUE);
 $p .= substr($p,0,8);
 $p - $key;
 $des->setKey($p);

 $phpEncrypted = $des->encrypt("Encrypted using PHP");
 $vbEncrypted = "5Ittyr0+jiI7QQmPrvSVnP3s2CeoTJmF"; // Encrypted using VB.NET

 echo "PHP Encrypted: " . $phpEncrypted . '<br />';
 echo "VB Encrypted: " . $vbEncrypted . '<br />';
 echo "PHP Encrypted (decrypted result): " . $des->decrypt(base64_decode($phpEncrypted)) . '<br />';
 echo "VB Encrypted (decrypted result): " . $des->decrypt(base64_decode($vbEncrypted)) . '<br />';
 
于 2021-09-17T14:04:51.577 回答