Little knowledge of PKCS1 and PKCS8

Preface: This article is organized by the editor of Xiaochangsense.com (cha138.com), and mainly introduces the knowledge related to the small knowledge of PKCS1 and PKCS8. I hope it has certain reference value for you.

Reference Technology A

Recently, some friends have been confused about the difference between RSA’s private key PKCS1 and PKCS8. There will be various questions in the application. This article will talk about the difference between the two formats in a little detail.
PKCS1: Full name “Public-Key Cryptography Standards (PKCS) #1: RSA Cryptography Specifications” latest version 2.2 (rfc8017, interested students can read it), it can be seen from the name that it is A specification for the RSA algorithm. It contains all the content such as RSA encryption, decryption, signature verification, and of course the format of the private key. Version 1.1 of PKCS1 was released in 1991.
PKCS8: The full name is the latest version 1.2 of “Public-Key Cryptography Standards (PKCS) #8: Private-Key Information Syntax Specification”. It can be seen from the name that it is a dedicated storage for private keys. File format specification. Version 1.2 of PKCS1 was released in 2008.
It just so happens that the two of them overlap and both define the storage of the private key, so what is the relationship between them? Let’s actually verify it below.

At this time, there is a pkcs1.pem file in the current directory, and you can see the contents of the file

At this time, there will be an additional pkcs8.pem file in the current directory, and you can see the content of the file

So what’s the difference? We first convert the two PEM format files to DER format, this binary data is easy to compare.

Now we have got two files in DER format, we can use hexdemp to see the contents of the two files first:

At this time, if you look from the back to the front, you can actually find that PKCS8 only has one more 26-header than PKCS1, and the rest of the content is exactly the same.
We can verify it with the following command:

You can see that the output of the line 22:d=1 hl=4 l= 608 prim: OCTET STRING is the content of pkcs1.der. And the above content is that pkcs8 has more content than pkcs1. In fact, the most important thing is algorithm information, which is why pkcs8 can store private keys of various algorithms.

It can be seen from the chronological order that the PKCS standard is also constantly developing. The PKCS1 standard came out before PKCS8. At that time, even the asymmetric algorithm was only mature with RSA. The algorithm is not difficult to understand as a standard output. With the development of encryption algorithms, PKCS8 came out to do such a thing as storing keys.

What is the difference between PEM and DER? In fact, PEM is to base64 encode the content of DER and format the output.
The storage in DER format uses a data storage format called asn1 to store each data item. We can look at the output of pkcs1.der:

All the data of the RSA private key is output.

Introduction

Recently, some friends have been confused about the difference between RSA’s private key PKCS1 and PKCS8. There will be various questions in the application. This article will talk about the difference between the two formats in a little detail.
PKCS1: Full name “Public-Key Cryptography Standards (PKCS) #1: RSA Cryptography Specifications” latest version 2.2 (rfc8017, interested students can read it), from It can be seen from the name that it is a specification for the RSA algorithm. It contains all the content such as RSA encryption, decryption, signature verification, and of course the format of the private key. Version 1.1 of PKCS1 was released in 1991.
PKCS8: Full name “Public-Key Cryptography Standards (PKCS) #8: Private-Key Information Syntax Specification” latest version 1.2, it can be seen from the name that it is specially used to store private keys File format specification. Version 1.2 of PKCS1 was released in 2008.
It just so happens that the two of them overlap and both define the storage of the private key, so what is the relationship between them? Let’s actually verify it below.

Authentication

Generate RSA private key

bash# openssl genrsa -out pkcs1.pem 1024

At this time, there is a pkcs1.pem file in the current directory, and you can see the contents of the file

