RSA算法具体过程这里不赘述,下面以RSA1024为例,介绍RSA密钥格式及相关加解密格式。首先利用RSA算法工具随机生成一对RSA公私钥(以PKCS1密钥格式为例)。
RSA公钥:(base64编码和对应HEX编码)
-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAME6HVwKX4ATK/btfGUK+a0JQJns6bBjNOGM29JhtZwC76xzz+vr35WvXeApIEfOsJkWV9Au7/Mg7NxOqjjQY0ZJHxo3HsyX9KJSIedZLHsKMRtR6aYbwnQpTb5yHzCMZHEVF1HcrXT3bNvVmjnbCIwlCeSPtAfuvM+u2JL11w8jAgMBAAE=
-----END RSA PUBLIC KEY-----
30818902818100c13a1d5c0a5f80132bf6ed7c650af9ad094099ece9b06334e18cdbd261b59c02efac73cfebebdf95af5de0292047ceb0991657d02eeff320ecdc4eaa38d06346491f1a371ecc97f4a25221e7592c7b0a311b51e9a61bc274294dbe721f308c6471151751dcad74f76cdbd59a39db088c2509e48fb407eebccfaed892f5d70f230203010001
RSA私钥:(base64编码和对应HEX编码)
-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQDBOh1cCl+AEyv27XxlCvmtCUCZ7OmwYzThjNvSYbWcAu+sc8/r69+Vr13gKSBHzrCZFlfQLu/zIOzcTqo40GNGSR8aNx7Ml/SiUiHnWSx7CjEbUemmG8J0KU2+ch8wjGRxFRdR3K1092zb1Zo52wiMJQnkj7QH7rzPrtiS9dcPIwIDAQABAoGAe9obLX68neQr/vdUD5QMWzwd+0uesl+0aooV8HCOrrQ3GcuVzzt+MXyZaHAT79Ls4QMwQBGF3coR6FhHxWjU5//AID16qRjI7KpzykRaGLHLI9TIQWYbi4L0/pVfeoJwhmYpGj7fXIkSePtugXnEOY5bRoyrMqcNDXlfEDmXr3ECQQDqPLSOAs01fFROHop4Do6Dvo6HnFy30n+oL61IpNhyzLICZLeArJ+Xgq3mTBxuIuq1xqYrUVcnBA51o2cEFq4rAkEA0y39akv8cDyVsAHxzyFwpHsVi4l3TVJvF7obNxqtgUFZfYhM7SVJYbNIR9tkzN1PFvQ8kKzNEL38m6mExye6QJAaRYXAucq3WYMBIgbI0WUlnJ1S03RIv8jGndZTI/4M3i2G/DHisjnSHfuQLXAw4CSwD6A6koxyWU+f3RxkRoDxQJALLwyUaE1IaWD5BwVWFZyITW9s6h0MFoFFJalC0BIfvgT+Aa2nQnoCJ1M84Pnf+9Hp3ifw6Ijii+hjKap4aSPeQJARLteSueNcZhg47Ttb3T8hMj2MUfmwryTiIu6eMMoCvJhm0O2bzwd31BTODvrbN4UaJ3wmogCYtqUiG157bdYiQ==
-----END RSA PRIVATE KEY-----
3082025b02010002818100c13a1d5c0a5f80132bf6ed7c650af9ad094099ece9b06334e18cdbd261b59c02efac73cfebebdf95af5de0292047ceb0991657d02eeff320ecdc4eaa38d06346491f1a371ecc97f4a25221e7592c7b0a311b51e9a61bc274294dbe721f308c6471151751dcad74f76cdbd59a39db088c2509e48fb407eebccfaed892f5d70f2302030100010281807bda1b2d7ebc9de42bfef7540f940c5b3c1dfb4b9eb25fb46a8a15f0708eaeb43719cb95cf3b7e317c99687013efd2ece10330401185ddca11e85847c568d4e7ffc0203d7aa918c8ecaa73ca445a18b1cb23d4c841661b8b82f4fe955f7a82708666291a3edf5c891278fb6e8179c4398e5b468cab32a70d0d795f103997af71024100ea3cb48e02cd357c544e1e8a780e8e83be8e879c5cb7d27fa82fad48a4d872ccb20264b780ac9f9782ade64c1c6e22eab5c6a62b515727040e75a3670416ae2b024100d32dfd6a4bfc703c95b001f1cf2170a47b158b89774d526f17ba1b371aad8141597d884ced254961b34847db789333753c5bd0f242b33442f7f26ea6131c9ee9024069161702e72add660c04881b2345949672754b4dd122ff231a77594c8ff83378b61bf0c78ac8e74877ee40b5c0c38092c03e80ea4a31c9653e7f7471911a03c502402cbc3251a13521a583e41c155856722135bdb3a874305a051496a50b40487ef813f806b69d09e8089d4cf383e77fef47a7789fc3a2238a2fa18ca6a9e1a48f79024044bb5e4ae78d719860e3b4ed6f74fc84c8f63147e6c2bc93888bba78c3280af2619b43b66f3c1ddf5053383beb6cde14689df09a880262da94886d79edb75889
(1)RSA公钥格式
3081 - 公钥标签头,81表示紧跟后续1个字节标识公钥长度
89 - 1字节,标识后续0x89=137=3+1+128+2+3字节内容
028181 - 3字节,标识后续为0x81 = 129字节
00 - 1字节,内容固定为0x00
c13a1d5c0a5f80132bf6ed7c650af9ad094099ece9b06334e18cdbd261b59c02efac73cfebebdf95af5de0292047ceb0991657d02eeff320ecdc4eaa38d06346491f1a371ecc97f4a25221e7592c7b0a311b51e9a61bc274294dbe721f308c6471151751dcad74f76cdbd59a39db088c2509e48fb407eebccfaed892f5d70f23 - 128字节=1024bit,RSA公钥(n,e)中的n取值
0203 - 2字节,标识e的长度为3字节
010001 - RSA公钥(n,e)中的e取值
(2)RSA私钥格式
3082 - 标签头,后面接2字节标识长度
025b - 标识长度0x025b = 603字节
02010002 - 固定序列
8181 - 标识后面的模式n的长度为0x81 = 129字节
00 - 固定值
c13a1d5c0a5f80132bf6ed7c650af9ad094099ece9b06334e18cdbd261b59c02efac73cfebebdf95af5de0292047ceb0991657d02eeff320ecdc4eaa38d06346491f1a371ecc97f4a25221e7592c7b0a311b51e9a61bc274294dbe721f308c6471151751dcad74f76cdbd59a39db088c2509e48fb407eebccfaed892f5d70f23 - 模数n的取值,这里可以看到和公钥中的模数n相同
0203010001 - 这里和公钥相同,也就是e的取值
028180 - 后接0x80字节
7bda1b2d7ebc9de42bfef7540f940c5b3c1dfb4b9eb25fb46a8a15f0708eaeb43719cb95cf3b7e317c99687013efd2ece10330401185ddca11e85847c568d4e7ffc0203d7aa918c8ecaa73ca445a18b1cb23d4c841661b8b82f4fe955f7a82708666291a3edf5c891278fb6e8179c4398e5b468cab32a70d0d795f103997af71 - d的取值
024100 - 起始序列
ea3cb48e02cd357c544e1e8a780e8e83be8e879c5cb7d27fa82fad48a4d872ccb20264b780ac9f9782ade64c1c6e22eab5c6a62b515727040e75a3670416ae2b - p的取值
024100 - 起始序列
d32dfd6a4bfc703c95b001f1cf2170a47b158b89774d526f17ba1b371aad8141597d884ced254961b34847db789333753c5bd0f242b33442f7f26ea6131c9ee9 - q的取值
0240 - 起始序列
69161702e72add660c04881b2345949672754b4dd122ff231a77594c8ff83378b61bf0c78ac8e74877ee40b5c0c38092c03e80ea4a31c9653e7f7471911a03c5 - d mod p-1的取值
0240 - 起始序列
2cbc3251a13521a583e41c155856722135bdb3a874305a051496a50b40487ef813f806b69d09e8089d4cf383e77fef47a7789fc3a2238a2fa18ca6a9e1a48f79 - d mod q-1的取值
0240 - 起始序列
44bb5e4ae78d719860e3b4ed6f74fc84c8f63147e6c2bc93888bba78c3280af2619b43b66f3c1ddf5053383beb6cde14689df09a880262da94886d79edb75889 - q inv p的取值
未完,待续......