Go to main content

手册页部分 1:用户命令

退出打印视图

更新时间: 2022年7月27日 星期三
 
 

decrypt(1)

名称

encrypt, decrypt - 加密或解密文件

用法概要

/usr/bin/encrypt -l
/usr/bin/encrypt -a algorithm [-v]
     [-k key_file | -K key_label [-T token_spec]]
     [-i input_file] [-o output_file]
/usr/bin/decrypt -l
/usr/bin/decrypt -a algorithm [-v]
     [-k key_file | -K key_label [-T token_spec]]
     [-i input_file] [-o output_file]

描述

These utilities encrypt or decrypt the given input file using the algorithm specified.If no input file is specified, input is read from standard input.If no output file is specified, output is printed to standard output.

If the –i and –o options specify the same file, the output is written to a temporary work file in the same file system and then renamed to replace the original input file.

encrypt 的输出文件和 decrypt 的输入文件包含以下信息:

  • 输出格式版本号,采用网络字节顺序的 4 个字节。当前版本是 1。

  • 密钥生成函数中使用的迭代,采用网络字节顺序的 4 个字节。

  • IV (ivlen bytes)[1].IV data is generated by random bytes equal to one block size.

  • 密钥生成中使用的 Salt 数据(16 字节)。

  • Cipher text data.

选项

支持以下选项:

–a algorithm

指定加密或解密过程中要使用的算法的名称。Note that some weak algorithms may be available for use with the decrypt command only and not for encryption.有关详细信息,请参见“用法”部分的“算法”。

–i input_file

指定输入文件。The default is standard input if –i is not specified.

–k key_file

指定包含用于加密算法的密钥值的文件。每种算法都具有特定的密钥材料要求,如 PKCS#11 规范中所述。如果未指定 –kencrypt 会使用 getpassphrase(3C) 提示您提供密钥材料。密钥文件的大小确定了密钥长度,从终端设置的口令短语始终用于为密钥长度可变的密码生成长度为 128 位的密钥。

有关生成密钥文件的信息,请参见 pktool(1) 中的 genkey 子命令。Alternatively, dd(8) can be used to read data from the random(4D) device to generate a key file.

–K key_label

指定 PKCS#11 令牌中的对称令牌密钥的标签。

–l

显示系统上可用的算法的列表。此列表可依加密框架的配置而变化。The list is also likely to be different for the encrypt and decrypt commands, as some algorithms such as arcfour and des can no longer be used for encryption, but only for decryption.以位为单位显示密钥大小。

–o output_file

Specify output file.The default is standard output if –o is not specified.If standard output is used without redirecting to a file, the terminal window can appear to hang because the raw encrypted or decrypted data has disrupted the terminal emulation, much like viewing a binary file can do at times.

–T token_spec

指定 PKCS#11 令牌,而不使用指定 –K 时的缺省软令牌对象存储。

token_spec 的格式为:

token_name [:manuf_id [:serial_no]]

当令牌标签包含结尾空格时,为方便起见,此选项不要求用户键入这些空格。

Colon separated token identification string.如果任一部分中包含冒号 (:) 文本字符,必须使用反斜杠 (\) 对其进行转义。如果未找到冒号 (:),则将整个字符串(最多 32 个字符)视为令牌标签。如果仅找到一个冒号 (:),则该字符串是令牌标签和生产商。

–v

Display verbose information.See Verbose section.

用法

Algorithms

The supported algorithms are displayed with their minimum and maximum key sizes by the –l option.这些算法由加密框架提供。所支持的每种算法都是 PKCS #11 机制的一个别名,对于特定的算法类型而言,PKCS #11 机制是最常用的也是限制最少的版本。例如,aesCKM_AES_CBC_PAD 的别名,3desCKM_DES3_CBC_PAD 的别名。不支持没有任何填充或 ECB 的算法变体。

这些别名与 –a 选项一起使用,并且区分大小写。

口令短语

若在加密和解密任务期间未使用 –k 选项,则会提示用户提供口令短语。使用 PKCS #5 中指定的 PBKDF2 算法将该口令短语处理成更安全的密钥。

When a passphrase is used with encrypt and decrypt, the user entered passphrase is turned into an encryption key using the PBKDF2 algorithm as defined in PKCS #5 v2.0.

Verbose

如果向命令提供输入文件,则屏幕上会显示一个进度条。进度条会在每完成 25% 时使用一个管道符号 (|) 表示。如果输入来自标准输入,每读取 40KB 后都会显示一个句点 (.)。在两种输入方法都完成时,会列显 Done

示例

示例 1 列出可与 encrypt 结合使用的算法

以下示例列出了可用的算法:

example$ encrypt -l
     Algorithm       Keysize:  Min   Max (bits)
     -----------------------------------
     aes                       128   256
     3des                      128   192
     camellia                  128   256
示例 2 Encrypting Using AES

The following example encrypts using AES and prompts for a passphrase:

example$ encrypt -a aes -i myfile.txt -o secretstuff
示例 3 对密钥文件使用 AES 加密

以下示例在已创建密钥文件后使用 AES 加密:

example$ pktool genkey keystore=file keytype=aes keylen=128 \
            outkey=key
example$ encrypt -a aes -k key -i myfile.txt -o secretstuff
示例 4 Using Pipes to Provide Encrypted Tape Backup

The following example uses pipes to provide encrypted tape backup:

example$ tar xcf - mydata | encrypt -a aes \
     -k ./backup.key | dd of=/dev/rmt/0
示例 5 Using Pipes to Restore Tape Backup

The following example uses pipes to restore a tape backup:

example$ decrypt -a aes -k ./backup.key \
     -i /dev/rmt/0 | tar zxvf -
示例 6 使用 3DES 算法加密输入文件

以下示例使用存储在 des3key 文件中的 192 位密钥加密 inputfile 文件:

example$ encrypt -a 3des -k des3key -i inputfile -o outputfile
示例 7 使用 AES 令牌密钥对输入文件进行加密

以下示例使用软令牌密钥库中的 AES 令牌密钥对输入文件进行加密。可使用 pktool(1) 生成 AES 令牌密钥:

example$ encrypt -a aes -K myaeskey \
     -T "Sun Software PKCS#11 softtoken" -i inputfile \
     -o outputfile
示例 8 列出可与 decrypt 结合使用的算法

以下示例列出了可与 decrypt 命令结合使用的算法:

$ decrypt -l
Algorithm       Keysize:         Min   Max (bits)
------------------------------------------
aes                       128   256
arcfour                     8  2048
des                        64    64
3des                      128   192
camellia                  128   256

退出状态

将返回以下退出值:

0

成功完成。

>0

出现错误。

属性

有关下列属性的说明,请参见 attributes(7)

属性类型
属性值
可用性
system/core-os
接口稳定性
Committed(已确定)

另请参见

digest(1)mac(1)pktool(1)getpassphrase(3C)libpkcs11(3LIB)attributes(7)pkcs11_softtoken(7)dd(8)

Kaliski, B., RFC 2898, PKCS #5: Password-Based Cryptography Specification, Version 2.0, September 2000. https://tools.ietf.org/html/rfc2898

https://www.oasis-open.org/committees/pkcs11/

History

The –K and –T options were added in Oracle Solaris 11.0.

The encrypt and decrypt commands, and all other options, were added in Solaris 10 3/05.