Rsa

RSA 加密私鑰中的 IV

  • February 20, 2021

我已經獲得了一個 RSA 加密私鑰,它是在 NodeJS(加密)中使用以下程式碼生成的:

generateKeyPairSync( "rsa", {
       modulusLength: 4096,
       publicKeyEncoding: {
         type: "spki",
         format: "pem"
       },
       privateKeyEncoding: {
         type: "pkcs8",
         format: "pem",
         cipher: "aes-256-cbc",
         passphrase: "aWCTJPET9fL7UBTp97hX99gdofeWKUf5tuxSuJeST2sEkyvkyinrfrj6EiSUTErF"
       }
     } )

Note: enforcing padding in encrypt and decrypt padding: RSA_PKCS1_PADDING

我的任務是通過解密 ENCRYPTED PRIVATE KEY 來獲取 PRIVATE KEY。我有如上所示的密碼和其他詳細資訊。除了IV之外的一切。

這必須手動完成,因為我必須使用 Java,它有一些問題:

java.security.NoSuchAlgorithmException: Cannot find any provider supporting 1.2.840.113549.1.5.13  (Password-Based Encryption Scheme 2 (PBES2))

不幸的是,NodeJS(加密)中用於生成 ENCRYPTED PRIVATE KEY 的庫不提供與密碼組合解密 AES-256 CBC 加密 PRIVATE KEY 所需的 IV。

我正在閱讀可以從密鑰中獲取 IV,這就是 NodeJS 庫能夠解密文件的方式,同時只要求使用者輸入密碼。

我想知道如何從加密的私鑰中導出 IV。

AES 中的 IV 始終是塊的大小,在 AES 中是 128 位或 16 字節。所以我看待它的方式是哪 16 個字節是加密私鑰中的 IV。也很高興知道如何從 16 字節

$$ $$重建字節以獲得IV。 我附上了所有形式的密鑰——加密、解密和相關的公鑰——因為您可能需要它來進行測試。

加密私鑰

-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIJrTBXBgkqhkiG9w0BBQ0wSjApBgkqhkiG9w0BBQwwHAQIRZpyCAQQR8oCAggA
MAwGCCqGSIb3DQIJBQAwHQYJYIZIAWUDBAEqBBC8h4heO8aR5NaJq5o89myBBIIJ
UDo2+MIuXH+y+hB9fx1kVe7yIogh0DEeYTnTLAraeg2yudSoNLq4xF2Ftid7Ax9Q
6BwdrY6YB8/Pk9vn7gWBoT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DmaY1r6
CE5QIv0d9ffk/5tmzEgF0hhVVz0hgeyc9zOP38IlkFbd9naQz5jfrYezByEY6+/t
uPCzl76nrhcHgpxXsenenkhEGGVMYHEFHvsfmetdbP/TFYfaxCdjP5rcmdI5vnBI
7MYEhBqo+QWaF1fy4I4L8UMl1Kzqh4bc4s4w9r2d+JDHS7uwZBY9iwRSD4ChJyzH
GYBhnCNBmHz78F+hTlGrxLgGGouQXTxsGu1XUTUPqUgDXPRuD+plKtiJ10VADBsL
/WQaIbJF7CoWWvZNDqd1kiBNDdUmTfSw/VIIUjLuJpJIsZlnK3PY0/nA0FWBtK71
YsKNM/QFSQ6VQ78tMd0BCPEaMxCHaabsas8QZE4zteSKAJ5EigmggcjtiP63cZnp
iijX6av6myULqz94+yD4qyTGWyiA1JtqQACn6yBHhyUbs4y5/xTwGy9nFi0LI4IA
mmFPquGOLG1Vsy6b9KCoq9HiCjPPx+S28g76JDTC5cq2sU0lPJRGQfhIzUsDW7Pf
U0+rnUQRxi+rznMEsllj1uDQa1T608IjVRyF93vl4ni0QOY7ZYLH1qiK2YSAbhQ3
4krHOc7bUbWy7MyNRF1l+jcmgbxceC9Y/FhKyoZT2FSYHlOG/P95osLGXKjkAQYl
jVTPIlD4+5ZNhWLyfzDwlAMCAwEAAQ==
-----END PUBLIC KEY-----

openssl asn1parse -in 密鑰文件名

你會看到一堆輸出,我相信“aes-256-cbc”之後的欄位是IV

 0:d=0  hl=4 l=2477 cons: SEQUENCE
 4:d=1  hl=2 l=  87 cons: SEQUENCE
 6:d=2  hl=2 l=   9 prim: OBJECT            :PBES2
17:d=2  hl=2 l=  74 cons: SEQUENCE
19:d=3  hl=2 l=  41 cons: SEQUENCE
21:d=4  hl=2 l=   9 prim: OBJECT            :PBKDF2
32:d=4  hl=2 l=  28 cons: SEQUENCE
34:d=5  hl=2 l=   8 prim: OCTET STRING      [HEX DUMP]:459A7208041047CA
44:d=5  hl=2 l=   2 prim: INTEGER           :0800
48:d=5  hl=2 l=  12 cons: SEQUENCE
50:d=6  hl=2 l=   8 prim: OBJECT            :hmacWithSHA256
60:d=6  hl=2 l=   0 prim: NULL
62:d=3  hl=2 l=  29 cons: SEQUENCE
64:d=4  hl=2 l=   9 prim: OBJECT            :aes-256-cbc
75:d=4  hl=2 l=  16 prim: OCTET STRING      [HEXDUMP]:BC87885E3BC691E4D689AB9A3CF66C81

我相信 0800(即 1024)是使用的 PBKDF2 的迭代次數。

引用自:https://crypto.stackexchange.com/questions/88362