C++调用openssl实现DES加密解密cbc模式 zeropadding填充方式 pkcs5padding填充方式 pkcs7padding填充方式

==============================================

    des   cbc  加密 zeropadding填充方式

==============================================

//加密 cbc zeropadding 自己实现
std::string des_cbc_zero_encrypt(const std::string &clearText, const std::string &key)
{
static unsigned char cbc_iv[] = {'j', 'k', 't', '', '', '', '', ''};
//初始化IV向量
std::string strCipherText;
DES_cblock keyEncrypt, ivec;
memset(keyEncrypt, , ); if (key.length() <= )
memcpy(keyEncrypt, key.c_str(), key.length());
else
memcpy(keyEncrypt, key.c_str(), ); DES_key_schedule keySchedule; //密钥表
DES_set_key_unchecked(&keyEncrypt, &keySchedule); //设置密钥,且不检测密钥奇偶性 memcpy(ivec, cbc_iv, sizeof(cbc_iv)); // 循环加密,每8字节一次
const_DES_cblock inputText;
DES_cblock outputText;
std::vector<unsigned char> vecCiphertext;
unsigned char tmp[]; for (int i = ; i < clearText.length() / ; i++)
{
memcpy(inputText, clearText.c_str() + i * , );
DES_ncbc_encrypt(inputText, outputText, , &keySchedule, &ivec, DES_ENCRYPT); //加密
memcpy(tmp, outputText, ); for (int j = ; j < ; j++)
vecCiphertext.push_back(tmp[j]); //重置ivec
memcpy(ivec, outputText, );
} if (clearText.length() % != )
{
int tmp1 = clearText.length() / * ;
int tmp2 = clearText.length() - tmp1;
memset(inputText, , );
memcpy(inputText, clearText.c_str() + tmp1, tmp2);
// 加密函数
DES_ncbc_encrypt(inputText, outputText, , &keySchedule, &ivec, DES_ENCRYPT); //加密
memcpy(tmp, outputText, ); for (int j = ; j < ; j++)
vecCiphertext.push_back(tmp[j]);
} strCipherText.clear();
strCipherText.assign(vecCiphertext.begin(), vecCiphertext.end());
return strCipherText;
}

==============================================

    des   cbc  加密 pkcs5padding填充方式  pkcs7padding跟pkcs5padding是一致的

==============================================

//加密 cbc pkcs5padding 自己实现  //pkcs7padding 跟 pkcs5padding是一样的
std::string des_cbc_pkcs5_encrypt(const std::string &clearText, const std::string &key)
{
static unsigned char cbc_iv[] = {'j', 'k', 't', '', '', '', '', ''};
//初始化IV向量
std::string strCipherText;
DES_cblock keyEncrypt, ivec;
memset(keyEncrypt, , ); if (key.length() <= )
memcpy(keyEncrypt, key.c_str(), key.length());
else
memcpy(keyEncrypt, key.c_str(), ); DES_key_schedule keySchedule; //密钥表
DES_set_key_unchecked(&keyEncrypt, &keySchedule); //设置密钥,且不检测密钥奇偶性 memcpy(ivec, cbc_iv, sizeof(cbc_iv)); // 循环加密,每8字节一次
const_DES_cblock inputText;
DES_cblock outputText;
std::vector<unsigned char> vecCiphertext;
unsigned char tmp[]; for (int i = ; i < clearText.length() / ; i++)
{
memcpy(inputText, clearText.c_str() + i * , );
DES_ncbc_encrypt(inputText, outputText, , &keySchedule, &ivec, DES_ENCRYPT); //加密
memcpy(tmp, outputText, ); for (int j = ; j < ; j++)
vecCiphertext.push_back(tmp[j]); //重置ivec
memcpy(ivec, outputText, );
} if (clearText.length() % != )
{
int tmp1 = clearText.length() / * ;
int tmp2 = clearText.length() - tmp1;
memset(inputText,(-tmp2), );
memcpy(inputText, clearText.c_str() + tmp1, tmp2);
}
else
{
memset(inputText,, );
}
// 加密函数
DES_ncbc_encrypt(inputText, outputText, , &keySchedule, &ivec, DES_ENCRYPT); //加密
memcpy(tmp, outputText, ); for (int j = ; j < ; j++)
vecCiphertext.push_back(tmp[j]); strCipherText.clear();
strCipherText.assign(vecCiphertext.begin(), vecCiphertext.end());
return strCipherText;
}

==============================================

    des   cbc  解密 zeropadding pkcs5padding  pkcs7padding都是一致的

==============================================

//解密 cbc pkcs5padding 自己实现  //zeropadding / pkcs7padding 跟 pkcs5padding是一样的
std::string des_cbc_pkcs5_decrypt(const std::string &cipherText, const std::string &key)
{
static unsigned char cbc_iv[] = {'j', 'k', 't', '', '', '', '', ''};
//初始化IV向量
std::string clearText;
DES_cblock keyEncrypt, ivec;
memset(keyEncrypt, , ); if (key.length() <= )
memcpy(keyEncrypt, key.c_str(), key.length());
else
memcpy(keyEncrypt, key.c_str(), ); DES_key_schedule keySchedule; //密钥表
DES_set_key_unchecked(&keyEncrypt, &keySchedule); //设置密钥,且不检测密钥奇偶性 memcpy(ivec, cbc_iv, sizeof(cbc_iv)); // 循环解密,每8字节一次
const_DES_cblock inputText;
DES_cblock outputText;
std::vector<unsigned char> vecCleartext;
unsigned char tmp[]; for (int i = ; i < cipherText.length() / ; i++)
{
memcpy(inputText, cipherText.c_str() + i * , );
DES_ncbc_encrypt(inputText, outputText, , &keySchedule, &ivec, DES_DECRYPT); //解密
memcpy(tmp, outputText, ); for (int j = ; j < ; j++)
vecCleartext.push_back(tmp[j]); //重置ivec
//memcpy(ivec, outputText, 8); //解密过程不需要用前一块的结果作为下一块的IV
} if (clearText.length() % != )
{
int tmp1 = clearText.length() / * ;
int tmp2 = clearText.length() - tmp1;
memset(inputText,, tmp2);
memcpy(inputText, cipherText.c_str() + tmp1, tmp2);
DES_ncbc_encrypt(inputText, outputText, tmp2, &keySchedule, &ivec, DES_DECRYPT); //解密
memcpy(tmp, outputText, tmp2);
for (int j = ; j < ; j++)
vecCleartext.push_back(tmp[j]);
}
clearText.clear();
clearText.assign(vecCleartext.begin(), vecCleartext.end());
return clearText;
}

附1:DES加解密 cbc模式 的简单讲解 && C++用openssl库来实现的注意事项

附2:C++ 使用openssl库实现 DES 加密——CBC模式 && RSA加密——公加私解——私加公解

上一篇:python 操作excel


下一篇:使用matplotlib.pylab绘制分段函数