openssl 產生secret key 的簡單用法

來源:互聯網
上載者:User

openssl 產生secret key 的簡單用法

為了給postfix, apache之類的伺服器加上ssl,加強安全係數,

就需要用到Certificate(認證),這個認證可以從官方CA得到

或者自己產生一個,在獲得這個認證的過程中,

第一步就是要產生一個Certificate Signing Request (CSR)

這樣才可以提交給官方CA來確認你的身份,產生CSR的時候

需要用到的工具是openssl, 只說一下對openssl的感性認識

比如目的是加密一個檔案plain.txt

$ cat plain.txt 

I love OpenSSL!

1. 首先看看openssl的版本

$ openssl version

OpenSSL 1.0.1c 10 May 2012

2. 然後看openssl裡面具體有哪些可用的命令

$ openssl list-standard-commands
asn1parse
ca
ciphers
cms
crl
crl2pkcs7
dgst
dh
dhparam
dsa
dsaparam
ec
ecparam
enc
engine
errstr
gendh
gendsa
genpkey
genrsa
nseq
ocsp
passwd
pkcs12
pkcs7
pkcs8
pkey
pkeyparam
pkeyutl
prime
rand
req
rsa
rsautl
s_client
s_server
s_time
sess_id
smime
speed
spkac
srp
ts
verify
version
x509

挑兩個說明一下

ca To create certificate authorities.

dgst To compute hash functions.

enc To encrypt/decrypt using secret key algorithms. It is possible to generate using a password or directly a secret key stored in a file.

genrsa This command permits to generate a pair of public/private key for the RSA algorithm.

password Generation of “hashed passwords”.

pkcs12 Tools to manage information according to the PKCS #12 standard.

pkcs7 Tools to manage information according to the PKCS #7 standard.

rand Generation of pseudo-random bit strings.

rsa RSA data management.

rsautl To encrypt/decrypt or sign/verify signature with RSA.

verify Checkings for X509.

x509 Data managing for X509.

我們要加密plain.txt要用到 enc 那個命令

然後再看一下openssl裡面支援多少種 secret key algorithms 

$ openssl list-cipher-commands
aes-128-cbc
aes-128-ecb
aes-192-cbc
aes-192-ecb
aes-256-cbc
aes-256-ecb
base64
......

這時來加密plain.txt檔案
3. 
$ openssl enc -aes-256-cbc -in plain.txt -out encrypted.bin
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
$

意思就是使用enc命令,利用-aes-256-cbc 密碼編譯演算法,產生了一個256位的secret key

這個secret key 是通過你提供的password 來計算出來的

產生的檔案是encrypted.bin

如果想要解密這個256位的secret key,則

4.
$ openssl enc -aes-256-cbc -d -in encrypted.bin -pass pass:helloI 
love OpenSSL!

已經看到, 我剛才提供的password是hello

-d 則表示需要解密


如果這時候我們想產生一對
public/private key 怎麼辦,還是同樣的原理,

只是用了不同的命令

# openssl genrsa -out key.pem 1024

這時就產生了一對1024位的RSA key

來看一下key.pem 的屬性
# cat key.pem
-----BEGIN RSA PRIVATE KEY-----
MIICwgIBAAKBlw26l3Kig8s51KAeZvL+MT62dBu9gH0kTZdZ625W3a/4hfV4KLhJ
RGx9v7QSkqpBG2I8qT9uzzqPh3nbeFlOBb1INoyl3MJ4JAsId+Z4NUwrxWxLyVQm
vSd8nn5uEI5tvZvr1xtNkh5qgmn+hfb1EqGZPGCTfQ2sugSHQjodscItMnsteBR0
...
FbU5H93Rc8oQjV1ffZ5kIohEkMSzsAx84Zt3ECbJZAowboSY9f+f0F5m7bzOlrDL
9Nkp18csfJMCTAKa4kF664SvEc/tXNwV/m+2xYXT8sbKm9c9ABBAHmVmxqtlmKoW
LDT0mhozHLYh44/vS+lma+vivgxUei0iiy/bevIz/TlX1elce9M=
-----END RSA PRIVATE KEY-----

# file key.pem
key.pem: PEM RSA private key

還可以用16進位來顯示這對RSA key的細節

# openssl rsa -in key.pem -text -noout

--noout讓我們避免使用 base 64 來顯示

其實接下來我要們做的是利用key.pem 抽出這一對key

因為我們總是想要private key隱蔽一些, 所以在這裡使用triple des(3-des)

來加密secret key

# openssl rsa -in key.pem -des3 -out enc-key.pem
writing RSA key
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:

對於public key, 無所謂, 因為人們要用publick key來發送他們加密的訊息

或者用public key 來確認訊息是來自你的

# openssl rsa -in key.pem -pubout -out pub-key.pem

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.