Skip to content

RSA各种格式密钥在C#的使用,及加密、解密、签名、验签,此库包含公钥解密。

Notifications You must be signed in to change notification settings

chenming219/RSA_CSharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

密钥保存方式

PEM格式

PEM格式包含几种报文头 "-----BEGIN PUBLIC KEY-----": PKCS#8 格式公钥 "-----BEGIN PRIVATE KEY-----": PKCS#8 格式私钥

"-----BEGIN RSA PUBLIC KEY-----": PKCS#1 格式公钥 "-----BEGIN RSA PRIVATE KEY-----": PKCS#1 格式私钥

公钥

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDX2k9fV+TXr0sIw/oGI9v2g+Nm
Drf16cdfK45oPEyEGH0sqcYvvTTjD2iovHltOMCidgV2TH+S0bIb6JEoPaW7/+ya
tX3+MvHpCnOylaDH5aKNKoy/JyRn/cy9TXAk0QLAlKTCnfP1A3s5WqRDO2f6B70r
imffp9gfs/SVVhjy0wIDAQAB
-----END PUBLIC KEY-----

私钥

-----BEGIN PRIVATE KEY-----
MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBANfaT19X5NevSwjD
+gYj2/aD42YOt/Xpx18rjmg8TIQYfSypxi+9NOMPaKi8eW04wKJ2BXZMf5LRshvo
kSg9pbv/7Jq1ff4y8ekKc7KVoMfloo0qjL8nJGf9zL1NcCTRAsCUpMKd8/UDezla
pEM7Z/oHvSuKZ9+n2B+z9JVWGPLTAgMBAAECgYEAjn9nwNikkaL+lRXTvDP2JaLh
/2EeHvErlm+JvtR36wjDsjWOSbvN60wj2qk6PFmoCliBuJw96C4ZOAVrtrYj72cy
WvCg302y8Ugc8JjSuQMaDFck5jAD1rWt4IyZnHgogjryOAZ8KTl8kfAqCWIPS2BS
xIWZojhphn/vWkCYMWECQQDzLOgEF7mGUtx14B9ezJ2DS+iKV6k5lSWTGO4AqZ1g
T+lOLiNo5PuDMxSTMvuUQVULda4Su0EI4zqmlcr7C3axAkEA4zyF0YAdPWjNTy+s
ezQ5NpZuj5K9A2xAAu0Y1vp8bJGSt8oZdGewtL/QHSylJbG+xKiMRo5g9xwkkx0W
M0yqwwJBALVF5ncWkHA3WKxuNDRg55I2hdSK4FVSfz4WOWU5C13+c/aZoj/RYkQ9
UbshKDUuNo1iXi8Mc8CCUvhXdYaK1yECQHCxtVP/C4FHFnstE4dOkQlHdbFyfpbR
+rT4zRbgxxA46eCINDOMsRATCK1PVN/q1MdHJ7peJiynImRrSnVKkm0CQQCpmnAg
UnByBn4BJBgA26WRx/ag2S4gEU9TMnW+vMPFfscMvzMuOy34phlQqbHyniHi9Y5r
i7mELpNVp45VGi0l
-----END PRIVATE KEY-----

ASN格式

内容是PEM格式密钥中间的Base64段,密钥格式如下:

公钥

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDoUK4y6LX1kiHu0ubO1C1wRBrcgdSXBwI4CRuKK1fpTsS2dHpB5V+wQ63Rts3Ohr0E+pqIlaFu77Kp/UTczTH5dzSi4vdsexCBSnqC9RZLftadskTlf7OlHrF9Emva3s7Yla73EY56Jb0E/CS/PjKuNDNZ2CoZc2c3YBHDtObvhQIDAQAB

私钥

MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAOhQrjLotfWSIe7S5s7ULXBEGtyB1JcHAjgJG4orV+lOxLZ0ekHlX7BDrdG2zc6GvQT6moiVoW7vsqn9RNzNMfl3NKLi92x7EIFKeoL1Fkt+1p2yROV/s6UesX0Sa9reztiVrvcRjnolvQT8JL8+Mq40M1nYKhlzZzdgEcO05u+FAgMBAAECgYByqsAQ6MRHfeW51+eM1zXGYBdSF9My6Iaz2er5/ALcacC2vi9KL0lX21AEX1KeSesqdfeoyxJvi2iIdzZyNeYf52JUvvhnwedDu7YKi6xFgjvBbCPmifbau/r3JELQDHqRbnpYIe1NLrz9D0QBST5skGrYmeQe9NvVKEMMFuwPAQJBAPnTQI4AArADapYQjQnIE2Z94f1sAFkmvuI0kHikXFJKZ+CSWLw+vYrRkV1ct+Am2nz244e1AIcVG/iteYMs13UCQQDuDqKNFT43crjquJt3EzQWLxvQaK+bIKGVjcWje2xSH5oCiyPCk2RhWjd46aSPViPwyn5RPjsy0+EwLrm7xsXRAkAVbVYLF9sjJ5fbuR2vqhZn4wIgD0RUYStcEXAWuNomQLV6IHM16hetsV3LaSloM0zoDPDk8dRSwa+9e3uBkbYFAkAFOw0XTIAxd7PYdHX2B9X9JsvVqHczUWQ7hQHytaOLqmYX6DbA77wk53aQkZGsFJh1kf10i4mBdGZeSKiMF7IBAkAhniFbHF4G0RKxCJ3paII0m7qn3eI+pIPEGFX3sZjMYaYtWV9WmbvxytSdv88pa1Mh/ZxZjZTjsp0cq+XffD3r

XML格式

C# 默认的格式,密钥格式如下:

公钥

<RSAKeyValue><Modulus>riLSQFVDC229P5F+Mkicbkpg5OC8+SeL6hvkJXIGYiN/e4YnprCxuIp5sH9AwWup4WJmObPKd1jOVGm07UwgVU7CDtTaVe1Uuk78yJBwgRuSteQjHYMmH6nG5YHvvONuvkmLnyIKGygJBL+4+Qmd3GaCHRtIrdfShlH3UbPINlM=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>

私钥

<RSAKeyValue><Modulus>riLSQFVDC229P5F+Mkicbkpg5OC8+SeL6hvkJXIGYiN/e4YnprCxuIp5sH9AwWup4WJmObPKd1jOVGm07UwgVU7CDtTaVe1Uuk78yJBwgRuSteQjHYMmH6nG5YHvvONuvkmLnyIKGygJBL+4+Qmd3GaCHRtIrdfShlH3UbPINlM=</Modulus><Exponent>AQAB</Exponent><P>6yCboYtKzIezMOFzGzzW8dp7SBT8f7jTRzH1ZIKQYKF0Mq/39k80SeUvY578O031+bg6i3cbNvvAhL8XjqTtmQ==</P><Q>vZgnL5LHnNE5uUW5NBwYvZbIz6hWNzc6kyDGimI8WBBFJOI06IdYGL2VMeGVs4lt5a1tM7T3c6gzBKgDQpL+yw==</Q><DP>p5tV9YDyr/unq5d6Uxc6bar9qHN1TqJ00VJ2h9BelNNinmM70fPB5U8fSddiG/BGAF3oNdSQrNAm+zmw1DkTOQ==</DP><DQ>fxS1b1XbJmm3X1A0y5DppGqlP0t+PpRuVp/pdGhUOlLthcN540KU8kBg+IZUaXr8hq6wO7BZDNT5HW3ggYc18Q==</DQ><InverseQ>q29etXnlszOH0FlQWDL9yLfJ+EruH4VURY1mZGz/+/qvPewUwyEf+EqJkZHVXEijnSa1CiFELK2YE9PhkUp2Xw==</InverseQ><D>bZUoLqf5KwYCJDDQ85/SIW3ZD++FvF1wpQCsUAwzjCq+nONNrI5hKLqr3bAW9iFkpJshrYpBDV3rah+jZfmUFk/UZeur2+kA2r5r1or34+HiIhT4sehU1lxww4DvTzf1/1ivG4LCvUPoFtT67Zdh8pNEC27N6bFDL8fbSU7GcmE=</D></RSAKeyValue>

DER 格式

直接对二进制密钥进行binToHex后得到的密钥格式,密钥格式如下:

公钥

