Javascript WebCryptoAPI:如何使用公钥解密RSA加密的消息?

Javascript WebCryptoAPI:如何使用公钥解密RSA加密的消息?,javascript,cryptography,rsa,webcrypto-api,Javascript,Cryptography,Rsa,Webcrypto Api,我有一条RSA加密的消息,可以用openssl解码: openssl rsautl -inkey cert.pem -pubin -in encrypted -out plaintext 如何使用WebCryptoAPI实现这一点?我在尝试以下操作时出错: window.crypto.subtle.importKey( "spki", new Uint8Array(pubKey), { name: "RSA-OAEP", hash: {name: "SHA-256"} }, true,

我有一条RSA加密的消息,可以用openssl解码:

openssl rsautl -inkey cert.pem -pubin -in encrypted -out plaintext
如何使用WebCryptoAPI实现这一点?我在尝试以下操作时出错:

window.crypto.subtle.importKey(
"spki",
new Uint8Array(pubKey),
{
  name: "RSA-OAEP",
  hash: {name: "SHA-256"}
},
true,
["verify"]
)
.then(function(publicKey){

  console.log(publicKey);

  window.crypto.subtle.decrypt(
    {
      name: "RSA-OAEP"
    },
    publicKey,
    new Uint8Array(encrypted)
    )
    .then(function(decrypted){
      console.log(new Uint8Array(decrypted));
    })
    .catch(function(err){
      console.error(err);
    });

})
.catch(function(err){
  throw(err);
});

(请参阅)

正如Artjom B.在其评论中指出的那样,Webcrypto API的验证操作可以做到这一点。然而,在我的例子中,PKCS#1v1.5是必需的,因为我在评论中提到的ASN.1结构是PKCS#1v1.5规范的一部分

可以使用以下OpenSSL命令生成测试签名:

openssl dgst -sha1 -sign private_key.pem -out signature data
可以通过以下方式验证创建的签名:

openssl dgst -sha1 -verify public_key.pem -signature signature data

这个问题是因为没有真正理解RFC 3477。

对于初学者来说,你不能用公钥“解密”任何东西。这将是验证签名,但随后您将使用“RSA-PSS”而不是“RSA-OAEP”。从技术上讲,这与RSA中的操作相同。当私钥用于加密而公钥用于解密时,签名和验证只是它的不同名称。但回到我的问题上来:RSA-PSS用公钥“解密”签名并将其与经过验证的数据的哈希版本进行比较,我理解对了吗?因为在我的应用程序中,散列被包装在ASN.1结构中,需要首先提取。OpenSSL不支持PSS进行签名验证,但只支持PKCS#1 v1.5填充,这也不同于OAEP。无论如何,您的openssl命令都很奇怪,因为您没有指定操作:
-decrypt
-verify
-encrypt
-sign
,我知道。但它只在没有操作的情况下工作。