OpenSSL之enc子命令用法

OpenSSL之enc子命令用法

enc子命令可用于对文件和标准输入进行各种对称加解密,也可用于BASE64编解码。

命令行格式:

openssl enc [options]

选项说明:

-in 指定输入文件,默认为标准输入。

-out 指定输出文件,默认为标准输出。

-e 执行加密,默认。

-d 执行解密。

-a/-base64 在加密后对密文执行BASE64编码,在解密前对密文执行BASE64解码。

-A 指定BASE64编解码都是单行处理,默认是多行。

-k 指定加密口令,不设置此项时,程序会提示用户输入口令。

-kfile 指定口令存放的文件。

-md 指定摘要算法,用于将口令生成为密钥,默认为md5。

-S 指定SALT,以16进制给出。

-K/-iv 指定密钥和IV,以16进制给出。

-[pP] 指定本参数后,会输出加解密使用的SALT、KEY、IV信息。

-bufsize 指定加解密用的缓冲区大小。

-nopad 禁用填充。

-nosalt 在根据口令生成密钥时,不使用SALT。

支持的对称密钥算法,名称列表:

-aes-128-cbc -aes-128-cbc-hmac-sha1 -aes-128-cbc-hmac-sha256

-aes-128-ccm -aes-128-cfb -aes-128-cfb1

-aes-128-cfb8 -aes-128-ctr -aes-128-ecb

-aes-128-gcm -aes-128-ofb -aes-128-xts

-aes-192-cbc -aes-192-ccm -aes-192-cfb

-aes-192-cfb1 -aes-192-cfb8 -aes-192-ctr

-aes-192-ecb -aes-192-gcm -aes-192-ofb

-aes-256-cbc -aes-256-cbc-hmac-sha1 -aes-256-cbc-hmac-sha256

-aes-256-ccm -aes-256-cfb -aes-256-cfb1

-aes-256-cfb8 -aes-256-ctr -aes-256-ecb

-aes-256-gcm -aes-256-ofb -aes-256-xts

-aes128 -aes192 -aes256

-bf -bf-cbc -bf-cfb

-bf-ecb -bf-ofb -blowfish

-camellia-128-cbc -camellia-128-cfb -camellia-128-cfb1

-camellia-128-cfb8 -camellia-128-ecb -camellia-128-ofb

-camellia-192-cbc -camellia-192-cfb -camellia-192-cfb1

-camellia-192-cfb8 -camellia-192-ecb -camellia-192-ofb

-camellia-256-cbc -camellia-256-cfb -camellia-256-cfb1

-camellia-256-cfb8 -camellia-256-ecb -camellia-256-ofb

-camellia128 -camellia192 -camellia256

-cast -cast-cbc -cast5-cbc

-cast5-cfb -cast5-ecb -cast5-ofb

-des -des-cbc -des-cfb

-des-cfb1 -des-cfb8 -des-ecb

-des-ede -des-ede-cbc -des-ede-cfb

-des-ede-ofb -des-ede3 -des-ede3-cbc

-des-ede3-cfb -des-ede3-cfb1 -des-ede3-cfb8

-des-ede3-ofb -des-ofb -des3

-desx -desx-cbc -id-aes128-CCM

-id-aes128-GCM -id-aes128-wrap -id-aes128-wrap-pad

-id-aes192-CCM -id-aes192-GCM -id-aes192-wrap

-id-aes192-wrap-pad -id-aes256-CCM -id-aes256-GCM

-id-aes256-wrap -id-aes256-wrap-pad -id-smime-alg-CMS3DESwrap

-idea -idea-cbc -idea-cfb

-idea-ecb -idea-ofb -rc2

-rc2-40-cbc -rc2-64-cbc -rc2-cbc

-rc2-cfb -rc2-ecb -rc2-ofb

-rc4 -rc4-40 -rc4-hmac-md5

-rc5 -rc5-cbc -rc5-cfb

-rc5-ecb -rc5-ofb -seed

-seed-cbc -seed-cfb -seed-ecb

-seed-ofb

用法举例:

对指定文件使用des-cbc加密和解密,从控制台输入口令。

echo hello > hello.txt

openssl enc -des-cbc -e -in hello.txt -out hello.txt.descbc

openssl enc -des-cbc -d -in hello.txt.descbc -out hello.txt2

对指定文件使用des-cbc加密和解密,由命令行指定口令。

echo hello > hello.txt

openssl enc -des-cbc -e -in hello.txt -out hello.txt.descbc -k aaa -md md5

openssl enc -des-cbc -d -in hello.txt.descbc -out hello.txt2 -k aaa -md md5

对指定文件使用des-cbc加密和解密,指定口令文件。

echo hello > hello.txt

echo aaa > pass.txt

openssl enc -des-cbc -e -in hello.txt -out hello.txt.descbc -kfile pass.txt -md md5

openssl enc -des-cbc -d -in hello.txt.descbc -out hello.txt2 -kfile pass.txt -md md5

对指定文件使用des-cbc加密和解密,同时指定KEY、IV,同时打印加解密内部使用的SALT、KEY、IV信息。

echo hello > hello.txt

openssl enc -des-cbc -e -in hello.txt -out hello.txt.descbc -K AAAA -iv AAAA -p

openssl enc -des-cbc -d -in hello.txt.descbc -out hello.txt2 -K AAAA -iv AAAA -p

在加解密过程中,加入BASE64处理(多行)。

echo hello > hello.txt

openssl enc -des-cbc -e -in hello.txt -out hello.txt.descbc -k aaa -a

openssl enc -des-cbc -d -in hello.txt.descbc -out hello.txt2 -k aaa -a

在加解密过程中,加入BASE64处理(单行)。

echo hello > hello.txt

openssl enc -des-cbc -e -in hello.txt -out hello.txt.descbc -k aaa -a -A

openssl enc -des-cbc -d -in hello.txt.descbc -out hello.txt2 -k aaa -a -A

对指定的文件进行单独的BASE64编解码。

echo hello > hello.txt

openssl enc -base64 -e -in hello.txt -out hello.txt.base64

openssl enc -base64 -d -in hello.txt.base64 -out hello.txt2

所有类如openssl enc -des-cbc的写法,也可写成openssl des-cbc的形式,如:

echo hello > hello.txt

openssl des-cbc -e -in hello.txt -out hello.txt.descbc -k aaa

openssl des-cbc -d -in hello.txt.descbc -out hello.txt2 -k aaa

程序相关:

从自己编写程序实现enc子命令的角度考虑,涉及各种对称加解密函数API用法、BASE64 API用法,参考相关的技术资料进行累加功能实现,技术上应该没有问题。

相关文章