0

Is there a way to find out how an RSA private key is encrypted? The password for the private file is known, but there is no more information about the cipher in the private key file, only the information -----BEGIN ENCRYPTED PRIVATE KEY----- with encrypted data after that.

The background is, when I generate encrypted public/private key files with PHP/OpenSSL with given ciphers for EC keys for example, so I got information about the cipher like this

-----BEGIN EC PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC,...

But for password-protected RSA keys only the information -----BEGIN ENCRYPTED PRIVATE KEY----- without any additional information about the cipher.

2

OpenSSL supports the older "traditional" or "SSLeay" key formats and the more secure PKCS#8 format. openssl rsa command works with the traditional format and openssl pkcs8 command works with the pkcs8 format. The pkcs8 command can convert between formats.

Files with -----BEGIN RSA PRIVATE KEY----- are in the traditional format and files with -----BEGIN ENCRYPTED PRIVATE KEY----- are in the PKCS#8 format.

In the traditional format, the file has a header like:

Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC,DF92177B22C9D06622FF3FEC3491C204

and everything else is encrypted.

The PKCS#8 format is ASN.1 and so you can use any ASN.1 parser to read it, for example:

$ openssl asn1parse -in encrypted.pkcs8.pem

    0:d=0  hl=4 l=1325 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]:51A5F9E6AA2874EF
   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      [HEX DUMP]:98228059794F6B60E616AA7C2E80A625
   93:d=1  hl=4 l=1232 prim: OCTET STRING      [HEX DUMP]:E7913261B9FA2C4C916581FA21B317E710727C5952D815997C039CF5D5C7F1DF86E44FB9A4BD47DF6303A73EB858F94CB548FA2BA220DEDD02CC7643257BC8F67684183C89BD18744B93DE11C0F76C0E71C2AFBE85DA3F60932CEF0377563A74F8C01A7C2A701F095A79A8711BF238AE52F511CD3915D159AD79426FDFDEB042069F0D58D5673BC64CAD56E00C74F267F77B6138639549BE2F76CBFE2EA1F86DBAC7791EE0F5E90E192985A647CF3ECC71A34F4B9B0818FE22DA8435A97D8D643986ED349730B83D81EE2AE1A2B6BCCF8580D0EDD119912562D1631BD1217DE76AF25CDCBBDC4452FB9D5482BC64759622215B3B51231F98757ED12933DC0D6F0DD9139A5D3F3237E388DBE8BD8873AE6EC0635274084858FB5E8A7CE1439C5A4505521263DE8425932ADDE91A0AAC187855BA2754A1125653FC2714AB04890972D675A73F00689D870CC8A2F4E505A55A7EB9319D3F56FF1A197E91AE56218280A13E4FF08499AFAB9F5005C022D85B437F414255E9355F10733A92E95307B25AAF15579EE343F925A83C62439D73A30FB8C6843BD83C0AFBB3C3FB4BC439BCDE168F7BD52A4953C968FAE4763DC9472E2B95BAA84EC4BAB3CA89CBBE8B2E539BAF3F86B4C27747ADB45F7AEB59ADB3FE675682D7F5BFEFD06AE27A280CDDC831B24C4D25E0C9D4CDCDF6FB62AF7FF482468506816A29019275DBA901ED5A86864DD170EA773E7BA102E90D325884D20D816CF60B9A4FBF5CE612F7A7787A52488440F95B551FE31911FC09926C2D928FA03C604B322B1384F5AA31C0D60205798125FA98E3F45F6AC6D72F9382992A9206E124764C2FDCB8735C8D06A323EC8B0F0C4A2C5C78C44014FBB2E34B56B3322641D21197256DCEC04FF79410FEA0A07AC67F3CA5AED086276744956468633B5419F0F6A077ECDC070E8F4AF82DD51E8F3180A3B5A966B806B6F42C2727678DC5BAD2982EE5FFEB009BFAF84F9F32E40C07370699D7CB07E98BA78C60E83C3468074C389EF35C60C949301869D4295C8DA6F2CDBB9AC5B483BD7D2B23E0E950BAD37F07F27A7C16102A1692AC87B9CC9CB18AD7A63EBFD5C9A2FB7E22BAC2C73A7D20AF1874578CEA504E809C34F0EFA216A655D447BED8BA40FBA26B7ADFE14566F6696BE1CA0FCFE04DF4B84DEE6DD83FC71AB619E8D9287A7DA6501012A7ED962D5E6DF0800902F81AB17C6DFF761C5B2B4F6A59058C93F4BE5F48FCCCF59EBF3009121961BE3BE80BB7BD4416427BCBD28B1432294B811A3619ECFABC036891912C16AFF38AA778E7287ACD96C098B226D1F4482555EA0116BDE6F3DF86AD623C6898AD2752C4E3023EEF36A6F05C8698007099B3D6FF6A9A9E15700FCFE7165DB5FBDD26E4641AD86276303186BA70E1402DBD025A8DFD5652867B1407D664F47A0D5C30B25B45B64E2C26D372C76CEDB004D81FE10ECD051B5DFFFBB76D0D9D5C2803B6672EABCF615C8797926679E127A99CB24327044CFD140586ED77957B4A6E62F61F0D70C83768C8775AC9955097EFAA93AD1D610DFC5593B00F19285305F15C8734DCBC0BD789F169A7B31F536E2A4083510B8C7419FC7E732754F6C380DA2C101F3DD8A75CFD0A96042619B5BEFEBDE88A2AC19671FBC521563CF255446428C0F7AD2D411C88E6DA8BF7DD560A504853AA966843C43B2731A1AA498D4278E83FA1F913A71154D575BD6309213B1F4617986BE1566255B369

The data in PKCS#8 wraps a PKCS#5 payload.

We see PBKDF2 with salt and number of iterations and KDF (PRF) function, and we see AES-256 in CBC mode with an initialization vector.

1
  • 1
    To be exact, most openssl (sub)commands including rsa can read both traditional and PKCS8 PEM files. rsa writes trad, while pkcs8 -topk8 writes P8 and since 1.0.0 pkcs8 (from) writes P8 PEM but trad DER; below 1.0.0 it writes both trad. pkey (only since 1.0.0) is the same as pkcs8 (from). – dave_thompson_085 May 4 '20 at 1:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.