我正在尝试使用预先生成的SubtleCrypto解密字符串。
我没有得到解密的文本,而是收到错误:无法在“SubtleCrypto”上执行“解密”:参数 2 不是“CryptoKey”类型。
console.log(window.crypto.subtle.decrypt({name:"AES-CBC", iv:""}, "1234567890123456", "i4+WxNH8XYMnAm7RsRkfOw=="));
Run Code Online (Sandbox Code Playgroud)
我尝试研究该错误,但没有遇到与这样一个基本示例相关的任何内容。我究竟做错了什么?
在服务器端,我使用PyCryptodome使用RSA-OAEP(使用 SHA-256)加密消息。
我正在尝试在客户端使用SubtleCrypto Web Crypto API解密消息,但它给了我一个DOMException错误,没有进一步的细节。
在 SubtleCrypto 上,我可以毫无问题地导入在 PyCryptodome 中生成的私钥,但是当我尝试解密消息时,它给了我错误。
我还尝试导入在客户端的 PyCryptodome 上生成的公钥,以使用 SubtleCrypto 加密相同的消息。在那种情况下,我可以毫无问题地解密它,使用与以前相同的流程。
这两个库之间的 RSA-OAEP 算法是否不兼容?我注意到 PyCryptodome在各自的文档中引用了RFC 8017(v2.2)和 SubtleCrypto RFC 3447(v2.1)。
编辑:
服务器端代码(pycryptodome==3.9.8):
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
class Cipher:
def rsa_encrypt(self, data, key_string):
key = RSA.importKey(key_string)
rsa_encryption_cipher = PKCS1_OAEP.new(key)
ciphertext = rsa_encryption_cipher.encrypt(data)
return base64.b64encode(ciphertext)
def rsa_decrypt(self, data, key_string):
data = base64.b64decode(data)
key = RSA.importKey(key_string)
rsa_decryption_cipher = PKCS1_OAEP.new(key)
plaintext = rsa_decryption_cipher.decrypt(data)
return plaintext …Run Code Online (Sandbox Code Playgroud)所以我试图实现一些方法来加密然后解密一些数据。我对此没有任何经验,并且我尝试按照网上的一些帖子了解如何进行此操作。
\n当我将加密的“hello”传递给解密函数时,我得到:
\nlet a = importPublicKeyAndEncrypt('hello')\n\nCryptoKey\xc2\xa0{type: "public", extractable: true, algorithm: {\xe2\x80\xa6}, usages: Array(1)} W29iamVjdCBBcnJheUJ1ZmZlcl0=\n\nimportPrivateKeyAndDecrypt(a)\nPromise\xc2\xa0{<pending>}\n\nDOMException: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.\nat importPrivateKeyAndDecrypt (<anonymous>:26:60)\nRun Code Online (Sandbox Code Playgroud)\n当我使用我在 PKCS#8 看到的一篇帖子中的加密消息时,解密功能可以正常工作,但当我生成自己的密钥时,解密功能就不能正常工作。
\n这是代码\n我做错了什么?
\n// PEM encoded X.509 key\nconst publicKey = `\n-----BEGIN PUBLIC KEY-----\n <removed for space>\n-----END PUBLIC KEY-----`;\n\n// PEM encoded PKCS#8 key\nconst privateKey = `\n-----BEGIN PRIVATE KEY-----\n <removed for space>\n-----END PRIVATE KEY-----`;\n\nasync function importPublicKeyAndEncrypt(str) {\n try {\n const pub = await importPublicKey(publicKey);\n console.log(pub);\n …Run Code Online (Sandbox Code Playgroud) 我有一些使用 SubtleCrypto 的代码来加密密钥并将其存储在数据库中。
对于另一个功能,我必须能够在 Node 12 中对其进行加密。如果没有 SubtleCrypto,我必须在 Crypto 中重新创建该功能。
我得到相同大小的输出,但它似乎无法被 SubtleCrypto 解密,我试图找出哪里出了问题。
这是在浏览器上使用 SubtleCrypto 运行的代码:
key = getKeyMaterial(password)];
salt = base64ToArraybuffer(optionalSalt)
aesKey = crypto.subtle.deriveKey(
{
name: constants.algorithms.pbkdf2,
salt: salt,
iterations: pbkdf2Iterations,
hash: { name: constants.hash.sha256 },
},
key,
{
name: constants.algorithms.aesGcm,
length: aesWrapKeyBitsLength,
},
true,
['wrapKey', 'unwrapKey']
),
return {
salt: arraybufferTobase64(salt),
aesKey: aesKey,
}
function getKeyMaterial(password) {
var enc = new TextEncoder();
crypto.subtle.importKey(
constants.format.raw,
enc.encode(password),
{
name: constants.algorithms.pbkdf2,
},
false,
['deriveKey']
)
Run Code Online (Sandbox Code Playgroud)
如果没有 SubtleCrypto,在 Node 12 中,我被迫使用 Crypto …
subtlecrypto ×4
encryption ×3
javascript ×3
rsa ×2
cryptojs ×1
node-crypto ×1
node.js ×1
pycryptodome ×1