30818902818100C19CFA5EA25F99C482499E3A557C7D0C3ABB375B19900CF4956E39F5B1EACA46A37CEE30DDBEA72979B6DABA11D4B9BD51CB1D79ED667607E65CF53491EE6BE35155072D5EFB96BA0E0FC0B9C1DFEDFA30886F645218CC680E55A7D5568AD59283E9BB3DC82970F6B3F6DD83FB308E2C610F362C71977D428614ED5FB59EFDB10203010001

私钥

3082025D020100028181009951BFB322876658587C207F2AFC2F2638DFD4EEEE925669C4A9487A3774891AEBE638940318B1AE270784FCFBE768C8C989E33B3953D820326DFAC7862AA133F96EE216C1B3F5651D45194CA02E9926E8FF133B2C03BB22BFE8C60B13D4757F263D4A792B188A8183FAB53B193C8AF8AB8EAB7020547C20D5BC90E1B369DBA902030100010281800FD9829ECB28022D89E0331FD25AC5A906E224CA1A81A84B40D85B34BF3CDDDB999D7025E4F80D8E3A5CADA3D58AC3AB56225A0A4A4FDF9CDC79C01E16419BEE71997546C68E6408E5D9C044858DCB6393F285D3EBAAD0C75298F61B33B752EB8E1ABA4D66E5380FF52DE07929A96367673CDAE5945A0C13F3503FABEDB1758D024100C52F9745C13B77968D52F29ACDEE00F2DF07AF8025048348B054B7EFF460097CAB824212447F674B55CF74E489DD399E702D3D655C74484248E05CA2E9DCCEE7024100C70CA9EA361ED73C42627254F33A3FA81AD0AADD64D45A1E536E64C7E31737B7ED3FCC20E03A082673C6E7A7270640F6132AA295FF406D6133090E7D89397EEF024100883505936395065872AAB77683854216824536CF97C2744543B8618E5909F5C3AE5D3DF28C6A4D19D6DE84EA50E905A211EECE18343306AEF2D43869388E1445024100B061FE6776D1D974A276CE4D8CC2FF099DC96EBF84CBCF97B3E2CD177B9A655B6CB6EDD1EC20407CA2778D6B475F794D152AE0ABFE663F06B4CCBFB46A5732AD02404981E8728E85719A319C9A8C4C7D3B162BFA728AD5FD054C0A45A9A625385167F0822D2398680FD75BF29A3C20A4D72D2115ABD06F27B3819214AAC77284518A

密钥内容格式

以下均以DER格式为例

公钥

一般情况,我们会遇到两种格式公钥PKCS#1 和PKCS#8,密钥结构如下:

后续(到结尾)内容长度为0x9F字节,相当于整个文件长度-当前这3字节。
格式:type[+长度数据占用位数(可选)]+后续长度数值,
	此处type=0x30,
	长度数据占用1位(参考前面长度表述方法),
	后续长度数值=0x9F个字节

直观JSON化ASN.1(后面新学的ASN.1结构):
30_SEQUENCE{
	03_BIT_STRING: "二进制内容(看是什么,也许是纯二进制,或者是一个可以解析成ASN.1格式的内容)"
	, 05_NULL: "NULL,后面内容长度为0"
	, 30_SEQUENCE:{
		02_INTEGER: "整数"
		, 02_INTEGER: "整数"
		, 02_INTEGER: "整数"
	}
}
*/
30 81 9F //容器[30 SEQUENCE] [81 长度数据占1位] [9F 容器内有9F字节数据]

	/*
	固定内容,长度为0x0D个字节,为RSA OID(1.2.840.113549.1.1.1)

	RSAES-PKCS1-v1_5 encryption scheme: https://www.oid-info.com/get/1.2.840.113549.1.1.1
	*/
	30 0D //容器[30 SEQUENCE] [0D 容器内有0D字节数据]
		06 09 2A864886F70D010101 //06 OID 1.2.840.113549.1.1.1
		05 00 //NULL

	/*后续内容长度,后面内容长度为0x8D字节,和开头格式一致*/
	03 81 8D //03 BIT STRING,后面放的是二进制公钥数据,此内容其实还是一个结构
		00 //BIT STRING内容前面需要加00

		/*后续内容长度,后面内容长度为0x89字节,和开头格式一致*/
		30 81 89 //容器[30 SEQUENCE] [81 长度数据占1位] [89 容器内有0x89字节数据]

			/*
			RSA Modulus内容
			格式:type[+长度数据占用位数(可选)]+内容长度数值+内容,
				此处type=0x02,所有RSA相关参数都是用02,
				长度数据占用位数为1字节,
				内容长度数值=0x81个字节,
				内容为0x00-0x27这一段(参考前面什么情况下要加0x00)。
			*/
			02 81 81 //02 INTEGER整数[02 INTEGER] [81 长度数据占1位] [81 Modulus内容长x81字节]
                               0098C3DF8CDFE444CC3B58AB006BB3F6ACFCECBC1911393A69EA395D047E41220888E27336D51059D9DEA4632DB0C04DF981C4D43A73A1606BC9D7DBC825107FDA2FF446D8A76923C28C52A280B3337276C1D27A1F4C48A71DE73812E0DB84D2EF9E47B081F243968A988B0D0998DFC929337EA945674D425A486930E8E4884927

			/*RSA Exponent内容,和Modulus一样,但此处长度数据占用位数不存在*/
			02 03 //02 INTEGER整数[02 INTEGER] [03 Exponent内容长x03字节]
				010001

