06.对称加密

6.1 Concepts in Symmetric Cryptography
虽然我们在第1章中简要介绍了对称密钥密码学,但还有一些其他内容我们应该作为本章其余部分的背景材料来讨论。 当然,我们不希望成为密码学的通用教科书。 对于这样的事情,我们推荐其他书籍,如布鲁斯·施奈尔的应用密码学(John Wiley&Sons)。
 
6.1.1 Block Ciphers and Stream Ciphers
Block Ciphers:分组密码传统上是最流行的。 它们通过将数据分解成固定大小的块来进行操作,然后分别对每个块进行加密,其中加密算法是输入的可逆函数。
Stream Ciphers:要加密数据,需要明文,并简单地将其与密钥流异或。 流密码本身不需要填充,虽然它们通常被填充到字节边界,因为实现通常在每个字节级而不是每个位级上进行。
 
 
6.1.2 Basic Block Cipher Modes
a.Electronic Code Book (ECB)
ECB(电子密码本)模式是操作的基本模式,其中密码采用单个明文块并产生单个密文块
b.Cipher Block Chaining (CBC )
CBC(密码块链接)模式实质上是通过将一个块的密文与下一个块的明文异或来解决ECB的字典问题。
 
c.Cipher Feedback (CFB )
CFB(密码反馈)模式是将分组密码转换为流密码的一种方式,尽管在开始加密之前必须接收完整的明文块。
d.Output Feedback (OFB )
OFB(输出反馈)模式是将分组密码转换为流密码的另一种方式
 
 
6.2 Encrypting with the EVP API
函数
功能
 
EVP_bf_cbc
我们可以使用下面的代码来获得对Blowfish-CBC密码对象的引用:
 
OpenSSL_add_all_ciphers
您可以调用所有对称密码
 
OpenSSL_add_all_algorithms
将加载其他种类的加密算法。
 
EVP_add_cipher
如果您想添加一小组密码,然后您可以根据动态信息按名称查找,
 
 
6.2.1 Available Ciphers
6.2.1.1 AES
Table 6-1. Referencing the AES cipher (OpenSSL 0.9.7 only)
Cipher mode
Key/block size
EVP call for cipher object
String for cipher lookup
ECB
128bits
EVP_aes_128_ecb()
aes-128-ecb
CBC
128bits
EVP_aes_192_ecb
aes-192-ecb
ECB
192bits
EVP_aes_192_ecb
aes-192-ecb
CBC
192bits
EVP_aes_192_cbc
aes-192-cbc
ECB
256bits
EVP_aes_256_ecb
aes-256-ecb
CBC
256bits
EVP_aes_256_cbc
aes-256-cbc
 
6.2.1.2 Blowfish
Table 6-2. Referencing the Blowfish cipher
Cipher mode
EVP call for cipher object
String for cipher lookup
ECB
EVP_bf_ecb
bf-ecb
CBC
EVP_bf_cbc
bf-cbc
CFB
EVP_bf_cfb
bf-cfb
OFB
EVP_bf_ofb
bf-ofb
 
6.2.1.3 CAST5
由Carlisle Adams和Stafford Tavares撰写的CAST5算法是另一种具有可变长度密钥和64位块的密码。
Table 6-3. Referencing the CAST5 cipher
Cipher mode
EVP call for cipher object
String for cipher lookup
ECB
EVP_cast_ecb
cast-ecb
CBC
EVP_cast_cbc
cast-cbc
CFB
EVP_cast_cfb
cast-cfb
OFB
EVP_cast_ofb
cast-ofb
 
 
6.2.1.4 DES
数据加密标准DES使用固定的64位块和64位密钥。
 
Table 6-4. Referencing standard DES
Cipher mode
EVP call for cipher object
String for cipher lookup
ECB
EVP_des_ecb
des-ecb
CBC
EVP_des_cbc
des-cbc
CFB
EVP_des_cfb
des-cfb
OFB
EVP_des_ofb
des-ofb
 
6.2.1.5 DESX
DESX是一种抵抗暴力攻击的DES变体。 它使用额外的64位密钥材料来掩盖DES的输入和输出。
Table 6-5. Referencing DESX
Cipher mode
EVP call for cipher object
String for cipher lookup
CBC
EVP_des_cbc
desx
 
