mac ssh-keygen 生成的密钥不能被 CICD 识别的解决方案

问题

在mac 系统上使用 ssh-keygen -t rsa 生成的密钥文件如下

1
2
3
   -----BEGIN OPENSSH PRIVATE KEY-----

-----END OPENSSH PRIVATE KEY-----

例如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
➜  ~ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/fwj/.ssh/id_rsa):
Created directory '/Users/fwj/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/fwj/.ssh/id_rsa.
Your public key has been saved in /Users/fwj/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Rn6MY1IWZ1DKynXdC+9jbmYoEJI3TJOxZz+7LESXmIA fwj@fwjs-MacBook-Pro.local
The key's randomart image is:
+---[RSA 2048]----+
| .+=+ |
| E.=* . . |
| +Oo+oo.. |
| .oB=*+.oo . |
| +oS+o.o o |
| +.o. + |
| o ..+ |
| o..++. |
| oo+. |
+----[SHA256]-----+
➜ ~ cat ~/.ssh/id_rsa
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABFwAAAAdzc2gtcn
NhAAAAAwEAAQAAAQEAr3Zpx788t9nmRCGYrny67axIj3WNdJOYvBYdBS2w8I9Xt4GYNmP0
nvcWcEK5xjdjPDuH2QU+RJCNYpUciH1dbnDipEsrAbDGwN+ctbUoQ0FyeyrLrLqHcf5x87
IEMesbpzugiNXd4ff53HAMyvoIUKsS2qDiVocDqbFybWyQgGyVSChQSqjyqLxzHt6oeJh8
HU/tUuRgi7gCsAeAKuSrsG/d0hu/M9K4ij2QK6uSL3taD1ax0FHlJiHERqbl/wYsGFllfn
g3d6gtH0G0HG/7ALZ6NF1aCyWiQReXg4sfMTK4AsPkkDa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RdMaPsf5h1zpKrcYQHC
GoS/dmuOJFi1QeKCQo55RTo6VWaMaTxfPqvAdZ7Z5T9vcb+xkAAACBAMnJ0sn1id090BDw
fhmOeBOzfmgHXrrS9M6HPMTkeR+nj9TmknsgTpukQoL5eYEkgBgiY2/+mVsx17OhP/BWna
oYHGVpcN51p1ci89flqad7MJk3GKtq7yuZ4of5f2VG0Nx3Qhz0aNRE/SWkUbMJE5ILQCXW
6QXYzUZ//A5qX59PAAAAGmZ3akBmd2pzLU1hY0Jvb2stUHJvLmxvY2Fs
-----END OPENSSH PRIVATE KEY-----


而通过Linux 生成的密钥是如下所示这样

1
2
-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----

原因

BEGIN RSA PRIVATE KEY是PKCS#1,只是一个RSA密钥。它本质上只是来自PKCS#8的关键对象,但前面没有版本或算法标识符。 BEGIN PRIVATE KEY是PKCS#8,并指示密钥类型包含在密钥数据本身中。

解决办法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
➜  ~ ssh-keygen -p -m PEM -f ~/.ssh/id_rsa
Key has comment 'fwj@fwjs-MacBook-Pro.local'
Enter new passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved with the new passphrase.
➜ ~ cat ~/.ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAr3Zpx788t9nmRCGYrny67axIj3WNdJOYvBYdBS2w8I9Xt4GY
NmP0nvcWcEK5xjdjPDuH2QU+RJCNYpUciH1dbnDipEsrAbDGwN+ctbUoQ0FyeyrL
rLqHcf5x87IEMesbpzugiNXd4ff53HAMyvoIUKsS2qDiVocDqbFybWyQgGyVSChQ
SqjyqLxzHt6oeJh8HU/tUuRgi7gCsAeAKuSrsG/d0hu/M9K4ij2QK6uSL3taD1ax
0FHlJiHERqbl/wYsGFllfng3d6gtH0G0HG/7ALZ6NF1aCyWiQReXg4sfMTK4AsPk
kD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ogsrMwZE1ppilh4XKFdrSx0BgZLiE7QKYjk+A
OuHK+QKBgQDR/fOrJhB9eVQqhxZ3jr41lfkiTdCQqrtrQGfY7yeAERkF/1C8AvfQ
O6j+1oQI4cksPx++7+lcDeGHluwLlTjZEoXIC0vUW9tDiy5Eq/PVtLRArCYpqoi+
qrw+aakUW5FidNi+9lWw8wjy0guBzSVL3jWtttChrR8ixgMq/29uRA==
-----END RSA PRIVATE KEY-----
阿文 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!