我尝试使用bashshell脚本从openssl命令中提取私有指数

9rygscc1  于 2023-08-07  发布在  Shell
关注(0)|答案(3)|浏览(107)

我正在使用以下命令来获取使用openssl命令的私钥。

  1. openssl genpkey -algorithm rsa -pkeyopt rsa_keygen_bits:1024 -out key_rsa_private_1024.pem
  2. openssl rsa -inform PEM -in key_rsa_private_1024.pem -text
    我想从响应中提取私有指数。响应看起来像这样
Private-Key: (1024 bit)
modulus:
    00:c8:e7:ab:e5:98:f7:f1:45:d6:4c:b2:73:6b:cb:
    4f:79:24:ce:6f:4d:60:b9:6a:f6:3b:cf:5b:a6:f8:
    00:da:bf:16:3d:d6:25:ed:86:63:56:4f:c5:5b:47:
    05:e7:d0:22:b2:d4:d4:ae:de:5c:51:8a:57:9b:46:
    59:7c:32:96:c8:35:8a:b8:f3:77:6c:5e:99:8e:3a:
    bb:82:e0:18:66:eb:ee:cf:c9:fd:84:e1:1e:eb:8a:
    c1:7a:88:4e:65:25:90:16:4f:3e:a7:13:e3:af:91:
    c1:d0:0a:2b:c7:ca:28:cb:a2:3f:5c:98:4c:05:b9:
    b2:97:1d:e8:fa:ef:1d:88:6d
publicExponent: 65537 (0x10001)
privateExponent:
    08:da:6c:b0:c9:74:e3:a4:f8:34:99:08:8d:0f:80:
    89:0e:1e:2a:b9:76:31:b5:8c:f4:45:c6:fc:2c:a1:
    9d:02:1e:b0:ca:e7:10:f8:83:18:22:9b:c7:d1:f1:
    68:ad:bb:59:b6:17:49:3d:73:44:04:9c:a7:b3:09:
    ee:77:2c:3d:c2:58:18:5a:e1:25:c9:0a:bc:b1:74:
    e8:6c:e1:42:d2:36:5f:80:64:97:00:3b:b6:5c:4c:
    28:7e:a1:26:d2:47:19:9f:d2:64:1b:24:88:54:ab:
    37:14:74:38:c3:46:08:0f:4c:6d:ec:4e:03:45:1f:
    fc:e7:db:e4:81:3c:91:e1
prime1:
    00:f0:e3:e7:e6:14:46:fb:38:69:ec:cb:db:52:e5:
    98:9d:f5:95:d7:9b:e4:18:55:ac:0f:b3:ab:8a:4c:
    02:f5:14:eb:97:8b:e6:47:67:7a:8f:ce:5d:68:13:
    5c:c5:23:eb:54:7d:39:ac:90:ab:96:ae:6f:3c:99:
    2a:16:c5:f2:93
prime2:
    00:d5:81:b3:b6:bd:25:e7:3b:c2:12:39:f2:8f:e2:
    b3:0c:a0:6e:88:5b:4c:ad:99:26:a8:ec:c3:66:1b:
    6d:f0:d7:b7:ee:a7:c4:90:26:cd:5d:d3:34:42:11:
    92:c6:b8:b3:dd:50:eb:8a:f0:93:4c:91:18:4a:e7:
    2f:6a:54:78:ff
exponent1:
    68:63:5a:ea:e9:7e:98:ff:29:67:e4:2f:77:0c:e7:
    c6:0b:d6:93:48:8a:df:c0:c0:7f:4d:6c:d8:d1:77:
    b6:9d:0a:3a:f1:f5:28:9c:3f:10:72:6d:27:dc:bc:
    26:f2:19:d8:d3:2b:5b:0c:96:4f:05:e8:4e:6a:51:
    42:ed:24:e1
exponent2:
    00:c6:a6:ac:40:9d:92:d3:a7:fd:35:1a:27:7f:0a:
    cd:cf:34:c2:43:2a:b7:98:5a:03:e7:6e:8b:8b:c8:
    7f:ad:73:f8:d6:1b:d0:be:d2:eb:87:9d:c8:64:a4:
    97:2d:40:93:51:9b:45:dc:fb:64:01:fc:8a:5e:be:
    e5:96:08:4c:9f
coefficient:
    26:d1:13:7c:27:d3:7c:7d:b4:fc:ad:a3:05:f5:e8:
    0e:26:2e:58:03:60:1c:27:cf:28:67:1e:be:f7:bd:
    56:ae:d2:34:48:0d:00:26:e5:8c:e5:93:94:06:c3:
    ce:ee:8b:cb:f9:de:19:17:21:c0:2e:01:0b:9b:9c:
    70:5a:f0:e2
