openssl是個密碼工具集,提供多端接口調用方式web
組成: 1. 代碼庫 libcryto ,libssl(ssl/tls) 2. 工具集 openssl
對稱加密主要是用aes,des算法算法
須要注意的是解密不要在源文件操做,不然解密失敗源文件也沒有了數據庫
usage: enc -ciphername [-AadePp] [-base64] [-bufsize number] [-debug] [-in file] [-iv IV] [-K key] [-k password] [-kfile file] [-md digest] [-none] [-nopad] [-nosalt] [-out file] [-pass arg] [-S salt] [-salt] -e 指定加密算法 -d 解密 -a 使用base64編碼 -base64 使用base64解碼 -in 要加密文件存放位置 -out 加密後的文件存放位置 -k 輸入密碼 -iv 輸入一個向量 加密 $ openssl enc -e aes-128-cbc -in secret.txt -out myAes128.txt 解密 $ openssl enc -d aes-128-cbc -in myAes128.txt -out
使用最多的是sha256,sha512,hmac服務器
md5再也不推薦使用,推薦使用sha-2ide
1.輸出值的數據長度不變 2.相同的輸入輸出也一定相同 3.輸入類似的數據,輸出也大不相同 4.輸入徹底不一樣的數據,輸出相同的哈希值會以極低的機率出現
openssl dgst options are -c to output the digest with separating colons -r to output the digest in coreutils format -d to output debug info -hex output as hex dump -binary output in binary form -hmac arg set the HMAC key to arg -verify file verify a signature using public key in file -prverify file verify a signature using private key in file -keyform arg key file format (PEM or ENGINE) -out filename output to filename rather than stdout -signature file signature to verify -sigopt nm:v signature parameter -hmac key create hashed MAC with key -mac algorithm create MAC (not neccessarily HMAC) -md4 to use the md4 message digest algorithm -md5 to use the md5 message digest algorithm -ripemd160 to use the ripemd160 message digest algorithm -sha to use the sha message digest algorithm -sha1 to use the sha1 message digest algorithm -sha224 to use the sha224 message digest algorithm -sha256 to use the sha256 message digest algorithm -sha384 to use the sha384 message digest algorithm -sha512 to use the sha512 message $ cat test.txt | openssl dgst -sha256 -hex -out hash.txt
1. 生成密鑰: 用於對稱密碼和消息認證碼 2. 生成密鑰對:用於公鑰密碼和數字簽名 3. 生成初始化向量(IV):用於分組密碼的CBC,CFB和OFB模式 4. 生成nonce, 用於防護重放攻擊以及分組密碼的CTR模式等 5. 生成鹽,用於基於口令的密碼(PBE)等
openssl rand [option] 字節數 where options are -out file - write to file -engine e - use engine e, possibly a hardware device. -rand file:file:... - seed PRNG from files -base64 - base64 encode output -hex - hex encode output $ openssl rand -hex 1 $ openssl rand -base64 10 -out a.txt
通常基於口令的密鑰爲了防止字典攻擊會使用一串隨機數加入單向散列函數函數
openssl passwd Usage: passwd [options] [passwords] where options are -crypt standard Unix password algorithm (default) -1 MD5-based password algorithm -apr1 MD5-based password algorithm, Apache variant -salt string use provided salt -in file read passwords from file -stdin read passwords from stdin -noverify never verify when reading password from terminal -quiet no warnings -table format output as table -reverse switch table columns $ openssl passwd -salt 123
首先使用genrsa生成私鑰,而後在使用rsa從私鑰中提取公鑰工具
openssl genrsa usage: genrsa [args] [numbits] -des encrypt the generated key with DES in cbc mode -des3 encrypt the generated key with DES in ede cbc mode (168 bit key) -idea encrypt the generated key with IDEA in cbc mode -seed encrypt PEM output with cbc seed -aes128, -aes192, -aes256 encrypt PEM output with cbc aes -camellia128, -camellia192, -camellia256 encrypt PEM output with cbc camellia -out file output the key to 'file -passout arg output file pass phrase source -f4 use F4 (0x10001) for the E value -3 use 3 for the E value -engine e use engine e, possibly a hardware device. $ openssl genrsa -out rsa.key 2048 //生成公鑰 $ openssl rsa -in rsa.key -pubout -out rsa-public.key
對證書所發佈的公鑰進行權威的認證,證書能夠有效的避免中間人攻擊的問題ui
1. PKC:Public-Key Certificate,公鑰證書,簡稱證書。 2. CA:Certification Authority,認證機構。對證書進行管理,負責 1.生成密鑰對、2. 註冊公鑰時對身份進行認證、3. 頒發證書、4. 做廢證書。其中負責註冊公鑰和身份認證的,稱爲 RA(Registration Authority 註冊機構) 3. PKI:Public-Key Infrastructure,公鑰基礎設施,是爲了更高效地運用公鑰而制定的一系列規範和規格的總稱。比較著名的有PKCS(Public-Key Cryptography Standards,公鑰密碼標準,由 RSA 公司制定)、X.509 等。PKI 是由使用者、認證機構 CA、倉庫(保存證書的數據庫)組成。 CRL:Certificate Revocation List 證書做廢清單,是 CA 宣佈做廢的證書一覽表,會帶有 CA 的數字簽名。通常由處理證書的軟件更新 CRL 表,並查詢證書是否有效。 4.證書的編碼格式:pem,der
1. 生成申請請求 2. RA驗證 3. CA簽署 4. 獲取證書 (1)建立所須要的文件 touch /etc/pki/CA/index.txt 生成證書索引數據庫文件 echo 01 > /etc/pki/CA/serial 指定第一個頒發證書的序列號 (2)CA自簽證書 (1)生成私鑰 cd /etc/pki/CA/ (umask 066; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048) (2)生成自簽名證書 openssl req -new -x509 –key /etc/pki/CA/private/cakey.pem -days 7300 -out /etc/pki/CA/cacert.pem -new: 生成新證書籤署請求 -x509: 專用於CA生成自簽證書 -key: 生成請求時用到的私鑰文件 -days n:證書的有效期限 -out /PATH/TO/SOMECERTFILE: 證書的保存路徑 (3)頒發證書 A 在須要使用證書的主機生成證書請求 給web服務器生成私鑰 (umask 066; openssl genrsa -out /etc/pki/tls/private/test.key 2048) 生成證書申請文件 openssl req -new -key /etc/pki/tls/private/test.key -days 365 -out etc/pki/tls/test.csr B 將證書請求文件傳輸給CA C CA簽署證書,並將證書頒發給請求者 openssl ca -in /tmp/test.csr –out /etc/pki/CA/certs/test.crt -days 365 注意:默認國家,省,公司名稱三項必須和CA一致 D 查看證書中的信息: openssl x509 -in /PATH/FROM/CERT_FILE -noout -text|issuer|subject|serial|dates openssl ca -status SERIAL 查看指定編號的證書狀態 (4) 吊銷證書 A 在客戶端獲取要吊銷的證書的serial openssl x509 -in /PATH/FROM/CERT_FILE -noout -serial -subject B 在CA上,根據客戶提交的serial與subject信息,對比檢驗是 否與index.txt文件中的信息一致,吊銷證書: openssl ca -revoke /etc/pki/CA/newcerts/SERIAL.pem C 指定第一個吊銷證書的編號 注意:第一次更新證書吊銷列表前,才須要執行 echo 01 > /etc/pki/CA/crlnumber D 更新證書吊銷列表 openssl ca -gencrl -out /etc/pki/CA/crl/crl.pem 查看crl文件: openssl crl -in /etc/pki/CA/crl/crl.pem -noout -text