-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCss7fSp + qZaACgW + rFStHODx8Z7Hr1bFMxnqde61SmcF6dbuc/
C1lgJY/FyrTPxlu4wGNpThVn1lPfvQKCNn492kd8kl0863fhU3rk + 7/18R + uQCrp
sz9RGz1zSSmRH2m3igwzNafjiu9vAJ7emZ5QmTGIGqAao62w7waXx8gfIwIDAQAB
AoGAKJj + x2htv64xJ4E0CTjzZQss9n0e0n6qd + mh0rBjb9TOiRe/CJT0Z6OszoQ +
SPGKMj4XoHOYmJZLlTbrVgzVU/XgMFuz92IDPhaSafHpk143BVu6fc0Hvt + KIfUq
tj0hSHInW1tICQRA3OJoUDWTNTNbD + iNQx + 6T53AGY3nZpECQQDggckjDYKB9SDB
8tG4oiC3H/rHcW9Iy9cVKFIqKy + 1kHiFJjsGaY8JWpNr4e0xSaVyC9oi7ICJJkWw
e94RCdE1AkEAxO2T3U9YNOQoqOt4DgzURClzJtbD1lXmcb5MPKkLxcgBPzGVor21
19vCgGmMk/n162vys5YHfixeg8XLINfR9wJAP6itdtIC2oS + SCBpFThrnGFmA52H
l5WtJCOtwKVEPvB7LA4eGIBf/aAAQ0vprNscHY18ygBTphADHLrSyfQFrQJAduth
F4wKoCYHx7oqTZHBvUeqmhGnQY + uEZHxR9Bto4ob9qViz1vwq + GxmwoiMloH/5q4
fWpkK/VvPfP7rmGmEQJBAIdhwWNOhIml + 6bS3MxLwFklwWALBzZMvOKWfKTQ9vA6
X27ZNTBt0ij25MI26sD7dtuFq277Ns99 + Qdwr5FVjng=
-----END RSA PRIVATE KEY-----

Convert the private key to PKCS8

bash# openssl pkcs8 -topk8 -inform PEM -in pkcs1.pem -outform PEM -nocrypt -out pkcs8.pem

At this time, there will be an additional pkcs8.pem file in the current directory, and you can see the content of the file

-----BEGIN PRIVATE KEY-----
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKyzt9Kn6ploAKBb
6sVK0c4PHxnsevVsUzGep17rVKZwXp1u5z8LWWAlj8XKtM/GW7jAY2lOFWfWU9 + 9
AoI2fj3aR3ySXTzrd + FTeuT7v/XxH65AKumzP1EbPXNJKZEfabeKDDM1p + OK728A
nt6ZnlCZMYgaoBqjrbDvBpfHyB8jAgMBAAECgYAomP7HaG2/rjEngTQJOPNlCyz2
fR7Sfqp36aHSsGNv1M6JF78IlPRno6zOhD5I8YoyPhegc5iYlkuVNutWDNVT9eAw
W7P3YgM + FpJp8emTXjcFW7p9zQe + 34oh9Sq2PSFIcidbW0gJBEDc4mhQNZM1M1sP
6I1DH7pPncAZjedmkQJBAOCBySMNgoH1IMHy0biiILcf + sdxb0jL1xUoUiorL7WQ
eIUmOwZpjwlak2vh7TFJpXIL2iLsgIkmRbB73hEJ0TUCQQDE7ZPdT1g05Cio63gO
DNREKXMm1sPWVeZxvkw8qQvFyAE/MZWivbXX28KAaYyT + fXra/Kzlgd + LF6Dxcsg
19H3AkA/qK120gLahL5IIGkVOGucYWYDnYeXla0kI63ApUQ + 8HssDh4YgF/9oABD
S+ms2xwdjXzKAFOmEAMcutLJ9AWtAkB262EXjAqgJgfHuipNkcG9R6qaEadBj64R
kfFH0G2jihv2pWLPW/Cr4bGbCiIyWgf/mrh9amQr9W898/uuYaYRAkEAh2HBY06E
iaX7ptLczEvAWSXBYAsHNky84pZ8pND28Dpfbtk1MG3SKPbkwjbqwPt224Wrbvs2
z335B3CvkVWOeA==
-----END PRIVATE KEY-----

Difference

So what’s the difference? We first convert the two PEM format files to DER format, this binary data is easy to compare.

