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

    des   cbc  加密 zeropadding填充方式

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

//加密 cbc zeropadding 自己实现
std::string des_cbc_zero_encrypt(const std::string &clearText, const std::string &key)
{
    static unsigned char cbc_iv[8] = {'j', 'k', 't', '1', '2', '3', '4', '5'}; 
    //初始化IV向量 
    std::string strCipherText;  
    DES_cblock keyEncrypt, ivec;  
    memset(keyEncrypt, 0, 8);  

    if (key.length() <= 8)   
        memcpy(keyEncrypt, key.c_str(), key.length());  
    else   
        memcpy(keyEncrypt, key.c_str(), 8);  

    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[8];  
  
    for (int i = 0; i < clearText.length() / 8; i++)  
    {  
        memcpy(inputText, clearText.c_str() + i * 8, 8);  
        DES_ncbc_encrypt(inputText, outputText, 8, &keySchedule, &ivec, DES_ENCRYPT);  //加密
        memcpy(tmp, outputText, 8);  
  
        for (int j = 0; j < 8; j++)  
            vecCiphertext.push_back(tmp[j]);

        //重置ivec
        memcpy(ivec, outputText, 8);
    }
  
    if (clearText.length() % 8 != 0)  
    {  
        int tmp1 = clearText.length() / 8 * 8;  
        int tmp2 = clearText.length() - tmp1;  
        memset(inputText, 0, 8);  
        memcpy(inputText, clearText.c_str() + tmp1, tmp2);  
        // 加密函数    
        DES_ncbc_encrypt(inputText, outputText, 8, &keySchedule, &ivec, DES_ENCRYPT);  //加密 
        memcpy(tmp, outputText, 8);  
  
        for (int j = 0; j < 8; 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[8] = {'j', 'k', 't', '1', '2', '3', '4', '5'}; 
    //初始化IV向量 
    std::string strCipherText;  
    DES_cblock keyEncrypt, ivec;  
    memset(keyEncrypt, 0, 8);  

    if (key.length() <= 8)   
        memcpy(keyEncrypt, key.c_str(), key.length());  
    else   
        memcpy(keyEncrypt, key.c_str(), 8);  

    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[8];  
  
    for (int i = 0; i < clearText.length() / 8; i++)  
    {  
        memcpy(inputText, clearText.c_str() + i * 8, 8);  
        DES_ncbc_encrypt(inputText, outputText, 8, &keySchedule, &ivec, DES_ENCRYPT);  //加密
        memcpy(tmp, outputText, 8);  
  
        for (int j = 0; j < 8; j++)  
            vecCiphertext.push_back(tmp[j]);

        //重置ivec
        memcpy(ivec, outputText, 8);
    }
  
    if (clearText.length() % 8 != 0)  
    {  
        int tmp1 = clearText.length() / 8 * 8;  
        int tmp2 = clearText.length() - tmp1;  
        memset(inputText,(8-tmp2), 8);  
        memcpy(inputText, clearText.c_str() + tmp1, tmp2);  
    }
    else
    {
        memset(inputText,8, 8);
    }
    // 加密函数    
    DES_ncbc_encrypt(inputText, outputText, 8, &keySchedule, &ivec, DES_ENCRYPT);  //加密 
    memcpy(tmp, outputText, 8);

    for (int j = 0; j < 8; 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[8] = {'j', 'k', 't', '1', '2', '3', '4', '5'};
    //初始化IV向量 
    std::string clearText;  
    DES_cblock keyEncrypt, ivec;  
    memset(keyEncrypt, 0, 8);  

    if (key.length() <= 8)   
        memcpy(keyEncrypt, key.c_str(), key.length());  
    else   
        memcpy(keyEncrypt, key.c_str(), 8);  

    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[8];  
  
    for (int i = 0; i < cipherText.length() / 8; i++)  
    {  
        memcpy(inputText, cipherText.c_str() + i * 8, 8);  
        DES_ncbc_encrypt(inputText, outputText, 8, &keySchedule, &ivec, DES_DECRYPT);  //解密
        memcpy(tmp, outputText, 8);  
  
        for (int j = 0; j < 8; j++)  
            vecCleartext.push_back(tmp[j]);

        //重置ivec
        //memcpy(ivec, outputText, 8);  //解密过程不需要用前一块的结果作为下一块的IV
    }

    if (clearText.length() % 8 != 0)
    {  
        int tmp1 = clearText.length() / 8 * 8;  
        int tmp2 = clearText.length() - tmp1;  
        memset(inputText,0, 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 = 0; j < 8; 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加密——公加私解——私加公解

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!