使用转到SSH -RSA公共密钥加密消息,然后可以使用openssl rsautl -decrypt解密
我一直在尝试解决这个问题。 In Go code I am looking to take a ssh-rsa public key like:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDGnnY4LuLq7Bs7VnFk2Vs6hNTmZLkUBRRhXNFyKZOCvmhKcM7BSHkGS7+phpIzj6mTOsJEBZKHQgac46COOT3ukO/farnnDz78KIq24U/+TZmyAyNNdzOVizK5aAApvpYTQpuSlIDDltLXQkPokedE/5vCIPiwVZW0TfqT/Rdy2XXwKewDQ05xvJhX3+nymZkyJX3GJ+pTfsDkKR+suSLDN3nupThPiWK5A1ZG9bbUkxHbsAXiTKS+qwADIWOtJvfNtPX54JjCo3Gh3/Fy0Ovxn3QSQlCF/IZNbSgm6R6adjaU4kXEF6zsLq+BjDKLtEA3A0tAIBj0T+DuuxpcV3aX
和消息类似: hello-world
,然后用该密钥加密该消息。
然后,私钥将用于使用 openssl rsautl -decrypt -inkey privateKeykeyFile -in encryptedmsgfile
解密密钥
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
帖子使用SSH键和Golang 加密数据,尤其是OpenSSH公共密钥的导入, ssh package 被应用。用于加密(使用PKCS#1 V1.5填充) A>使用。
以下示例代码说明了这一点。由于openssl语句从文件中读取密文,因此将密文存储在二进制文件中是最有意义的。但是,为简单起见,在此示例中,密码文本已输出十六进制:
可能的十六进制编码的密文是:
请注意,此加密不是确定性的,即执行代码时,您将获得不同的ciphertext。
在发布的OpenSSL语句中可以解密:
考虑到:
EncryptedMsgfile
包含密文的 binary 数据,即7610da的十六进制解码。 。
(如果ciphertext直接作为二进制文件存储如上所述,则encryptedmsgfile
将与此文件相对应)。privateKeyfile
包含PEM编码的私人PKCS#1或PKCS#8密钥。如果您的密钥处于Openssh的专有格式(-----开始OpenSSH私有密钥-----...
),则必须将其转换为EG PKCS#8格式,例如,ssh_keygen
:请注意,
data.key
包含OpenSSH键,并用PKCS#8密钥覆盖。对于发布的示例,privateKeyfile
是:Encryption with SSH keys is described in detail in the post Encrypting Data With SSH Keys and Golang, in particular the import of the OpenSSH public key, for which the ssh package is applied. For encryption (with PKCS#1 v1.5 padding) the rsa package is used.
The following sample code illustrates this. Since the OpenSSL statement reads the ciphertext from a file, it makes most sense for the ciphertext to be stored in a binary file. For simplicity, however, in this example the cipher text is output hex encoded:
A possible hex encoded ciphertext is:
Note that this encryption is not deterministic, i.e. when you execute the code, you will get a different ciphertext.
Decryption is possible with the posted OpenSSL statement:
taking into account:
encryptedMsgFile
contains the binary data of the ciphertext, i.e. the hex decoding of7610da...
(if the ciphertext is stored directly as a binary file as mentioned above,encryptedMsgFile
would correspond to this file).privateKeyFile
contains the PEM encoded private PKCS#1 or PKCS#8 key. If your key is in OpenSSH's proprietary format (-----BEGIN OPENSSH PRIVATE KEY-----...
), it must be converted to e.g. PKCS#8 format, e.g. withssh_keygen
:Note that
data.key
contains the OpenSSH key and is overwritten with the PKCS#8 key. For the posted example,privateKeyFile
is: