我正在进行关于RSA算法的纯数学演示,所以我用命令创建了一对键:
ssh-keygen -t rsa -C "my_mail@server.com"
所以它创建了文件id_rsa和id_rsa.pub,因为这些文件仅用于示范海豚我没有问题显示它们,这些是
id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA3WqSN/xKwYb3NcgkLV0FO+KSKTcAQR/LChN6gXfHi8n+DXy9
4Ms4RnXohW2pSFinTs+k0I6VEgiwUYGqO5Ak6S5k6L4DuOG2frQtLkIvapV+LG4W
8jkWXRgsyrtne3iUTjbUa9/iDPN6hsGuCDANLjodu8dxzOXNApX7EyIv+9NpabcF
kt8AUwN8z8mCz52j4fhzOD/0DlfHWj1trqtNkq9dzKyY+4FOpgxTUSmKBxzuYT6m
f8T3E8S9eHPA9GB9ZidauqdOYhP8YYJp9cTHaxZFpmzjBfY0wAajOjN7oLWuoXRD
Ave8pI6WMABLlC4duVym6hFW+SIjlb0SlitF5QIDAQABAoIBAQDWK1+EB+XHjGVT
AGT9/Bwz8NSwSSNV2wrqlCzLTrEV5ix3n1GLPwcQILzpU0mLUTWEZhVmJoTLDNx/
+oxPUP5jDx1Mg3/WNX3w0Rdix2JWFoQVNee8JLwdEGVDNELEi73oaeDO96AQJvM9
pdpQ0SnurQhVunW5XA8RkUFrIIaJbgjCrCajNdh0uilGXj8MZInQoApCsVd0Eax4
/X1iaCndkAnYGsy7s3YQMrZ3iPigbLG4nuYgS8NYDLQpEX9vq9bqbER5wUjfldyp
0nN3mSltvFItkzW6LfYsgRD86KVUwqMq/EjN6xNX1M4pSfRh9BQKcgwLWQwBDkyw
fcOn4zwBAoGBAPIaiMVf2oIV0bEUCdkY+rFGoNHWgkeMBfbxeqfZ7K9sXp7eLUic
Knciqzh/GQ9PM6H6g8PhedLXonFtzShNG+dFW+UX3fFTdfJDgX0MUih+6qDp5QkI
0Ej1K5y7g9pOEVOxbgyHtN0s38Li6oZHGRnbf7tDrnMpZP0LGdZceiNpAoGBAOog
Dq5/Z8HpYT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h25XKbqEVb5IiOVvRKWK0Xl my_mail@server.com
但当我检查https://en.wikipedia.org/wiki/RSA_(cryptosystem)中的信息时,我发现算法是: c = m ^ e mod n m = c ^ d mod n
,其中
e is public key
d is private key
n is the common module
我的问题是在文件中哪个部分是e,哪个是d,哪个是n?
或
有没有办法从文件中读取它并将其直接插入gmp c ++库的mpz_class中?
任何这些对我来说都足够了
答案 0 :(得分:2)
-m
的{{1}}命令行选项记录在手册页中,如下所示:
ssh-keygen
key_format : 指定-m
(导入)或-i
(导出)转换选项的密钥格式。支持的密钥格式为:“RFC4716”(RFC 4716 / SSH2公钥或私钥),“PKCS8”(PEM PKCS8公钥)或“PEM”(PEM公钥)。默认转换格式为“RFC4716”。
由Artjom B.表示数据是某种形式的ASN.1,让我们导出到PEM并将结果提供给-e
:
openssl asn1parse
这是公共密钥$ ssh-keygen -m PEM -e -f id_rsa.pub | openssl asn1parse -i
0:d=0 hl=4 l= 266 cons: SEQUENCE
4:d=1 hl=4 l= 257 prim: INTEGER :DD6A9237FC4AC186F735C8242D5D053BE292293700411FCB0A137A8177C78BC9FE0D7CBDE0CB384675E8856DA94858A74ECFA4D08E951208B05181AA3B9024E92E64E8BE03B8E1B67EB42D2E422F6A957E2C6E16F239165D182CCABB677B78944E36D46BDFE20CF37A86C1AE08300D2E3A1DBBC771CCE5CD0295FB13222FFBD36969B70592DF0053037CCFC982CF9DA3E1F873383FF40E57C75A3D6DAEAB4D92AF5DCCAC98FB814EA60C5351298A071CEE613EA67FC4F713C4BD7873C0F4607D66275ABAA74E6213FC618269F5C4C76B1645A66CE305F634C006A33A337BA0B5AEA1744302F7BCA48E9630004B942E1DB95CA6EA1156F9222395BD12962B45E5
265:d=1 hl=2 l= 3 prim: INTEGER :010001
,后跟公钥n
,两者都以十六进制表示法给出。看私钥:
e
将其与the document Artjom referenced进行比较,您可以猜测此9个整数的序列可能对应于$ openssl asn1parse -i < id_rsa
0:d=0 hl=4 l=1188 cons: SEQUENCE
4:d=1 hl=2 l= 1 prim: INTEGER :00
7:d=1 hl=4 l= 257 prim: INTEGER :DD6A9237FC4AC186F735C8242D5D053BE292293700411FCB0A137A8177C78BC9FE0D7CBDE0CB384675E8856DA94858A74ECFA4D08E951208B05181AA3B9024E92E64E8BE03B8E1B67EB42D2E422F6A957E2C6E16F239165D182CCABB677B78944E36D46BDFE20CF37A86C1AE08300D2E3A1DBBC771CCE5CD0295FB13222FFBD36969B70592DF0053037CCFC982CF9DA3E1F873383FF40E57C75A3D6DAEAB4D92AF5DCCAC98FB814EA60C5351298A071CEE613EA67FC4F713C4BD7873C0F4607D66275ABAA74E6213FC618269F5C4C76B1645A66CE305F634C006A33A337BA0B5AEA1744302F7BCA48E9630004B942E1DB95CA6EA1156F9222395BD12962B45E5
268:d=1 hl=2 l= 3 prim: INTEGER :010001
273:d=1 hl=4 l= 257 prim: INTEGER :D62B5F8407E5C78C65530064FDFC1C33F0D4B0492355DB0AEA942CCB4EB115E62C779F518B3F071020BCE953498B5135846615662684CB0CDC7FFA8C4F50FE630F1D4C837FD6357DF0D11762C7625616841535E7BC24BC1D1065433442C48BBDE869E0CEF7A01026F33DA5DA50D129EEAD0855BA75B95C0F1191416B2086896E08C2AC26A335D874BA29465E3F0C6489D0A00A42B1577411AC78FD7D626829DD9009D81ACCBBB3761032B67788F8A06CB1B89EE6204BC3580CB429117F6FABD6EA6C4479C148DF95DCA9D2737799296DBC522D9335BA2DF62C8110FCE8A554C2A32AFC48CDEB1357D4CE2949F461F4140A720C0B590C010E4CB07DC3A7E33C01
534:d=1 hl=3 l= 129 prim: INTEGER :F21A88C55FDA8215D1B11409D918FAB146A0D1D682478C05F6F17AA7D9ECAF6C5E9EDE2D489C2A7722AB387F190F4F33A1FA83C3E179D2D7A2716DCD284D1BE7455BE517DDF15375F243817D0C52287EEAA0E9E50908D048F52B9CBB83DA4E1153B16E0C87B4DD2CDFC2E2EA86471919DB7FBB43AE732964FD0B19D65C7A2369
666:d=1 hl=3 l= 129 prim: INTEGER :EA200EAE7F67C1E9613AFF0BC193DC1B194CAAACD706B0289699F4D29FFF4FAF73595367C665486D03CBD1E51027D874885769FE67D101B7391EFB821EA862AB7D702A248204EB9A0C71CFB35D71741ECDF42BE7354C7AA0DCDA84C22B29AB217AEE58B599ADBD35DF8520465EA5708C775C609C8B5F819442549408C519CB1D
798:d=1 hl=3 l= 128 prim: INTEGER :2D486D408D972F31A990F770881CB5F72680B9FEE74B3E937706DFAC39849DA325678DA2E6ABDEFD7D8C8CFAA8D3463B2016DD426C0FFF31B9FE814D1836A3FB73EBA643FA8D42D112C931951412E1E42B3A81C5071590EFB47BBDCB0CF46E19AB8FCA47A9B25894261E2B97361A624585DAB44A0A7D1BC6F9D573B5B68259D1
929:d=1 hl=3 l= 128 prim: INTEGER :1877AB311F4D779D6D34DE2C9B59597A5785849CB5881466D97E5370A399E9F8FB040DCC5D967E202E7261881FCD1A38EB7ABF3000277E976857CD8F06A730509DB6582122996309E15D0F88BE2E3A2571FE9F8BF52CFDEFCBC4E674E84FDD7B8D3E5574FD0F84239A2D6E770C331BF502105C645FC8666159213FAB3EF5F81D
1060:d=1 hl=3 l= 129 prim: INTEGER :B6018E371D8FD340005ED12A72811C157F24CD80BB1509936EF81299F55DF07385AF8AAF027974A8783E8CDE3F1A764548B33FD047089ABB745ABE831F1F723A2B4E05F9ADD3B5FB82BE56EE8F858E246535B36F620FC0D97A7BBFBC37D678D159215BE1C767BBD1A85DA72D196D5EEB62880C7AC01AFB454E43465F2B56B427
,version
,modulus
,publicExponent
,{{ 1}},privateExponent
,prime1
,prime2
,exponent1
。您可以识别exponent2
和coefficient
的值,因为它们与您在公钥中的值相匹配。因此,为了读取私钥,我们知道解析类似PEM的文件就足够了。您可能可以链接libopenssl并使用其API为您执行此操作。
但是modulus
的格式是什么?如果您运行没有publicExponent
标志的命令,您将得到类似
id_rsa.pub
该输出主要部分的base64数字正是来自-m
行的数字,因此它必须是此“RFC4716”格式的略微变体。阅读RFC 4716 section 3.4:
<强> 3.4。公钥文件正文
公钥文件的主体是base64编码的([RFC2045]) [RFC4253], Section 6.6指定的公钥数据:
$ ssh-keygen -e -f id_rsa.pub ---- BEGIN SSH2 PUBLIC KEY ---- Comment: "2048-bit RSA, converted by … from OpenSSH" AAAAB3NzaC1yc2EAAAADAQABAAABAQDdapI3/ErBhvc1yCQtXQU74pIpNwBBH8sKE3qBd8 eLyf4NfL3gyzhGdeiFbalIWKdOz6TQjpUSCLBRgao7kCTpLmTovgO44bZ+tC0uQi9qlX4s bhbyORZdGCzKu2d7eJRONtRr3+IM83qGwa4IMA0uOh27x3HM5c0ClfsTIi/702lptwWS3w BTA3zPyYLPnaPh+HM4P/QOV8daPW2uq02Sr13MrJj7gU6mDFNRKYoHHO5hPqZ/xPcTxL14 c8D0YH1mJ1q6p05iE/xhgmn1xMdrFkWmbOMF9jTABqM6M3ugta6hdEMC97ykjpYwAEuULh 25XKbqEVb5IiOVvRKWK0Xl ---- END SSH2 PUBLIC KEY ----
与所有其他线一样,身体中的每条线都不能更长 超过72个8位字节,不包括行终止字符。
&#34; ssh-rsa&#34;密钥格式具有以下特定编码:
id_rsa.pub
此处使用的 string certificate or public key format identifier
byte[n] key/certificate data
和 string "ssh-rsa"
mpint e
mpint n
类型在RFC4251 Section 5中进行了说明。本质上,每个都是一个32位数字,给出以字节为单位的长度,然后是实际数据。如果仔细观察,可以识别与其他表示形式相同的整数。
string
我已编辑输出以使数据边界更加清晰可见。查看十六进制输出,您会看到以下三个部分:
mpint