Dec 5

[译]OpenSSL Command-Line HOWTO 不指定

linuxing , 12:10 , 网络服务 » 安全相关 , 评论(0) , 引用(0) , 阅读(63643) , Via 本站原创 | |

七、加密和解密
1、使用base64编码
引用
# send encoded contents of file.txt to stdout
openssl enc -base64 -in file.txt
# same, but write contents to file.txt.enc
openssl enc -base64 -in file.txt -out file.txt.enc

命令行方式:
$ echo "encode me" | openssl enc -base64
ZW5jb2RlIG1lCg==

※注意,echo默认会输出回车符,可使用-n参数屏蔽:
$ echo -n "encode me" | openssl enc -base64
ZW5jb2RlIG1l

解密使用-d参数:
$ echo "ZW5jb2RlIG1lCg==" | openssl enc -base64 -d
encode me

2、如何简单地加密一个文件
使用cipher是一种比较简单的加密方式,下面的命令可以知道其支持的运算规则:
openssl list-cipher-commands

当你选择号一个运算规则后,需要决定是否使用base64编码,以使用可打印字符来代替二进制方式的显示(例如要发送邮件),就可以这样做:
引用
# encrypt file.txt to file.enc using 256-bit AES in CBC mode
openssl enc -aes-256-cbc -salt -in file.txt -out file.enc
# the same, only the output is base64 encoded for, e.g., e-mail
openssl enc -aes-256-cbc -a -salt -in file.txt -out file.enc

解密时,提供短语和对应的规则即可:
引用
# decrypt binary file.enc
openssl enc -d -aes-256-cbc -in file.enc
# decrypt base64-encoded version
openssl enc -d -aes-256-cbc -a -in file.enc

如果你连短语也不想手动输入,可以使用“PASS PHRASE ARGUMENTS”格式:
引用
# provide password on command line
openssl enc -aes-256-cbc -salt -in file.txt \
 -out file.enc -pass pass:mySillyPassword
# provide password in a file
openssl enc -aes-256-cbc -salt -in file.txt \
 -out file.enc -pass file:/path/to/secret/password.txt

八、错误
如果你从日志中发现一些SSL的内容错误信息,例如:
sshd[31784]: error: RSA_public_decrypt failed: error:0407006A:lib(4):func(112):reason(106)
sshd[770]: error: RSA_public_decrypt failed: error:0407006A:lib(4):func(112):reason(106)
你应该把error和lib之间的代码提取出来,并查询:
$ openssl errstr 0407006A
error:0407006A:rsa routines:RSA_padding_check_PKCS1_type_1:block type is not 01
这样,再从manual里面去搜索信息,能比较好的解决问题。

九、密钥
1、如果创建一个RSA密钥
使用genrsa选项:
引用
# default 512-bit key, sent to standard output
openssl genrsa
# 1024-bit key, saved to file named mykey.pem
openssl genrsa -out mykey.pem 1024
# same as above, but encrypted with a passphrase
openssl genrsa -des3 -out mykey.pem 1024

2、如何创建一个RSA公钥
使用rsa选项,可以从私钥创建公钥:
openssl rsa -in mykey.pem -pubout
3、如果创建一个DSA密钥
创建DSA密钥需要parameter file,而且验证比RSA要慢,所以使用范围没有RSA广泛。
如果你想创建一个单一的RSA密钥,可以:
引用
# key will be called dsakey.pem
openssl dsaparam -noout -out dsakey.pem -genkey 1024

另一方面,如果你希望多个DSA密钥共享一个parameter file,可以这样:
引用
# create parameters in dsaparam.pem
openssl dsaparam -out dsaparam.pem 1024
# create first key
openssl gendsa -out key1.pem dsaparam.pem
# and second ...
openssl gendsa -out key2.pem dsaparam.pem

4、创建elliptic curve key
OpenSSL 0.9.8以上的版本才支持elliptic curve key:
openssl ecparam -out key.pem -name prime256v1 -genkey

# -name 选型可以使用的参数,可通过下面的命令获得:
openssl ecparam -list_curves

5、如何从一个密钥移除passphrase(短语)
依赖于你使用rsa或dsa方式,使用不同的处理方法。
假设你创建的RSA密钥,并放在单独的key.pem文件,那么下面的命令后,你就可以得到一个没有短语加密,但相同编码的RSA密钥newkey.pem
引用
# you'll be prompted for your passphrase one last time
openssl rsa -in key.pem -out newkey.pem

通常,私钥和公钥可以放在同一个文件。假设叫mycert.pem。通过下面的步骤,也可以得到没有短语的文件newcert.pem
引用
# you'll need to type your passphrase once more
openssl rsa -in mycert.pem -out newcert.pem
openssl x509 -in mycert.pem >>newcert.pem
内文分页: [1] [2] [3] [4]
Tags: , , ,
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]