c#加密(dec,cbc):
private void button1_Click(object sender, EventArgs e)
{
string input = "d";
byte[] key = new byte[] { 0x1d, 0x80, 0x6a, 0x0b, 0xc2, 0x04, 0xbf, 0x0d }; //密钥
byte[] iv = new byte[] { 0xce, 0xa8, 0xd9, 0x61, 0x16, 0x7f, 0x6a, 0xa9 }; //初始化向量
DES des_cbc = new DESCryptoServiceProvider();
byte[] inbuff = ASCIIEncoding.ASCII.GetBytes(input);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des_cbc.CreateEncryptor(key, iv), CryptoStreamMode.Write);
cs.Write(inbuff, 0, inbuff.Length);
cs.Close();
byte[] ciphertext = ms.ToArray();
//the result: ciphertext={ 0x18, 0x12, 0xe0, 0x4e, 0x83, 0x31, 0x25, 0xec};
}
c++ openssl解密
#include <iostream>
using namespace std;
#include <openssl/evp.h>
#pragma comment(lib, "libeay32.lib")
int main()
{
unsigned char key[8] = { 0x1d, 0x80, 0x6a, 0x0b, 0xc2, 0x04, 0xbf, 0x0d }; //密钥
unsigned char iv[8] = { 0xce, 0xa8, 0xd9, 0x61, 0x16, 0x7f, 0x6a, 0xa9 }; //初始化向量
unsigned char ciphertext[8]={ 0x18, 0x12, 0xe0, 0x4e, 0x83, 0x31, 0x25, 0xec};
int input_length = 8; //密文长度
EVP_CIPHER_CTX de;
EVP_CIPHER_CTX_init(&de);
EVP_DecryptInit_ex(&de, EVP_des_cbc(), NULL, key, iv);
EVP_CIPHER_CTX_set_padding(&de, 8);
int plaintext_len = input_length+8;
unsigned char* cbc_out = new unsigned char[plaintext_len];
memset(cbc_out,0,plaintext_len);
//output_length and output_length1 need to be different.
int output_length1 = 0;
int ret1 = EVP_DecryptUpdate(&de, cbc_out, &plaintext_len, ciphertext, input_length);
if(!EVP_DecryptFinal_ex(&de, cbc_out+plaintext_len, &output_length1))
{
printf("ERROR in EVP_DecryptFinal_ex\n");
return 1;
}
//the result:cbc_out={'d', 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x00},output_length1=1
delete[] cbc_out;
EVP_CIPHER_CTX_cleanup(&de);
}
参考:http://blogs.inxsasia.com/hiran/2010/07/des-with-openssl-vs-descryptoserviceprovider-in-net.html
http://stackoverflow.com/questions/5727646/what-is-the-length-parameter-of-aes-evp-decrypt
安装开源openssl:http://blog.youkuaiyun.com/dragoo1/article/details/22398473
-------------------------------------------------------------------------------------------------------------------------------
下面内容只是参考:
1.c++ openssl 实现c#加密相同结果
#include <iostream>
using namespace std;
#include <openssl/evp.h>
#pragma comment(lib, "libeay32.lib")
int main()
{
unsigned char key[8] = { 29, 128, 106, 11, 194, 4, 191, 13 };
unsigned char iv[8] = { 206, 168, 217, 97, 22, 127, 106, 169};
static unsigned char cbc_data[40]={
'd',0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
unsigned char cbc_out[40];
memset(cbc_out,0,40);
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_EncryptInit_ex(&ctx, EVP_des_cbc(), NULL, key, iv);
EVP_CIPHER_CTX_set_padding(&ctx, 8);
//output_length and output_length1 need to be different.
int output_length = sizeof(cbc_data);
int input_length = 1;
int output_length1 = 0;
EVP_EncryptUpdate(&ctx, cbc_out, &output_length, cbc_data, input_length);
EVP_EncryptFinal_ex(&ctx, cbc_out+output_length, &output_length1);
output_length += output_length1;
}
2.vc++解密:
#include <stdio.h>
#include <windows.h>
#include <wincrypt.h>
typedef struct
{
BLOBHEADER header;
DWORD cbKeySize;
BYTE rgbKeyData[8];
}KeyBlob;
int main(int argc, char* argv[])
{
unsigned char key[8] = { 0x1d, 0x80, 0x6a, 0x0b, 0xc2, 0x04, 0xbf, 0x0d }; //密钥
unsigned char iv[8] = { 0xce, 0xa8, 0xd9, 0x61, 0x16, 0x7f, 0x6a, 0xa9 }; //初始化向量
unsigned char ciphertext[8]={ 0x18, 0x12, 0xe0, 0x4e, 0x83, 0x31, 0x25, 0xec};
DWORD length = 8; //密文长度
// init
HCRYPTPROV hProv = NULL;
HCRYPTKEY hSessionKey = NULL;
DWORD dwDataLen = 0;
KeyBlob blob;
blob.header.bType = PLAINTEXTKEYBLOB;
blob.header.bVersion = CUR_BLOB_VERSION;
blob.header.reserved = 0;
blob.header.aiKeyAlg = CALG_DES;
blob.cbKeySize = 8;
memcpy(blob.rgbKeyData, key, 8);
BYTE IV[9] = {0};
memcpy( IV, iv, 8 );
// start
CryptAcquireContext(&hProv,NULL, MS_DEF_PROV, PROV_RSA_FULL,0);
CryptImportKey(hProv, (BYTE*)&blob, sizeof(blob), 0, 0, &hSessionKey);
CryptSetKeyParam(hSessionKey, KP_IV, (BYTE*)IV, 0);
// Do
BOOL ret = CryptDecrypt(hSessionKey,NULL,TRUE,0,(BYTE*)ciphertext,&length);
CryptDestroyKey(hSessionKey);
CryptReleaseContext(hProv, 0);
return 0;
}
参考:http://bbs.youkuaiyun.com/topics/390566435