1
大家好:我使用NodeRSA生成由如下的公私密鑰對節點加密createDecipheriv拋出無效的密鑰長度錯誤
const key = new nodeRSA();
key.generateKeyPair(2048, 65537);
和刪除頁眉和頁腳和\n
,一對後看起來是這樣的:
//only for demo
"publicKey": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlJ3PpWq77KY+tFccFfH4bO65Nu82LyzX0FxYGZ/QaF+LuZqeXEErXUO5iJ3P+hPRtiHUEHr9d5mfqStA/DnXxBbFmbwatNE8ncu4pSBOr9pbUyt22BRVkaiBBN0IuzBwo4ZWNrsON7IolVJnh8BKyurO5r7uj3aa8Yz0wcwIIkgais967iFWoj8YyeMSXpqUuMQu4mvcWB0EY/+wJNQnlSf7WuoJaTtzPzLe0qBaGnYO01ASxeOgPRzAJJ8prz2xZ5xy1nW+w+88XHVQsQizJ32B5Mzw7CpAeKIAnhlMVs8gVVc7iCfiwLoMiZXSReNxl2WfIYbHpaH2e6kPZoYNLQIDAQAB",
"privateKey": "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCUnc+larvspj60VxwV8fhs7rk27zYvLNfQXFgZn9BoX4u5mp5cQStdQ7mInc/6E9G2IdQQev13mZ+pK0D8OdfEFsWZvBq00Tydy7ilIE6v2ltTK3bYFFWRqIEE3Qi7MHCjhlY2uw43siiVUmeHwErK6s7mvu6PdprxjPTBzAgiSBqKz3ruIVaiPxjJ4xJempS4xC7ia9xYHQRj/7Ak1CeVJ/ta6glpO3M/Mt7SoFoadg7TUBLF46A9HMAknymvPbFnnHLWdb7D7zxcdVCxCLMnfYHkzPDsKkB4ogCeGUxWzyBVVzuIJ+LAugyJldJF43GXZZ8hhselofZ7qQ9mhg0tAgMBAAECggEBAI3lt9Mq+89SU+CAo7JYXGg17uYCtGuKl2y8Huooayacw54tgTJ4jtz3wvgsJd2SetM5rW8m30gIiCdPwzaQS/FQBM9qiN1IQmYvk1cmZ7sD89qPT2O01pbCmp14JdQExid8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peEg=="
我通過以下線路使用上述公鑰來加密一些數據,後來被上面的私鑰解密(轉換緩衝)
// ivBuf is the buffer of initialization vector
crypto.createDecipheriv('aes-256-gcm', privateKeyBuf, ivBuf);
但節點加密抱怨:
crypto.js:235
this._handle.initiv(cipher, toBuf(key), toBuf(iv));
^
Error: Invalid key length
at Error (native)
at new Decipheriv (crypto.js:235:16)
at Object.Decipheriv (crypto.js:232:12)
問題是:爲什麼關鍵lenght是無效的?
謝謝!
您正在使用RSA密鑰創建AES解密。你忘了在你的問題中提到AES密鑰,或者你使用了錯誤的方法。可能你需要'crypto.privateDecrypt(private_key,buffer)' – pedrofb
@pedrofb是的,你說得對,我認爲它是錯誤的,但已經解決了,很快就會刪除這個問題。 – leonsPAPA