bash# openssl rsa -in pkcs1.pem -out pkcs1.der -outform DER
bash# openssl pkcs8 -topk8 -inform PEM -in pkcs1.pem -outform DER -nocrypt -out pkcs8.der
bash # ls
pkcs1.der pkcs1.pem pkcs8.der pkcs8.pem

Now we have got two files in DER format, we can use hexdemp to see the contents of the two files first:

bash# cat pkcs1.der | hexdump
0000000 30 82 02 5c 02 01 00 02 81 81 00 ac b3 b7 d2 a7
0000010 ea 99 68 00 a0 5b ea c5 4a d1 ce 0f 1f 19 ec 7a
0000020 f5 6c 53 31 9e a7 5e eb 54 a6 70 5e 9d 6e e7 3f
0000030 0b 59 60 25 8f c5 ca b4 cf c6 5b b8 c0 63 69 4e
0000040 15 67 d6 53 df bd 02 82 36 7e 3d da 47 7c 92 5d
0000050 3c eb 77 e1 53 7a e4 fb bf f5 f1 1f ae 40 2a e9
0000060 b3 3f 51 1b 3d 73 49 29 91 1f 69 b7 8a 0c 33 35
0000070 a7 e3 8a ef 6f 00 9e de 99 9e 50 99 31 88 1a a0
0000080 1a a3 ad b0 ef 06 97 c7 c8 1f 23 02 03 01 00 01
0000090 02 81 80 28 98 fe c7 68 6d bf ae 31 27 81 34 09
00000a0 38 f3 65 0b 2c f6 7d 1e d2 7e aa 77 e9 a1 d2 b0
00000b0 63 6f d4 ce 89 17 bf 08 94 f4 67 a3 ac ce 84 3e
00000c0 48 f1 8a 32 3e 17 a0 73 98 98 96 4b 95 36 eb 56
00000d0 0c d5 53 f5 e0 30 5b b3 f7 62 03 3e 16 92 69 f1
00000e0 e9 93 5e 37 05 5b ba 7d cd 07 be df 8a 21 f5 2a
00000f0 b6 3d 21 48 72 27 5b 5b 48 09 04 40 dc e2 68 50
0000100 35 93 35 33 5b 0f e8 8d 43 1f ba 4f 9d c0 19 8d
0000110 e7 66 91 02 41 00 e0 81 c9 23 0d 82 81 f5 20 c1
0000120 f2 d1 b8 a2 20 b7 1f fa c7 71 6f 48 cb d7 15 28
0000130 52 2a 2b 2f b5 90 78 85 26 3b 06 69 8f 09 5a 93
0000140 6b e1 ed 31 49 a5 72 0b da 22 ec 80 89 26 45 b0
0000150 7b de 11 09 d1 35 02 41 00 c4 ed 93 dd 4f 58 34
0000160 e4 28 a8 eb 78 0e 0c d4 44 29 73 26 d6 c3 d6 55
0000170 e6 71 be 4c 3c a9 0b c5 c8 01 3f 31 95 a2 bd b5
0000180 d7 db c2 80 69 8c 93 f9 f5 eb 6b f2 b3 96 07 7e
0000190 2c 5e 83 c5 cb 20 d7 d1 f7 02 40 3f a8 ad 76 d2
00001a0 02 da 84 be 48 20 69 15 38 6b 9c 61 66 03 9d 87
00001b0 97 95 ad 24 23 ad c0 a5 44 3e f0 7b 2c 0e 1e 18
00001c0 80 5f fd a0 00 43 4b e9 ac db 1c 1d 8d 7c ca 00
00001d0 53 a6 10 03 1c ba d2 c9 f4 05 ad 02 40 76 eb 61
00001e0 17 8c 0a a0 26 07 c7 ba 2a 4d 91 c1 bd 47 aa 9a
00001f0 11 a7 41 8f ae 11 91 f1 47 d0 6d a3 8a 1b f6 a5
0000200 62 cf 5b f0 ab e1 b1 9b 0a 22 32 5a 07 ff 9a b8
0000210 7d 6a 64 2b f5 6f 3d f3 fb ae 61 a6 11 02 41 00
0000220 87 61 c1 63 4e 84 89 a5 fb a6 d2 dc cc 4b c0 59
0000230 25 c1 60 0b 07 36 4c bc e2 96 7c a4 d0 f6 f0 3a
0000240 5f 6e d9 35 30 6d d2 28 f6 e4 c2 36 ea c0 fb 76
0000250 db 85 ab 6e fb 36 cf 7d f9 07 70 af 91 55 8e 78
0000260
bash# cat pkcs8.der | hexdump
0000000 30 82 02 76 02 01 00 30 0d 06 09 2a 86 48 86 f7
0000010 0d 01 01 01 05 00 04 82 02 60 30 82 02 5c 02 01
0000020 00 02 81 81 00 ac b3 b7 d2 a7 ea 99 68 00 a0 5b
0000030 ea c5 4a d1 ce 0f 1f 19 ec 7a f5 6c 53 31 9e a7
0000040 5e eb 54 a6 70 5e 9d 6e e7 3f 0b 59 60 25 8f c5
0000050 ca b4 cf c6 5b b8 c0 63 69 4e 15 67 d6 53 df bd
0000060 02 82 36 7e 3d da 47 7c 92 5d 3c eb 77 e1 53 7a
0000070 e4 fb bf f5 f1 1f ae 40 2a e9 b3 3f 51 1b 3d 73
0000080 49 29 91 1f 69 b7 8a 0c 33 35 a7 e3 8a ef 6f 00
0000090 9e de 99 9e 50 99 31 88 1a a0 1a a3 ad b0 ef 06
00000a0 97 c7 c8 1f 23 02 03 01 00 01 02 81 80 28 98 fe
00000b0 c7 68 6d bf ae 31 27 81 34 09 38 f3 65 0b 2c f6
00000c0 7d 1e d2 7e aa 77 e9 a1 d2 b0 63 6f d4 ce 89 17
00000d0 bf 08 94 f4 67 a3 ac ce 84 3e 48 f1 8a 32 3e 17
00000e0 a0 73 98 98 96 4b 95 36 eb 56 0c d5 53 f5 e0 30
00000f0 5b b3 f7 62 03 3e 16 92 69 f1 e9 93 5e 37 05 5b
0000100 ba 7d cd 07 be df 8a 21 f5 2a b6 3d 21 48 72 27
0000110 5b 5b 48 09 04 40 dc e2 68 50 35 93 35 33 5b 0f
0000120 e8 8d 43 1f ba 4f 9d c0 19 8d e7 66 91 02 41 00
0000130 e0 81 c9 23 0d 82 81 f5 20 c1 f2 d1 b8 a2 20 b7
0000140 1f fa c7 71 6f 48 cb d7 15 28 52 2a 2b 2f b5 90
0000150 78 85 26 3b 06 69 8f 09 5a 93 6b e1 ed 31 49 a5
0000160 72 0b da 22 ec 80 89 26 45 b0 7b de 11 09 d1 35
0000170 02 41 00 c4 ed 93 dd 4f 58 34 e4 28 a8 eb 78 0e
0000180 0c d4 44 29 73 26 d6 c3 d6 55 e6 71 be 4c 3c a9
0000190 0b c5 c8 01 3f 31 95 a2 bd b5 d7 db c2 80 69 8c
00001a0 93 f9 f5 eb 6b f2 b3 96 07 7e 2c 5e 83 c5 cb 20
00001b0 d7 d1 f7 02 40 3f a8 ad 76 d2 02 da 84 be 48 20
00001c0 69 15 38 6b 9c 61 66 03 9d 87 97 95 ad 24 23 ad
00001d0 c0 a5 44 3e f0 7b 2c 0e 1e 18 80 5f fd a0 00 43
00001e0 4b e9 ac db 1c 1d 8d 7c ca 00 53 a6 10 03 1c ba
00001f0 d2 c9 f4 05 ad 02 40 76 eb 61 17 8c 0a a0 26 07
0000200 c7 ba 2a 4d 91 c1 bd 47 aa 9a 11 a7 41 8f ae 11
0000210 91 f1 47 d0 6d a3 8a 1b f6 a5 62 cf 5b f0 ab e1
0000220 b1 9b 0a 22 32 5a 07 ff 9a b8 7d 6a 64 2b f5 6f
0000230 3d f3 fb ae 61 a6 11 02 41 00 87 61 c1 63 4e 84
0000240 89 a5 fb a6 d2 dc cc 4b c0 59 25 c1 60 0b 07 36
0000250 4c bc e2 96 7c a4 d0 f6 f0 3a 5f 6e d9 35 30 6d
0000260 d2 28 f6 e4 c2 36 ea c0 fb 76 db 85 ab 6e fb 36
0000270 cf 7d f9 07 70 af 91 55 8e 78
000027a