6.2.1.6 Triple DES
三重DES通常被写为3DES,是DES最流行的变体,可能是最保守的对称密码,因为DES在过去的四分之一时间里已经看到了广泛的审查。
Table 6-6. Referencing 3DES
Cipher mode
EVP call for cipher object
String for cipher lookup
ECB(3 key)
EVP_des_ede3
des-ede3
CBC(3 key)
EVP_des_ede3_cbc
des-ede3-cbc
CFB(3 key)
EVP_des_ede3_cfb
des-ede3-cfb
OFB(3 key)
EVP_des_ede3_ofb
des-ede3-ofb
ECB(2 key)
EVP_des_ede
des-ede
CBC(2 key)
EVP_des_ede_cbc
des-ede-cbc
CFB(2 key)
EVP_des_ede_cfb
des-ede-cfb
OFB(2 key)
EVP_des_ede_ofb
des-ede-ofb
 
 
6.2.1.7 IDEA
IDEA密码是一个具有128位密钥和64位块的全面分组密码。
Table 6-7. Referencing IDEA
Cipher mode
EVP call for cipher object
String for cipher lookup
ECB
EVP_idea_ecb
idea-ecb
CBC
EVP_idea_cbc
idea-cbc
CFB
EVP_idea_cfb
idea-cfb
OFB
EVP_idea_ofb
idea-ofb
 
 
6.2.1.8 RC2™
RC2算法是RSA实验室的分组密码。
Table 6-8. Referencing RC2
Cipher mode
EVP call for cipher object
String for cipher lookup
ECB
EVP_rc2_ecb
rc2-ecb
CBC
EVP_rc2_cbc
rc2-cbc
CFB
EVP_rc2_cfb
rc2-cfb
OFB
EVP_rc2_ofb
rc2-ofb
 
 
6.2.1.9 RC4™
RC4是具有可变长度密钥的流密码,长度可达256个字节。
Table 6-9 Referencing RC4
Cipher mode
EVP call for cipher object
String for cipher lookup
40 bits
EVP_rc4_40
rc4-40
128 bits
EVP_rc4
rc4
 
6.2.1.10 RC5™
RC5是RSA Security的另一个分组密码。 它的名字是商标,其算法是由一个已发布的专利。
Table 6-10. Referencing RC5
Cipher mode
Key bits
Rounds
EVP call for cipher object
String for cipher lookup
ECB
128
12
EVP_rc5_32_16_12_ecb
rc5-ecb
CBC
128
12
EVP_rc5_32_16_cbc
rc5-cbc
CFB
128
12
EVP_rc5_32_16_12_cfb
rc5-cfb
OFB
128
12
EVP_rc5_32_16_12_ofb
rc5-ofb
 
6.2.2 Initializing Symmetric Ciphers
使用注意点:
a.选择我们将使用的密码类型,包括使用该密码的模式。 我们将传递一个EVP_CIPHER对象到一个初始化例程。
b.通过将其作为字节数组传递给初始化例程来设置用于操作的密钥。
c.指定密码的初始化向量,如果适合该模式。 如果没有特别说明,将使用默认的IV。
d.如果使用“引擎”版本,我们可以指定是否要使用硬件加速(如果可用)。 如果我们这样做,我们必须事先指定一个“引擎”来支持我们的硬件。
 
函数
 
EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *x )
功能
初始化ctx对象
参数
 
ctx
 
 
 
函数
 
int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,unsigned char *key, unsigned char *iv);
int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,unsigned char *key, unsigned char *iv);
功能
数据加密和解密
参数
 
 
ctx
 
type
使用的密码。
key
用于加密或解密的关键。
iv
要使用的初始化向量。
 
随机数
int RAND_bytes(unsigned char *buf, int num);
int RAND_pseudo_bytes(unsigned char *buf, int num);
 
6.2.3 Specifying Key Length and Other Options
EVP_EncryptInit(&ctx, EVP_bf_ecb(), NULL, NULL);
EVP_CIPHER_CTX_set_key_length(&ctx, 8);
EVP_EncryptInit(&ctx, NULL, key, NULL);
EVP_CIPHER_key_length(EVP_bf_ecb());
 
 
函数
 
EVP_CIPHER_CTX_ctrl
功能
设置或查询RC2中的有效密钥强度或回合次数用于RC5。
参数
 
ctx
句柄
type
EVP_CTRL_GET_RC2_KEY_BITS
EVP_CTRL_SET_RC2_KEY_BITS
EVP_CTRL_GET_RC5_ROUNDS
EVP_CTRL_SET_RC5_ROUNDS
 
arg
 
 
6.2.4 Encryption
函数
 
int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int
*outl,unsigned char *in, int inl);
功能
EVP加密
参数
 
 
ctx
context对象
out
加密输出
out1
加密长度
in
加密输入
inl
加密输入长度
 
 
函数
 
int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
 
功能
EVP加密
参数
 
 
ctx
context对象
out
加密输出
out1
加密长度
 
 
6.2.6 Handling UDP Traffic with Counter Mode
函数
 
 
int counter_encrypt_or_decrypt(EVP_CIPHER_CTX *ctx, char *pt, char
*ct, int len, unsigned char *counter);
 
 
功能
只有一个函数counter_encrypt_or_decrypt,因为加密和解密在计数器模式下是相同的。
参数
 
 
ctx
context对象
pt
包含要加密或解密的数据的缓冲区
ct
将包含加密或解密数据的缓冲区。
len
pt长度
counter
 
 
原文地址:https://www.cnblogs.com/aixiaoxiaoyu/p/8215247.html