2

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

该值使用自定义加密方案进行加密。加密方案使用带密钥的 HMAC-SHA1 算法根据唯一事件 ID 生成密钥。加密值具有 28 字节的固定长度。它由 16 字节的初始化向量、8 字节的密文和 4 字节的完整性签名组成。根据 RFC 3548,加密值是 web 安全的 base-64 编码,省略了填充字符。因此,28 字节的加密值被编码为 38 个字符的 Web 安全 base-64 字符串。该值被加密为:

值异或 HMAC-SHA1(加密密钥,初始化向量)>

所以解密计算:

HMAC-SHA1(加密密钥,初始化向量)

和 xor 与加密的值来反转加密。完整性阶段需要 4 个字节

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

在哪里 || 是串联。

所以我写了下面的 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

无法绕过这个问题。请指教。

4

3 回答 3

1
$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;

希望这能解决你的问题。

于 2012-05-17T10:34:56.800 回答
1

您发布的代码应如下所示

$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);

//here is the change
$pad=hash_hmac("sha1",$initvec,$ekey, true);

$uncipher=$ciphertext^$pad;

print(hexdec(strToHex($uncipher))); //This is 8 byte binary. Dumps some binary on screen. Result should be a 8 byte number
于 2012-05-29T17:03:58.220 回答
1

试试这个

function decrypt_google_winning_price($value, $ekey, $ikey, &$reason = '') {
if (strlen($value) != 38)
{
    $reason = "Wrong encrypted value length";
    return false;
}

$ekey = base64_decode($ekey);
$ikey = base64_decode($ikey);
$value = strtr($value, '-_,', '+/=') . "==";
$enc_value = base64_decode($value); //Gets a 28 byte encrypted string.
if (strlen($enc_value) != 28)
{
    $reason = "Wrong encrypted value length after base64_decode()";
    return false;
}

$iv = substr($enc_value, 0, 16);// initialization vector (16 bytes - unique to the impression)
$p = substr($enc_value, 16, 8); // encryption key (32 bytes - provided at account set up)
$sig = substr($enc_value, 24, 4);// integrity signature (4 bytes)
$price_pad = hash_hmac("sha1", $iv, $ekey, true);
$price = $p ^ $price_pad;// XOR

$conf_sig = substr(hex2bin(hash_hmac("sha1", $price . $iv, $ikey)), 0, 4);

if ($sig !== $conf_sig)
{
    $reason = "Signature is not valid";
    return false;
}

return  hexdec(bin2hex($price)); //This is 8 byte binary. Dumps some binary on screen. Result should be a 8 byte number
}


$value = "[VALUE]"; //38 character base64
$ekey = "[ENCRYPTIONKEY]"; //64 byte hex encoded key . 32 byte key
$ikey "[INTEGRITYKEY]"; //64 byte hex encoded key . 32 byte key

var_dump(decrypt_google_winning_price($value, $ekey, $ikey));
于 2016-04-25T14:33:33.853 回答