At this time, if you look from the back to the front, you can actually find that PKCS8 only has one more 26-header than PKCS1, and the rest of the content is exactly the same.
We can verify it with the following command:

bash# openssl asn1parse -i -in pkcs8.der -inform DER
    0:d=0 hl=4 l= 630 cons: SEQUENCE
    4: d=1 hl=2 l= 1 prim: INTEGER :00
    7: d=1 hl=2 l= 13 cons: SEQUENCE
    9: d=2 hl=2 l= 9 prim: OBJECT :rsaEncryption
   20: d=2 hl=2 l= 0 prim: NULL
   22:d=1 hl=4 l= 608 prim: OCTET STRING [HEX DUMP]:3082025C02010002818100ACB3B7D2A7EA996800A05BEAC54AD1CE0F1F19EC7AF56C53319EA75EEB54A6705E9D6EE73F0B 5960258FC5CAB4CFC65BB8C063694E1567D653DFBD0282367E3DDA477C925D3CEB77E1537AE4FBBFF5F11FAE402AE9B33F511B3D734929911F69B78A0C3335A7E38AEF 6F009EDE999E509931881AA01AA3ADB0EF0697C7C81F2302030100010281802898FEC7686DBFAE312781340938F3650B2CF67D1ED27EAA77E9A1D2B0636FD4CE8917BF0 894F467A3ACCE843E48F18A323E17A0739898964B9536EB560CD553F5E0305BB3F762033E169269F1E9935E37055BBA7DCD07BEDF8A21F52AB63D214872275B5B480 90440DCE26850359335335B0FE88D431FBA4F9DC0198DE76691024100E081C9230D8281F520C1F2D1B8A220B71FFAC7716F48CBD71528522A2B2FB5907885263B06 698F095A936BE1ED3149A5720BDA22EC80892645B07BDE1109D135024100C4ED93DD4F5834E428A8EB780E0CD444297326D6C3D655E671BE4C3CA90BC5C8013F3195A2B DB5D7DBC280698C93F9F5EB6BF2B396077E2C5E83C5CB20D7D1F702403FA8AD76D202DA84BE48206915386B9C6166039D879795AD2423ADC0A5443EF07B2C0E1E18805 FFDA000434BE9ACDB1C1D8D7CCA0053A610031CBAD2C9F405AD024076EB61178C0AA02607C7BA2A4D91C1BD47AA9A11A7418FAE1191F147D06DA38A1BF6A562CF5BF0ABE1B19 B0A22325A07FF9AB87D6A642BF56F3DF3FBAE61A6110241008761C1634E8489A5FBA6D2DCCC4BC05925C1600B07364CBCE2967CA4D0F6F03A5F6ED935306DD228F6E4C 236EAC0FB76DB85AB6EFB36CF7DF90770AF91558E78

