如何在PHP中解密由SQL Server的EncryptByPassPhrase()加密的字符串?

时间:2019-04-22 11:43:19

标签: php sql sql-server tsql encryption

我有一个加密的字符串及其密钥,它是通过SQL Server使用“ EncryptByPassPhrase”创建的,如何在PHP中对其解密?

我已经阅读了“ EncryptByPassPhrase”的文档,该文档指出这是128长度的Triple DES加密。我尝试对PHP进行3DES解密,但未返回预期的输出。

MS SQL中的加密是通过

完成的
declare @encrypt varbinary(200) 
select @encrypt = EncryptByPassPhrase('key', 'taskseq=10000&amt=200.5' )
select @encrypt 

我正在使用以下方式在PHP中对其进行解密:

    function decryptECB($encrypted, $key) {
       $iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_ECB);
       $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
       // decrypting
       $stringText = mcrypt_decrypt(MCRYPT_3DES, $key, $encrypted, 
       MCRYPT_MODE_ECB, $iv);

       return $stringText;
    }

3 个答案:

答案 0 :(得分:1)

我自由地将this Stack Overflow answer转换为PHP。

这是结果:

<?php

// SQL Server's DecryptByPassphrase translated into PHP. 
function decrypt(string $data, string $password): ?string {
    // SQL Server <2017 uses SHA1 for the key and the DES-EDE-CBC crypto algorithm
    // whereas SQL Server >= 2017 uses SHA256 and AES-256-CBC. 
    // Version 1 is the SHA1 + DES-EDE-CBC version, Version 2 is the AES-256-CBC version.
    // Version is stored in the first four bytes as a little endian int32.
    $version_bytes = substr($data, 0, 4);
    $version = unpack('V', $version_bytes)[1];

    // Password must be converted to the UTF-16LE encoding.
    $passwordUtf16 = mb_convert_encoding($password, 'UTF-16LE');

    if ($version === 1) {
        // Key is hashed using SHA1, The first 16 bytes of the hash are used.
        $key = substr(hash('sha1', $passwordUtf16, true), 0, 16);
        $method = 'des-ede-cbc';
        $options = OPENSSL_RAW_DATA;
        $iv = substr($data, 4, 8); // initialization vector of 8 bytes
        $encrypted_data = substr($data, 12); // actual encrypted data
    } else if ($version === 2) {
        // Key is hashed using sha256. Key length is always 32 bytes.
        $key = hash('sha256', $passwordUtf16, true);
        $method = 'aes-256-cbc';
        $options = OPENSSL_RAW_DATA;
        $iv = substr($data, 4, 16); // iv of 16 bytes
        $encrypted_data = substr($data, 20);
    } else {
        throw new \InvalidArgumentException('Invalid version');
    }

    $decrypted = openssl_decrypt($encrypted_data, $method, $key, $options, $iv);

    if ($decrypted === false) {
        return null;
    }

    // First 8 bytes contain the magic number 0xbaadf00d and the length
    // of the decrypted data
    $decrypted = substr($decrypted, 8);

    // UTF-16 encoding should be converted to UTF-8. Note that
    // there might be a better way to accomplish this.
    $isUtf16 = strpos($decrypted, 0) !== false;

    if ($isUtf16) {
        return mb_convert_encoding($decrypted, 'UTF-8', 'UTF-16LE');
    }

    return $decrypted;
}

// A version 1 encrypted string. Taken directly from the Stack Overflow answer linked above
$s = '010000007854E155CEE338D5E34808BA95367D506B97C63FB5114DD4CE687FE457C1B5D5';
$password = 'banana';
$bin = hex2bin($s);
$d = decrypt($bin, $password);
var_dump($d); // string(6) "turkey"

// A version 2 encrypted string. Taken directly from the Stack Overflow answer linked above
$s = '02000000266AD4F387FA9474E825B013B0232E73A398A5F72B79BC90D63BD1E45AE3AA5518828D187125BECC285D55FA7CAFED61';
$password = 'Radames';
$bin = hex2bin($s);
$d = decrypt($bin, $password);
var_dump($d); // string(16) "LetTheSunShining"

旁注:mcrypt is deprecated as it has been abandoned for over a decade.

答案 1 :(得分:0)

在查询中使用以下内容

DECRYPTBYPASSPHRASE('key', [field] )

Reference

答案 2 :(得分:0)

EncryptByPassPhrase()使用的专有格式似乎没有现成的文档。解密的最佳选择是DecryptByPassPhrase()

此专有格式的目的是在应用程序的数据库层中使用-而不是跨应用程序/网络/语言。

如果您不愿意使用这种格式(我建议您不要这样做),则需要获取此格式的规范,包括使用哪种类型的密钥偏离功能将密码转换为实际的加密密钥等。

拥有此规范后,您将不得不自己实现此规范。