C++調用openssl實現DES加密解密cbc模式 zeropadding填充方式 pkcs5padding填充方式 pkcs7padding填充方式

==============================================html

    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;
}

==============================================spa

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

==============================================htm

//加密 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;
}

==============================================blog

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

==============================================ssl

//解密 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庫來實現的注意事項ci

附2:C++ 使用openssl庫實現 DES 加密——CBC模式 && RSA加密——公加私解——私加公解

相關文章
相關標籤/搜索