目录
起因来自于支付宝
最近要和支付宝支付做个对接,然后发现自己对RSA一无所知。。。。补知识点吧。。。。
嗯,百度了一下Sha256withRSA,出现了一大堆文章,嗯,都很好,学习中。。。。。
然后,将支付宝提供的测试密钥导入到c#,出现问题了,一直失败,好吧,c#的证书格式和支付宝提供的不一样,支付宝使用的是openssl生成的密钥,再次百度c# rsa openssl,很好,又一大堆需要学习的内容,同时也明白了,openssl的密钥格式是asn.1格式的
参考目录
最后,因为文盲自己是需要.net环境的,所以重点关注了一下c#相关的内容,找了几篇文章,终于可以把PKCS8和PKCS1格式的asn密钥文件成功导入到c#中了
参考目录:
https://www.cnblogs.com/lidj/p/5220816.html C#中使用OpenSSL的公钥加密/私钥解密
.NET导入openssl生成的公钥之BEGIN RSA PUBLIC KEY - Ady Lee - 博客园 .NET导入openssl生成的公钥之BEGIN RSA PUBLIC KEY
Cryptographic Interoperability: Keys - CodeProject Cryptographic Interoperability: Keys
然后,终于明白了PKCS1和PKCS8的密钥格式。。。。其实说起来也非常简单。。。。
pkcs1 秘钥格式解析
先说说PKCS1的私钥密钥格式
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAmLrQGvXhsKkR0mY+mJVOy5vJcZ72xMfGVE3Iytoi+R55Jstf
j6Y7r0rr5FRk3qjGss+uV25mxRMRxuNmqcP6y4d89F9NXKkiXHHj56H9vtTCuiWg
zB2VmpJAc7/V26HK59kt8wldO5zmM81Y2yJArN00edj5m3+T8zgCsYi6CL1oRPYD
Ura9kok7kizXBRl2PUTxJmjHpS7FlIdveYobGceqcD80R86jjuBqh8ZK2FhX44DY
Mkam80bC7gx35lq8y5SnwvfyCb1HCanGmVzxN7XJn0s5TeixmA487LpKwoX7ODjO
DuxHQh5A4HP4wrzcZ4IT1zGBn0hvixBbVzwMGQIDAQABAoIBADx5jDmArtLivznO
Xm7QgBjDIOqqhQ+6AuASxLT91udJl7MyyC37JprsUadhe2dm8PaQoXnhLzRNrrZQ
CLCj90Rr66BftCRCnGZ6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TvCeyoQAQKY6uMq7U8s4+qHLJTn7JnNxS6ulFJh5ump0HmF5UR1M
-----END RSA PRIVATE KEY-----
以上是支付宝生成的一个私钥证书,掐头去尾之后,解码一下base64得到数组
48,130,4,163,2,1,0,2,130,1,1,0,152,186,208,26,245,225,176,169,17,210,102,62,152,149,78,203,155,201,113,158,246,196,199,198,84,77,200,202,218,34,249,30,121,38,203,95,143,166,59,175,74,235,228,84,100,222,168,198,178,207,174,87,110,102,197,19,17,198,227,102,169,195,250,203,135,124,244,95,77,92,169,34,92,113,227,231,161,253,190,212,194,186,37,160,204,29,149,154,146,64,115,191,213,219,161,202,231,217,45,243,9,93,59,156,230,51,205,88,219,34,64,172,221,52,121,216,249,155,127,147,243,56,2,177,136,186,8,189,104,68,246,3,82,182,189,146,137,59,146,44,215,5,25,118,61,68,241,38,104,199,165,46,197,148,135,111,121,138,27,25,199,170,112,63,52,71,206,163,142,224,106,135,198,74,216,88,87,227,128,216,50,70,166,243,70,194,238,12,119,230,90,188,203,148,167,194,247,242,9,189,71,9,169,198,153,92,241,55,181,201,159,75,57,77,232,177,152,14,60,236,186,74,194,133,251,56,56,206,14,236,71,66,30,64,224,115,248,194,188,220,103,130,19,215,49,129,159,72,111,139,16,91,87,60,12,25,2,3,1,0,1,2,130,1,0,60,121,140,57,128,174,210,226,191,57,206,94,110,208,128,24,195,32,234,170,133,15,186,2,224,18,196,180,253,214,231,73,151,179,50,200,45,251,38,154,236,81,167,97,123,103,102,240,246,144,161,121,225,47,52,77,174,182,80,8,176,163,247,68,107,235,160,95,180,36,66,156,102,122,122,65,151,180,255,193,3,49,238,0,83,146,75,80,159,36,130,48,173,159,127,218,246,30,218,192,231,46,81,146,120,220,126,44,246,232,3,47,239,142,253,200,90,93,166,16,145,55,214,105,49,184,104,8,235,3,235,143,25,82,251,167,46,214,220,41,3,252,159,225,49,119,194,239,235,162,190,151,6,162,160,139,98,29,199,37,58,3,20,45,98,89,161,80,91,44,103,56,143,60,191,86,112,17,176,171,99,98,80,151,228,136,23,151,138,124,178,175,87,232,161,100,143,117,213,228,7,217,238,82,168,105,146,187,209,58,17,87,4,130,134,110,237,72,244,103,121,168,224,129,162,118,0,111,129,92,97,231,235,7,14,155,160,3,192,122,254,240,187,207,132,169,165,221,245,12,199,155,147,135,70,1,2,129,129,0,202,115,72,100,151,178,62,38,241,127,82,55,151,72,183,17,16,172,221,127,226,74,243,192,146,33,50,103,157,180,247,90,2,251,189,145,87,166,117,136,127,161,59,107,5,89,199,162,170,105,161,233,192,76,197,182,152,110,105,90,235,159,64,203,46,47,115,133,233,163,60,122,26,20,172,220,137,112,152,68,73,115,57,175,98,174,123,86,114,124,240,148,167,142,54,79,83,98,232,198,17,202,199,221,40,50,71,136,129,233,0,52,67,246,243,242,119,209,95,49,167,226,179,144,180,52,195,185,2,129,129,0,193,32,192,90,141,198,121,213,193,159,75,178,223,253,15,186,45,181,145,202,3,176,78,33,97,7,227,169,27,135,180,168,106,126,15,159,176,184,111,143,17,145,71,24,69,56,88,81,156,149,240,143,68,22,34,63,4,141,46,177,104,184,178,81,243,167,255,114,197,223,201,61,30,247,247,42,192,28,177,235,171,106,142,62,10,208,84,67,186,206,240,71,72,56,196,137,207,30,169,45,102,60,41,18,82,88,115,134,51,213,64,215,147,121,208,81,161,180,104,128,28,85,39,76,160,212,123,97,2,129,128,26,112,137,224,209,74,239,1,9,3,22,202,185,208,62,80,85,60,46,69,88,89,111,146,19,195,144,32,166,33,15,56,203,48,243,101,250,235,123,104,19,59,12,86,130,193,97,232,37,151,92,20,250,15,76,182,247,27,92,89,10,114,166,244,162,24,130,94,215,207,128,195,117,138,131,180,221,162,61,208,149,67,30,34,128,161,198,235,216,81,230,221,147,48,104,121,93,171,138,56,235,243,238,188,60,86,172,174,240,64,108,205,102,158,230,231,32,172,208,96,220,203,39,166,91,82,148,201,2,129,129,0,157,183,49,94,194,9,35,213,252,153,42,40,153,226,255,196,251,47,28,47,237,115,61,90,228,242,156,104,173,162,213,85,244,33,158,128,186,192,227,217,226,110,209,176,112,86,227,73,66,166,240,29,15,109,67,234,82,189,57,52,136,96,204,191,64,76,33,192,136,247,136,79,241,202,110,126,235,4,132,8,1,164,179,36,215,143,234,64,165,125,249,155,60,207,159,49,43,131,206,132,212,200,172,129,217,221,129,250,70,186,16,137,252,85,174,89,251,196,104,210,182,80,148,23,112,0,22,1,2,129,128,79,166,108,232,100,185,96,200,168,202,104,109,0,32,104,240,44,55,97,98,30,27,206,230,139,35,23,223,17,237,151,152,142,78,181,5,247,92,1,26,213,64,206,142,78,244,207,98,244,35,105,249,14,184,210,169,39,46,248,98,130,70,114,130,26,22,132,184,178,1,248,20,140,45,237,112,152,195,252,148,107,48,129,82,249,253,171,24,85,78,240,158,202,132,0,64,166,58,184,202,187,83,203,56,250,161,203,37,57,251,38,115,113,75,171,165,20,152,121,186,106,116,30,97,121,81,29,76
然后,就开始准备把他弄成c#所对应的RSAParameters对象
来看看各个字节所描述的意义吧
48 RSA 标示第一字节
130 长度占用字节描述方式,长度计算方式,如果小于128,自身表示长度值,否则与127进行与运算,得到的结果为长度,但与运算后的长度不得大于3,例如130表示长度占2位
4,163 长度值,计算方式为第一字节为最高位,顺序后排,例如这个值的结果就是0x04a3,即4*256+163,得到1187,此处的长度值为该字节之后的所有字节数,且需要验证通过
2 RSA 私钥证书固定字节
1 长度
0 RSA 描述头结束,该字节必须为0
2 PKCS1 长度开始标示
130 长度占用字节描述方式
1,1 长度值
0 此字节开始,连续的0值字节为填充内容,需要忽视,且忽视掉的字节数需要被长度减掉
152,186,208,26,245,225,176,169,17,210,102,62,152,149,78,203,155,201,113,158,246,196,199,198,84,77,200,202,218,34,249,30,121,38,203,95,143,166,59,175,74,235,228,84,100,222,168,198,178,207,174,87,110,102,197,19,17,198,227,102,169,195,250,203,135,124,244,95,77,92,169,34,92,113,227,231,161,253,190,212,194,186,37,160,204,29,149,154,146,64,115,191,213,219,161,202,231,217,45,243,9,93,59,156,230,51,205,88,219,34,64,172,221,52,121,216,249,155,127,147,243,56,2,177,136,186,8,189,104,68,246,3,82,182,189,146,137,59,146,44,215,5,25,118,61,68,241,38,104,199,165,46,197,148,135,111,121,138,27,25,199,170,112,63,52,71,206,163,142,224,106,135,198,74,216,88,87,227,128,216,50,70,166,243,70,194,238,12,119,230,90,188,203,148,167,194,247,242,9,189,71,9,169,198,153,92,241,55,181,201,159,75,57,77,232,177,152,14,60,236,186,74,194,133,251,56,56,206,14,236,71,66,30,64,224,115,248,194,188,220,103,130,19,215,49,129,159,72,111,139,16,91,87,60,12,25 RSAParameters.Modulus 数据
2 PKCS1 长度开始标示
3 长度占用字节描述方式
1,0,1 RSAParameters.Exponent 数据
2 PKCS1 长度开始标示
130 长度占用字节描述方式
1,0 长度值
60,121,140,57,128,174,210,226,191,57,206,94,110,208,128,24,195,32,234,170,133,15,186,2,224,18,196,180,253,214,231,73,151,179,50,200,45,251,38,154,236,81,167,97,123,103,102,240,246,144,161,121,225,47,52,77,174,182,80,8,176,163,247,68,107,235,160,95,180,36,66,156,102,122,122,65,151,180,255,193,3,49,238,0,83,146,75,80,159,36,130,48,173,159,127,218,246,30,218,192,231,46,81,146,120,220,126,44,246,232,3,47,239,142,253,200,90,93,166,16,145,55,214,105,49,184,104,8,235,3,235,143,25,82,251,167,46,214,220,41,3,252,159,225,49,119,194,239,235,162,190,151,6,162,160,139,98,29,199,37,58,3,20,45,98,89,161,80,91,44,103,56,143,60,191,86,112,17,176,171,99,98,80,151,228,136,23,151,138,124,178,175,87,232,161,100,143,117,213,228,7,217,238,82,168,105,146,187,209,58,17,87,4,130,134,110,237,72,244,103,121,168,224,129,162,118,0,111,129,92,97,231,235,7,14,155,160,3,192,122,254,240,187,207,132,169,165,221,245,12,199,155,147,135,70,1 RSAParameters.D 数据
2 PKCS1 长度开始标示
129 长度占用字节描述方式
129 长度值
0 填充的空数据
202,115,72,100,151,178,62,38,241,127,82,55,151,72,183,17,16,172,221,127,226,74,243,192,146,33,50,103,157,180,247,90,2,251,189,145,87,166,117,136,127,161,59,107,5,89,199,162,170,105,161,233,192,76,197,182,152,110,105,90,235,159,64,203,46,47,115,133,233,163,60,122,26,20,172,220,137,112,152,68,73,115,57,175,98,174,123,86,114,124,240,148,167,142,54,79,83,98,232,198,17,202,199,221,40,50,71,136,129,233,0,52,67,246,243,242,119,209,95,49,167,226,179,144,180,52,195,185 RSAParameters.P 数据
2 PKCS1 长度开始标示
129 长度占用字节描述方式
129 长度值
0 填充的空数据
193,32,192,90,141,198,121,213,193,159,75,178,223,253,15,186,45,181,145,202,3,176,78,33,97,7,227,169,27,135,180,168,106,126,15,159,176,184,111,143,17,145,71,24,69,56,88,81,156,149,240,143,68,22,34,63,4,141,46,177,104,184,178,81,243,167,255,114,197,223,201,61,30,247,247,42,192,28,177,235,171,106,142,62,10,208,84,67,186,206,240,71,72,56,196,137,207,30,169,45,102,60,41,18,82,88,115,134,51,213,64,215,147,121,208,81,161,180,104,128,28,85,39,76,160,212,123,97 RSAParameters.Q 数据
2 PKCS1 长度开始标示
129 长度占用字节描述方式
128 长度值
26,112,137,224,209,74,239,1,9,3,22,202,185,208,62,80,85,60,46,69,88,89,111,146,19,195,144,32,166,33,15,56,203,48,243,101,250,235,123,104,19,59,12,86,130,193,97,232,37,151,92,20,250,15,76,182,247,27,92,89,10,114,166,244,162,24,130,94,215,207,128,195,117,138,131,180,221,162,61,208,149,67,30,34,128,161,198,235,216,81,230,221,147,48,104,121,93,171,138,56,235,243,238,188,60,86,172,174,240,64,108,205,102,158,230,231,32,172,208,96,220,203,39,166,91,82,148,201 RSAParameters.DP 数据
2 PKCS1 长度开始标示
129 长度占用字节描述方式
129 长度值
0 填充的空数据
157,183,49,94,194,9,35,213,252,153,42,40,153,226,255,196,251,47,28,47,237,115,61,90,228,242,156,104,173,162,213,85,244,33,158,128,186,192,227,217,226,110,209,176,112,86,227,73,66,166,240,29,15,109,67,234,82,189,57,52,136,96,204,191,64,76,33,192,136,247,136,79,241,202,110,126,235,4,132,8,1,164,179,36,215,143,234,64,165,125,249,155,60,207,159,49,43,131,206,132,212,200,172,129,217,221,129,250,70,186,16,137,252,85,174,89,251,196,104,210,182,80,148,23,112,0,22,1 RSAParameters.DQ 数据
2 PKCS1 长度开始标示
129 长度占用字节描述方式
128 长度值
79,166,108,232,100,185,96,200,168,202,104,109,0,32,104,240,44,55,97,98,30,27,206,230,139,35,23,223,17,237,151,152,142,78,181,5,247,92,1,26,213,64,206,142,78,244,207,98,244,35,105,249,14,184,210,169,39,46,248,98,130,70,114,130,26,22,132,184,178,1,248,20,140,45,237,112,152,195,252,148,107,48,129,82,249,253,171,24,85,78,240,158,202,132,0,64,166,58,184,202,187,83,203,56,250,161,203,37,57,251,38,115,113,75,171,165,20,152,121,186,106,116,30,97,121,81,29,76 RSAParameters.InverseQ 数据
这么一看,openssl生成的PKCS1格式的内容,变成c#中的RSAParameters好简单啊
pkcs8 秘钥格式解析
嗯,再看看PKCS8的
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCYutAa9eGwqRHS
Zj6YlU7Lm8lxnvbEx8ZUTcjK2iL5Hnkmy1+PpjuvSuvkVGTeqMayz65XbmbFExHG
42apw/rLh3z0X01cqSJccePnof2+1MK6JaDMHZWakkBzv9Xbocrn2S3zCV07nOYz
zVjbIkCs3TR52Pmbf5PzOAKxiLoIvWhE9gNStr2SiTuSLNcFGXY9RPEmaMelLsWU
h295ihsZx6pwPzRHzqOO4GqHxkrYWFfjgNgyRqbzRsLuDHfmWrzLlKfC9/IJvUcJ
qcaZXPE3tcmfSzlN6LGYDjzsukrChfs4OM4O7EdCHkDgc/jCvNxnghPXMYGfSG+L
EFtXPAwZAgMBAAECggEAPHmMOYCu0uK/Oc5ebtCAGMMg6qqFD7oC4BLEtP3W50mX
szLILfsmmuxRp2F7Z2bw9pCheeEvNE2utlAIsKP3RGvroF+0JEKcZnp6QZe0/8ED
Me4AU5JLUJ8kgjCtn3/a9h7awOcuUZJ43H4s9ugDL++O/chaXa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RHUw=
-----END PRIVATE KEY-----
用同样的方式掐头去尾解个码,得到
48,130,4,189,2,1,0,48,13,6,9,42,134,72,134,247,13,1,1,1,5,0,4,130,4,167,48,130,4,163,2,1,0,2,130,1,1,0,152,186,208,26,245,225,176,169,17,210,102,62,152,149,78,203,155,201,113,158,246,196,199,198,84,77,200,202,218,34,249,30,121,38,203,95,143,166,59,175,74,235,228,84,100,222,168,198,178,207,174,87,110,102,197,19,17,198,227,102,169,195,250,203,135,124,244,95,77,92,169,34,92,113,227,231,161,253,190,212,194,186,37,160,204,29,149,154,146,64,115,191,213,219,161,202,231,217,45,243,9,93,59,156,230,51,205,88,219,34,64,172,221,52,121,216,249,155,127,147,243,56,2,177,136,186,8,189,104,68,246,3,82,182,189,146,137,59,146,44,215,5,25,118,61,68,241,38,104,199,165,46,197,148,135,111,121,138,27,25,199,170,112,63,52,71,206,163,142,224,106,135,198,74,216,88,87,227,128,216,50,70,166,243,70,194,238,12,119,230,90,188,203,148,167,194,247,242,9,189,71,9,169,198,153,92,241,55,181,201,159,75,57,77,232,177,152,14,60,236,186,74,194,133,251,56,56,206,14,236,71,66,30,64,224,115,248,194,188,220,103,130,19,215,49,129,159,72,111,139,16,91,87,60,12,25,2,3,1,0,1,2,130,1,0,60,121,140,57,128,174,210,226,191,57,206,94,110,208,128,24,195,32,234,170,133,15,186,2,224,18,196,180,253,214,231,73,151,179,50,200,45,251,38,154,236,81,167,97,123,103,102,240,246,144,161,121,225,47,52,77,174,182,80,8,176,163,247,68,107,235,160,95,180,36,66,156,102,122,122,65,151,180,255,193,3,49,238,0,83,146,75,80,159,36,130,48,173,159,127,218,246,30,218,192,231,46,81,146,120,220,126,44,246,232,3,47,239,142,253,200,90,93,166,16,145,55,214,105,49,184,104,8,235,3,235,143,25,82,251,167,46,214,220,41,3,252,159,225,49,119,194,239,235,162,190,151,6,162,160,139,98,29,199,37,58,3,20,45,98,89,161,80,91,44,103,56,143,60,191,86,112,17,176,171,99,98,80,151,228,136,23,151,138,124,178,175,87,232,161,100,143,117,213,228,7,217,238,82,168,105,146,187,209,58,17,87,4,130,134,110,237,72,244,103,121,168,224,129,162,118,0,111,129,92,97,231,235,7,14,155,160,3,192,122,254,240,187,207,132,169,165,221,245,12,199,155,147,135,70,1,2,129,129,0,202,115,72,100,151,178,62,38,241,127,82,55,151,72,183,17,16,172,221,127,226,74,243,192,146,33,50,103,157,180,247,90,2,251,189,145,87,166,117,136,127,161,59,107,5,89,199,162,170,105,161,233,192,76,197,182,152,110,105,90,235,159,64,203,46,47,115,133,233,163,60,122,26,20,172,220,137,112,152,68,73,115,57,175,98,174,123,86,114,124,240,148,167,142,54,79,83,98,232,198,17,202,199,221,40,50,71,136,129,233,0,52,67,246,243,242,119,209,95,49,167,226,179,144,180,52,195,185,2,129,129,0,193,32,192,90,141,198,121,213,193,159,75,178,223,253,15,186,45,181,145,202,3,176,78,33,97,7,227,169,27,135,180,168,106,126,15,159,176,184,111,143,17,145,71,24,69,56,88,81,156,149,240,143,68,22,34,63,4,141,46,177,104,184,178,81,243,167,255,114,197,223,201,61,30,247,247,42,192,28,177,235,171,106,142,62,10,208,84,67,186,206,240,71,72,56,196,137,207,30,169,45,102,60,41,18,82,88,115,134,51,213,64,215,147,121,208,81,161,180,104,128,28,85,39,76,160,212,123,97,2,129,128,26,112,137,224,209,74,239,1,9,3,22,202,185,208,62,80,85,60,46,69,88,89,111,146,19,195,144,32,166,33,15,56,203,48,243,101,250,235,123,104,19,59,12,86,130,193,97,232,37,151,92,20,250,15,76,182,247,27,92,89,10,114,166,244,162,24,130,94,215,207,128,195,117,138,131,180,221,162,61,208,149,67,30,34,128,161,198,235,216,81,230,221,147,48,104,121,93,171,138,56,235,243,238,188,60,86,172,174,240,64,108,205,102,158,230,231,32,172,208,96,220,203,39,166,91,82,148,201,2,129,129,0,157,183,49,94,194,9,35,213,252,153,42,40,153,226,255,196,251,47,28,47,237,115,61,90,228,242,156,104,173,162,213,85,244,33,158,128,186,192,227,217,226,110,209,176,112,86,227,73,66,166,240,29,15,109,67,234,82,189,57,52,136,96,204,191,64,76,33,192,136,247,136,79,241,202,110,126,235,4,132,8,1,164,179,36,215,143,234,64,165,125,249,155,60,207,159,49,43,131,206,132,212,200,172,129,217,221,129,250,70,186,16,137,252,85,174,89,251,196,104,210,182,80,148,23,112,0,22,1,2,129,128,79,166,108,232,100,185,96,200,168,202,104,109,0,32,104,240,44,55,97,98,30,27,206,230,139,35,23,223,17,237,151,152,142,78,181,5,247,92,1,26,213,64,206,142,78,244,207,98,244,35,105,249,14,184,210,169,39,46,248,98,130,70,114,130,26,22,132,184,178,1,248,20,140,45,237,112,152,195,252,148,107,48,129,82,249,253,171,24,85,78,240,158,202,132,0,64,166,58,184,202,187,83,203,56,250,161,203,37,57,251,38,115,113,75,171,165,20,152,121,186,106,116,30,97,121,81,29,76
再看看这个数据对应的意义如何?
48 RSA 标示第一字节
130 长度占用字节描述方式,长度计算方式,如果小于128,自身表示长度值,否则与127进行与运算,得到的结果为长度,但与运算后的长度不得大于3,例如130表示长度占2位
4,189 长度值,计算方式为第一字节为最高位,顺序后排,例如这个值的结果就是0x04bd,即4*256+189,得到1213,此处的长度值为该字节之后的所有字节数,且需要验证通过
2 RSA 私钥证书固定字节
1 长度
0 RSA 描述头结束,该字节必须为0
48 PKCS8 长度开始标示
13 长度占用字节描述方式
6 此处字节值必须为6,不知道为什么
9 长度值
42,134,72,134,247,13,1,1,1 此处9个字节的值为固定,需验证通过,貌似是个PKCS8的版本描述?Expected OID 1.2.840.113549.1.1.1
5,0 如果此字节是5,则必须跟一个0,否则为其他填充数据,跳过第一个字节,0所在的字节为长度占用字节描述方式,并按照规则,跳过指定的长度的内容
4 验证字节,此字节必须为4,也不知道为什么
130 长度占用字节描述方式
4,167 长度值,该值与字后的字节总数相等,应该也需要验证通过,根据Cryptographic Interoperability: Keys中的代码,他验证了这个值不能大于后边的字节总长度
48 PKCS8 长度开始标示
130 长度占用字节描述方式
4,163 长度值,该值与字后的字节总数相等,应该也需要验证通过,根据Cryptographic Interoperability: Keys中的代码,他验证了这个值不能小于后边的字节总长度
2 RSA 私钥证书固定字节
1 长度
0 RSA 描述头结束,该字节必须为0
2 PKCS1 长度开始标示
130 长度占用字节描述方式
1,1 长度值
0 填充数据
152,186,208,26,245,225,176,169,17,210,102,62,152,149,78,203,155,201,113,158,246,196,199,198,84,77,200,202,218,34,249,30,121,38,203,95,143,166,59,175,74,235,228,84,100,222,168,198,178,207,174,87,110,102,197,19,17,198,227,102,169,195,250,203,135,124,244,95,77,92,169,34,92,113,227,231,161,253,190,212,194,186,37,160,204,29,149,154,146,64,115,191,213,219,161,202,231,217,45,243,9,93,59,156,230,51,205,88,219,34,64,172,221,52,121,216,249,155,127,147,243,56,2,177,136,186,8,189,104,68,246,3,82,182,189,146,137,59,146,44,215,5,25,118,61,68,241,38,104,199,165,46,197,148,135,111,121,138,27,25,199,170,112,63,52,71,206,163,142,224,106,135,198,74,216,88,87,227,128,216,50,70,166,243,70,194,238,12,119,230,90,188,203,148,167,194,247,242,9,189,71,9,169,198,153,92,241,55,181,201,159,75,57,77,232,177,152,14,60,236,186,74,194,133,251,56,56,206,14,236,71,66,30,64,224,115,248,194,188,220,103,130,19,215,49,129,159,72,111,139,16,91,87,60,12,25 RSAParameters.Modulus 数据
2 PKCS1 长度开始标示
3 长度占用字节描述方式
1,0,1 RSAParameters.Exponent 数据
2 PKCS1 长度开始标示
130 长度占用字节描述方式
1,0 长度值
.......之后和PKCS1的格式就一毛一样了。。。
额。。。。知道这几个关键格式,那么自己就可以很方便的双向转换了。。。。
简单来说,PKCS1和PKCS8的主要分歧开始在第八个字节,PKCS1直接进入到长度、数据的阶段了,而PKCS8则开始了一个验证阶段,但是验证阶段结束后,后边的数据和PKCS1开始进入到长度、数据阶段的内容又一毛一样。。。。
-----------