You can see that the output of the line 22:d=1 hl=4 l= 608 prim: OCTET STRING is the content of pkcs1.der. And the above content is that pkcs8 has more content than pkcs1. In fact, the most important thing is algorithm information, which is why pkcs8 can store private keys of various algorithms.

Summary

It can be seen from the chronological order that the PKCS standard is also constantly developing. The PKCS1 standard came out before PKCS8. At that time, even the asymmetric algorithm was only mature with RSA. The algorithm is not difficult to understand as a standard output. With the development of encryption algorithms, PKCS8 came out to do such a thing as storing keys.

Tips: about PEM and DER formats

What is the difference between PEM and DER? In fact, PEM is to base64 encode the content of DER and format the output.
The storage in DER format uses a data storage format called asn1 to store each data item. We can look at the output of pkcs1.der:

bash# openssl asn1parse -i -in pkcs1.der -inform DER
    0:d=0 hl=4 l= 604 cons: SEQUENCE
    4: d=1 hl=2 l= 1 prim: INTEGER :00
    7:d=1 hl=3 l= 129 prim: INTEGER :ACB3B7D2A7EA996800A05BEAC54AD1CE0F1F19EC7AF56C53319EA75EEB54A6705E9D6EE73F0B5960258FC5CAB4CFC65BB8C063694E1567D 653DFBD0282367E3DDA477C925D3CEB77E1537AE4FBBFF5F11FAE402AE9B33F511B3D734929911F69B78A0C3335A7E38AEF6F009EDE999E509931881AA01AA3ADB0EF06 97C7C81F23
  139: d=1 hl=2 l= 3 prim: INTEGER :010001
  144:d=1 hl=3 l= 128 prim: INTEGER :2898FEC7686DBFAE312781340938F3650B2CF67D1ED27EAA77E9A1D2B0636FD4CE8917BF0894F467A3ACCE843E48F18A323E17A 0739898964B9536EB560CD553F5E0305BB3F762033E169269F1E9935E37055BBA7DCD07BEDF8A21F52AB63D214872275B5B48090440DCE26850359335335B0FE88D4 31FBA4F9DC0198DE76691
  275:d=1 hl=2 l= 65 prim: INTEGER :E081C9230D8281F520C1F2D1B8A220B71FFAC7716F48CBD71528522A2B2FB5907885263B06698F095A936BE1ED3149A5720B DA22EC80892645B07BDE1109D135
  342:d=1 hl=2 l= 65 prim: INTEGER :C4ED93DD4F5834E428A8EB780E0CD444297326D6C3D655E671BE4C3CA90BC5C8013F3195A2BDB5D7DBC280698C93F9F5EB6BF2B39 6077E2C5E83C5CB20D7D1F7
  409:d=1 hl=2 l= 64 prim: INTEGER :3FA8AD76D202DA84BE48206915386B9C6166039D879795AD2423ADC0A5443EF07B2C0E1E18805FFDA000434BE9ACDB1C1D8D7CCA0053A 610031CBAD2C9F405AD
  475:d=1 hl=2 l= 64 prim: INTEGER :76EB61178C0AA02607C7BA2A4D91C1BD47AA9A11A7418FAE1191F147D06DA38A1BF6A562CF5BF0ABE1B19B0A22325A07FF9AB87D6A 642BF56F3DF3FBAE61A611
  541:d=1 hl=2 l= 65 prim: INTEGER :8761C1634E8489A5FBA6D2DCCC4BC05925C1600B07364CBCE2967CA4D0F6F03A5F6ED935306DD228F6E4C236EAC0FB76DB85AB6EFB3 6CF7DF90770AF91558E78

All the data of the RSA private key is output.

This article only compares the unencrypted pkcs1 and pkcs8 private key files, and does not compare the encrypted file format.

https://press.one/file/v?s=88e1e88832b525da091cfd1679e85312e0709cd87401ae7f11495bcd68138980105a69c480bc0e0309cb97fbee7736ff5ebd7d6d6ccb8b302b c8a1766e166b9f1 &h=efa44ee9077828eda4000e7dc1050a678414617cf0bd8598f87bfd2470b1418b &a=28b6cefc3c00c7e355b435d4b5e7f5888056 4fa6 &f=P1 &v=3

Author: Wang Ju
Link: https://www.jianshu.com/p/a428e183e72e
Source: Jianshu
Copyright belongs to the author. For commercial reprint, please contact the author for authorization, for non-commercial reprint, please indicate the source.