当前位置 : 主页 > 网络编程 > net编程 >

c#DESCryptoServiceProvider加解密对接openssl,CBC模式

来源:互联网 收集:自由互联 发布时间:2023-09-03
c++代码//加密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 keyE
c++代码
//加密
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;
}

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

//解密 cbc pkcs5padding 自己实现  //zeropadding / pkcs7padding 跟 pkcs5padding是一样的 
//Base64Decode先将数据解密
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;
}

//使用base64
std::string Base64Decode(const std::string &input, bool with_new_line)
{
	BIO *b64 = nullptr;
	BIO *bmem = nullptr;
	std::string ret = "";

	b64 = BIO_new(BIO_f_base64());
	if (!with_new_line) {
		BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
	}
	bmem = BIO_new_mem_buf(input.c_str(), input.length());
	bmem = BIO_push(b64, bmem);

	char *buffer = (char *)malloc(input.length());
	memset(buffer, 0, input.length());

	int len = BIO_read(bmem, buffer, input.length());
	if (len > 0) {
		ret = std::string(buffer, len);
	}
	BIO_free_all(bmem);
	free(buffer);
	return ret;
}
搬运https://www.cnblogs.com/azbane/p/10179660.html
C#
        public static string DESDecrypt(string data, string key, string iv)
        {
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv);
            byte[] byEnc;
            try
            {
                byEnc = Convert.FromBase64String(data);
                DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
                MemoryStream ms = new MemoryStream(byEnc);
                CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
                StreamReader sr = new StreamReader(cst);
                return sr.ReadToEnd();
            }
            catch
            {
                return null;
            }
        }
        
        public static string DESEncrypt(string data, string key, string iv)
        {
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv);

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            MemoryStream ms = new MemoryStream();
            CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);

            StreamWriter sw = new StreamWriter(cst);
            sw.Write(data);
            sw.Flush();
            cst.FlushFinalBlock();
            sw.Flush();
            return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
        }
上一篇:C#打开文本文件避免乱码
下一篇:没有了
网友评论