gpt4 book ai didi

php - PHP中的解密

转载 作者:行者123 更新时间:2023-12-04 05:46:26 24 4
gpt4 key购买 nike

我正在尝试解密传送到我们服务器的数据。它是使用某种方案加密的某个 8 位数字。我有加密和完整性 key 。我有一个关于如何解密它的文档,上面写着 -

The value is encrypted using a custom encryption scheme. The encryption scheme uses a keyed HMAC-SHA1 algorithm to generate a secret pad based on the unique event ID. The encrypted value has a fixed length of 28 bytes. It is comprised of a 16-byte initialization vector, 8 bytes of ciphertext, and a 4-byte integrity signature. The encrypted value is web-safe base-64-encoded, according to RFC 3548, with padding characters omitted. Thus, the 28-byte encrypted value is encoded as a 38 character web-safe base-64 string. The value is encrypted as:

value xor HMAC-SHA1(encryption_key, initialization_vector)>

so decryption calculates:

HMAC-SHA1(encryption_key, initialization_vector)

and xor's with the encrypted value to reverse the encryption. The integrity stage takes 4 bytes of

HMAC-SHA1(integrity_key, value||initialization_vector)>

where || is concatenation.



所以我写了以下 PHP 代码。
$value= "[VALUE]"; //38 character base64
$ekey=hextostr("[ENCRYPTIONKEY]"); //64 byte hex encoded key . 32 byte key
$ikey=hextostr("[INTEGRITYKEY]"); //64 byte hex encoded key . 32 byte key

$value=str_replace("-","+",$value);
$value=str_replace("_","/",$value);
$value=$value."==";
$dvalue=base64_decode($value); //Gets a 28 byte encrypted string.

$initvec=substr($dvalue,0,16);
$ciphertext=substr($dvalue,16,8);
$integritysig=substr($dvalue,24,4);

$pad=hash_hmac("sha1",$initvec,$ekey); //Generates 40 byte pad

$uncipher=$ciphertext^$pad;

print($uncipher); //This is 8 byte binary. Dumps some binary on screen. Result should be a 8 byte number

无法解决这个问题。请指教。

最佳答案

$pad=hash_hmac("sha1",$initvec,$ekey); // returns a hexstring, but XOR interprets
// as ASCII string and converts to binary
// accordingly

$ciphertext=substr($dvalue,16,8); // this is ASCII, converted to binary by XOR

$uncipher=$ciphertext^$pad; // so the XOR operation is confused in interpretation.

尝试将其更改为,
function bin2asc($in)#syntax - bin2asc("binary to convert");
{
$out = '';
for ($i = 0, $len = strlen($in); $i < $len; $i += 8)
{
$out .= chr(bindec(substr($in,$i,8)));
}
return $out;
}

$pad= hash_hmac("sha1",$initvec,$ekey, true); // now it will return in binary
$pad = bin2asc($pad);

$uncipher=$ciphertext^$pad;

希望这能解决您的问题。

关于php - PHP中的解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10628771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com