donglengyuan6826 2017-06-20 14:34
浏览 66

用JS Forge解密PHP openssl_seal输出

I am trying to decrypt my openssl_seal stored values. My PHP code:

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
    ];
}

and my client-side code:

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

However I keep getting 'forge.min.js:4068 Uncaught Error: Encryption block is invalid.'. Is there something I am missing here?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料