writing RSA key
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDI56vlmPfxRdZMsnNry095JM5vTWC5avY7z1um+ADavxY91iXt
hmNWT8VbRwXn0CKy1NSu3lxRilebRll8MpbINYq483dsXpmOOruC4Bhm6+7Pyf2E
4R7risF6iE5lJZAWTz6nE+OvkcHQCivHyijLoj9cmEwFubKXHej67x2IbQIDAQAB
AoGACNpssMl046T4NJkIjQ+AiQ4eKrl2MbWM9EXG/CyhnQIesMrnEPiDGCKbx9Hx
aK27WbYXST1zRAScp7MJ7ncsPcJYGFrhJckKvLF06GzhQtI2X4BklwA7tlxMKH6h
JtJHGZ/SZBskiFSrNxR0OMNGCA9MbexOA0Uf/Ofb5IE8keECQQDw4+fmFEb7OGns
y9tS5Zid9ZXXm+QYVawPs6uKTAL1FOuXi+ZHZ3qPzl1oE1zFI+tUfTmskKuWrm88
mSoWxfKTAkEA1YGztr0l5zvCEjnyj+KzDKBuiFtMrZkmqOzDZhtt8Ne37qfEkCbN
XdM0QhGSxriz3VDrivCTTJEYSucvalR4/wJAaGNa6ul+mP8pZ+QvdwznxgvWk0iK
38DAf01s2NF3tp0KOvH1KJw/EHJtJ9y8JvIZ2NMrWwyWTwXoTmpRQu0k4QJBAMam
rECdktOn/TUaJ38Kzc80wkMqt5haA+dui4vIf61z+NYb0L7S64edyGSkly1Ak1Gb
Rdz7ZAH8il6+5ZYITJ8CQCbRE3wn03x9tPytowX16A4mLlgDYBwnzyhnHr73vVau
0jRIDQAm5Yzlk5QGw87ui8v53hkXIcAuAQubnHBa8OI=
-----END RSA PRIVATE KEY-----

字符串
期望的响应是

privateExponent:
        08:da:6c:b0:c9:74:e3:a4:f8:34:99:08:8d:0f:80:
        89:0e:1e:2a:b9:76:31:b5:8c:f4:45:c6:fc:2c:a1:
        9d:02:1e:b0:ca:e7:10:f8:83:18:22:9b:c7:d1:f1:
        68:ad:bb:59:b6:17:49:3d:73:44:04:9c:a7:b3:09:
        ee:77:2c:3d:c2:58:18:5a:e1:25:c9:0a:bc:b1:74:
        e8:6c:e1:42:d2:36:5f:80:64:97:00:3b:b6:5c:4c:
        28:7e:a1:26:d2:47:19:9f:d2:64:1b:24:88:54:ab:
        37:14:74:38:c3:46:08:0f:4c:6d:ec:4e:03:45:1f:
        fc:e7:db:e4:81:3c:91:e1


我已经尝试了“grep privateExponent”和awk,但没有得到所需的响应。

drnojrws

drnojrws1#

追加

| sed -n '/^privateExponent:/,/[^:]$/p'

字符串
打印所有行,其中第一行以privateExponent:开始(^),最后一行以:结束($)。
输出量:

privateExponent:
    08:da:6c:b0:c9:74:e3:a4:f8:34:99:08:8d:0f:80:
    89:0e:1e:2a:b9:76:31:b5:8c:f4:45:c6:fc:2c:a1:
    9d:02:1e:b0:ca:e7:10:f8:83:18:22:9b:c7:d1:f1:
    68:ad:bb:59:b6:17:49:3d:73:44:04:9c:a7:b3:09:
    ee:77:2c:3d:c2:58:18:5a:e1:25:c9:0a:bc:b1:74:
    e8:6c:e1:42:d2:36:5f:80:64:97:00:3b:b6:5c:4c:
    28:7e:a1:26:d2:47:19:9f:d2:64:1b:24:88:54:ab:
    37:14:74:38:c3:46:08:0f:4c:6d:ec:4e:03:45:1f:
    fc:e7:db:e4:81:3c:91:e1


参见man sedThe Stack Overflow Regular Expressions FAQ

2nc8po8w

2nc8po8w2#

您可以使用sed命令解析PEM格式的私钥:

sed -n '/^privateExponent/,${p;/[^:]$/q}'

字符串

3mpgtkmj

3mpgtkmj3#

有点乱:

mawk '$__ == "privateExponent:" && (_ = NR + 10)^(NF = __) || 
                                 NR <_,  NR<_ || _ = !_' FS=':'

字符串

相关问题