OpenSSL 无法加载公钥 [英] OpenSSL unable to load Public Key

查看:314
本文介绍了OpenSSL 无法加载公钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在 OSX Yosomite 10.10.2 上通过命令行加密文本消息

Trying to encrypt a text message via command line on OSX Yosomite 10.10.2

像这样创建公共 .pem 密钥:

Created public .pem key like this:

ssh-keygen -f ~/.ssh/id_rsa.pub -e -t PKCS8 > id_rsa.pem

如果我尝试加密 myMessage.txt

If I try to encrypt myMessage.txt

openssl rsautl -encrypt -inkey ~/.ssh/id_rsa.pem -pubin -in ~/Desktop/myMessage.txt -out ~/Desktop/encrypted.txt

我得到无法加载公钥

如果我输入:

openssl asn1parse -in id_rsa.pem

返回:错误:偏移量太大

但我不知道如何解决它.我应该更改什么才能使其正常工作?

But I have no idea how to fix it. What should I change to make it work?

推荐答案

仍然不知道我的问题出了什么问题但找到了解决方案:

Still don't know what went wrong in my question but found a solution:

1) 生成 RSA 密钥:

1) Generate RSA key:

$ openssl genrsa -out key.pem 1024 
$ openssl rsa -in key.pem -text -noout 

2) 将公钥保存在 pub.pem 文件中:

2) Save public key in pub.pem file:

$ openssl rsa -in key.pem -pubout -out pub.pem 
$ openssl rsa -in pub.pem -pubin -text -noout 

3) 加密一些数据:

$ echo test test test > file.txt 
$ openssl rsautl -encrypt -inkey pub.pem -pubin -in file.txt -out file.bin 

4) 解密加密数据:

$ openssl rsautl -decrypt -inkey key.pem -in file.bin 

它就像一个魅力

感谢 Marek Marcola 提供信息 http://openssl.6102.n7.nabble.com/Re-Can-I-use-my-own-keys-with-openssl-for-RSA-enc-dec-td12506.html

Thanks to Marek Marcola for providing the information http://openssl.6102.n7.nabble.com/Re-Can-I-use-my-own-keys-with-openssl-for-RSA-enc-dec-td12506.html

这篇关于OpenSSL 无法加载公钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