用JS Forge解密PHP openssl_seal输出

时间:2017-06-20 14:34:43

标签: javascript php encryption php-openssl forge

我正在尝试解密我的openssl_seal存储值。 我的PHP代码:

public static function multiEncrypt( string $data, array $publicKeys ) : ?array
{
    foreach ($publicKeys as $cert)
    {
        if (false === openssl_get_publickey( $cert )) {
            Log::getInstance()->error('multiEncrypt : invalid pubkey ; ' . openssl_error_string());
            return null;
        }
    }

    $encryptedData = null;
    $encryptedKeys = [];
    $initVector = self::getInitVector( self::SEAL_CIPHER );

    $result = openssl_seal(
        $data,
        $encryptedData,
        $encryptedKeys,
        $publicKeys,
        self::SEAL_CIPHER,
        $initVector
    );

    if (false === $result)
        return null;

    $encodedData = base64_encode( $encryptedData );
    $encodedKeys = [];
    foreach ($encryptedKeys as $key) {
        $encodedKeys[] = base64_encode($key);
    }

    return [
        'keys' => $encodedKeys,
        'data' => $encodedData
    ];
}

和我的客户端代码:

    function decrypt(privkey, blob, key)
    {
        var byteKey = forge.pki.privateKeyFromPem(privkey);
        var usrPubKey = forge.pki.publicKeyFromPem(publicKey);
        //var genPubKey = forge.pki.rsa.setPublicKey(byteKey.n, byteKey.e);

        /*if (usrPubKey !== genPubKey) {
            error('Your private key does not match the public key we have of you.');
            return;
        }*/

        console.log('Decoding key..');
        var decodedKey = atob(key);
        console.log('Decrypting key..');
        var contractUserKey = byteKey.decrypt(decodedKey);

        console.log(contractUserKey);

        console.log(contractUserKey.decrypt(blob));
}

但是我一直得到'forge.min.js:4068未捕获错误:加密块无效。'。我在这里缺少什么吗?

0 个答案:

没有答案