私钥

/*后续内容长度,后面内容长度为0x025C个字节,和公钥开头格式一致,参考公钥部分*/
30 82 025C

	/*固定版本号*/
	02 01
		00

	/*#####从这里开始后面就是内容了 注:KCS#8仅仅是在此处插入部分内容#####*/

	/*RSA Modulus内容,和公钥开头格式一致,参考公钥部分*/
	02 81 81 
		0098C3DF8CDFE444CC3B58AB006BB3F6ACFCECBC1911393A69EA395D047E41220888E27336D51059D9DEA4632DB0C04DF981C4D43A73A1606BC9D7DBC825107FDA2FF446D8A76923C28C52A280B3337276C1D27A1F4C48A71DE73812E0DB84D2EF9E47B081F243968A988B0D0998DFC929337EA945674D425A486930E8E4884927

	/*RSA Exponent*/
	02 03
		010001

	/*RSA D*/
	02 81 80
		706352591AF29E26BED689DFE25CE0F2FD94D10184295D2F44D174FC744248DE8C8E45032717437345181D9B24B925E49533108BFC3BD26B1A710351A8EB0D937D8EEAF54FC6BE0D7D355A215E7D26FF59E525C6891991657791C3492C29874EC1A0C926EF73113347668305266F2CABFB4624F4023CD7ACA19459B2C2FB9A39

	/*RSA P*/
	02 41
		00CE23A8CAB7AD1BBEEE955EFB50A325146F2323F5E08E05AB52E3D8D1BEFDF6E9331CF79D5EF5371B9C3D1CC49AD5D4DEB2222FBEF9E8F07DA81B7987FEF8DEB5

	/*RSA Q*/
	02 41
		00BDB73C2939416C8F533309D5910A6CB7E724826DE0A510E101F43F2EDEEBF20D8A007B09BFEC1B0090A0BBC3E0A8CF6642357167EEFCF9A2C443E879AEF315EB

	/*RSA DP*/
	02 40
		63573DFDCB7C8A157E66CFAA2F57200473F6AC5ACEC7C2A5A8C192F8A9A13C3F6FD972DF0D270152B5FDA18281D7B0C94D713A2F3FC2693B352B606B108E20CD

	/*RSA DQ*/
	02 41
		009210B3D7216807C749CD6FF2E85AD85DB72EDAE4F222E49DF8BD2EB04D9DE79D18F549C5B5E878D9B480AC50217697482A0DAFA4CFD221F97996B73488C84BC5

	/*RSA InverseQ*/
	02 40
		06F0F9DD96248A27FAFB94B0A93676D653A99C7C7B9DCDADE65414C6889284B924F26A23AFFA86AF3FD804453B73FDFEA9310CC909BFC9928D5F757198B1075D

参照如上格式,密钥间的相互转换也就非常简单了。

密钥格式转换

DER -> ASN

ASN = encode_base64(decode_hex(DER))

ASN -> PEM

PEM = "-----BEGIN PUBLIC KEY-----" + ASN + "-----END PUBLIC KEY-----"

About

RSA各种格式密钥在C#的使用,及加密、解密、签名、验签,此库包含公钥解密。

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages