UniCode, UFT8
http://www.myexception.cn/cpp/1465313.html
std::string string_To_UTF8(const std::string & str) { int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0); wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴 ZeroMemory(pwBuf, nwLen * 2 + 2); ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen); int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL); char * pBuf = new char[nLen + 1]; ZeroMemory(pBuf, nLen + 1); ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL); std::string retStr(pBuf); delete[]pwBuf; delete[]pBuf; pwBuf = NULL; pBuf = NULL; return retStr; } ////////////////////////////////////////////////////////////////////////// //UTF - 8编码格式字符串 转普通sting类型 std::string UTF8_To_string(const std::string & str) { int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0); wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴 memset(pwBuf, 0, nwLen * 2 + 2); MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen); int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL); char * pBuf = new char[nLen + 1]; memset(pBuf, 0, nLen + 1); WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL); std::string retStr = pBuf; delete[]pBuf; delete[]pwBuf; pBuf = NULL; pwBuf = NULL; return retStr; }
http://blog.csdn.net/educast/article/details/18762427 => good examples
C# Encoding.UTF8.GetBytes(string) => C++ UTF8.GetString(Bytes)
__declspec(dllexport) string DecryptString(string cipherText1) { BYTE DesKeyBlob[] = { 0x08,0x02,0x00,0x00,0x01,0x68,0x00,0x00, // BLOB header 0x05,0x00,0x00,0x00, // key length, in bytes 0xd3,0xa7,0x07,0x72,0x50 // DES key with parity }; HCRYPTPROV hProv; DWORD dwSize; DWORD dwResult; HCRYPTKEY hSessionKey; HCRYPTKEY hExchangeKeyPair = 0; if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)) { dwResult = GetLastError(); if (dwResult == NTE_BAD_KEYSET) { if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) { dwResult = GetLastError(); } } else { dwResult = GetLastError(); } } if (!CryptGenKey(hProv, CALG_RC4, CRYPT_EXPORTABLE, &hSessionKey)) { dwResult = GetLastError(); //MessageBox("Error CryptGenKey() failed.", "Information", MB_OK); //return; } DWORD keySize = sizeof(DesKeyBlob); if (!CryptImportKey( hProv, DesKeyBlob, keySize, 0, 0, &hSessionKey ) ) { printf("Error 0x%08x in importing the Des key \n", GetLastError()); } string cipherText = base64_decode(cipherText1); DWORD length = cipherText.length()+1; unsigned char* plainText = (unsigned char*)malloc(length); memset(plainText, 0, length); memcpy(plainText, cipherText.c_str(), length - 1); if (!CryptDecrypt(hSessionKey, 0, TRUE, 0, plainText, &length)) //abcd1234 { dwResult = GetLastError(); } int wcsLen = ::MultiByteToWideChar(CP_ACP, NULL, (char*)plainText, strlen((char*)plainText), NULL, 0); //分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间 wchar_t* wszString = new wchar_t[wcsLen + 1]; //转换 ::MultiByteToWideChar(CP_UTF8, NULL, (char*)plainText, strlen((char*)plainText), wszString, wcsLen); //最后加上'\0' wszString[wcsLen] = '\0'; int ansiLen = ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL); //同上,分配空间要给'\0'留个空间 char* szAnsi2 = new char[ansiLen + 1]; //转换 //unicode版对应的strlen是wcslen ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), szAnsi2, ansiLen, NULL, NULL); if(CryptDestroyKey(hSessionKey)) { //printf("The public key has been released."); } else { //printf("The public key has not been released."); //return FALSE; } if (CryptReleaseContext(hProv,0)) { //printf("The handle has been released.\n"); } else { //printf("The handle could not be released.\n"); } string plainText2(szAnsi2); return plainText2; }
ANSI and UNICODE C++ CODE =>
.h
// Test.h #include <string.h> #include <windows.h> #include <iostream> using namespace std; #ifdef TEST_EXPORTS #define TEST_API __declspec(dllexport) #else #define TEST_API __declspec(dllimport) #endif #ifdef UNICODE #define Msg MsgW #else #define Msg MsgA #endif // !UNICODE //extern "C" TEST_API int fnTest(void); extern "C" TEST_API int _stdcall Sum(int a1,int a2); extern "C" TEST_API LPSTR _stdcall MsgA( LPSTR str1,LPCSTR str2); extern "C" TEST_API LPWSTR _stdcall MsgW( LPWSTR str1,LPCWSTR str2);
.cpp
// Test.cpp : Defines the entry point for the DLL application. // #include "stdafx.h" #include "Test.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; } //extern "C" TEST_API int fnTest(void) //{ // return 100; //} extern "C" TEST_API int _stdcall Sum(int a1,int a2) { return a1+a2; } extern "C" TEST_API LPSTR _stdcall MsgA( LPSTR str1,LPCSTR str2) { return lstrcpyA(str1,str2); } extern "C" TEST_API LPWSTR _stdcall MsgW( LPWSTR str1,LPCWSTR str2) { return lstrcpyW(str1,str2); }
.def
LIBRARY "Test" DESCRIPTION 'TEST Windows Dynamic Link Library' EXPORTS ; Explicit exports can go here Sum @1 MsgA @2 MsgW @3
C++, C#, PB mixed code
// EncryptDecryptCPP.cpp : 定义 DLL 应用程序的导出函数。 // #include "stdafx.h" #include "EncryptDecrypt.h" #include <wincrypt.h> #include <string> #include <iostream> #include <codecvt> #include <stdio.h> #include <atlconv.h> #include <atlstr.h> using namespace std; #pragma comment(lib, "crypt32.lib") namespace Wrapper { #define KEY_PAIR_SIZE dwSize - 12 //#ifdef __cplusplus extern "C" { //#endif static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; static inline bool is_base64(unsigned char c) { return (isalnum(c) || (c == '+') || (c == '/')); } std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) { std::string ret; int i = 0; int j = 0; unsigned char char_array_3[3]; unsigned char char_array_4[4]; while (in_len--) { char_array_3[i++] = *(bytes_to_encode++); if (i == 3) { char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); char_array_4[3] = char_array_3[2] & 0x3f; for(i = 0; (i <4) ; i++) ret += base64_chars[char_array_4[i]]; i = 0; } } if (i) { for(j = i; j < 3; j++) char_array_3[j] = '\0'; char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); char_array_4[3] = char_array_3[2] & 0x3f; for (j = 0; (j < i + 1); j++) ret += base64_chars[char_array_4[j]]; while((i++ < 3)) ret += '='; } return ret; } std::string base64_decode(std::string const& encoded_string) { int in_len = encoded_string.size(); int i = 0; int j = 0; int in_ = 0; unsigned char char_array_4[4], char_array_3[3]; std::string ret; while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { char_array_4[i++] = encoded_string[in_]; in_++; if (i ==4) { for (i = 0; i <4; i++) char_array_4[i] = base64_chars.find(char_array_4[i]); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (i = 0; (i < 3); i++) ret += char_array_3[i]; i = 0; } } if (i) { for (j = i; j <4; j++) char_array_4[j] = 0; for (j = 0; j <4; j++) char_array_4[j] = base64_chars.find(char_array_4[j]); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; } return ret; } //void UniCodeToAnsi(wchar_t* wszString, char* szAnsi) //{ // wchar_t* wszString = L"abcd1234你我他"; // //预转换,得到所需空间的大小,这次用的函数和上面名字相反 // int ansiLen = ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL); // //同上,分配空间要给'\0'留个空间 // char* szAnsi = new char[ansiLen + 1]; // //转换 // //unicode版对应的strlen是wcslen // ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), szAnsi, ansiLen, NULL, NULL); // //最后加上'\0' // szAnsi[ansiLen] = '\0'; // //} __declspec(dllexport) int Sum(int i, int j) { return i + j; } BOOL GetExportedPLAINTEXTKEYBLOBKey( HCRYPTKEY hKey, LPBYTE *ppbKeyBlob); __declspec(dllexport) void FillString(char* myString, int length) { //check that length is enough for your string //simple test strcpy_s(myString, length, "hello"); } __declspec(dllexport) string EncryptStringAndReturnKeyIV(string plainText, unsigned char* key, int& keySize, string& iv) { DWORD dwResult; HCRYPTPROV hProv; HCRYPTKEY hKey; HCRYPTKEY hSessionKey; HCRYPTKEY hExchangeKeyPair = 0; DWORD cbBlob; BYTE *pbBlob; BYTE *pbExportedKeyBlob = NULL; BYTE *pbEncryptedKey = NULL; DWORD dwSize; unsigned int c; LPBYTE *ppbKeyBlob, *exportedKey; LPDWORD pdwBlobLen = 0; DWORD dwBlobLen; BYTE* pbKeyBlob; dwResult= 0; hProv = 0; hKey = 0; hSessionKey = 0; cbBlob = 0; pbBlob = NULL; if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)) { dwResult = GetLastError(); if (dwResult == NTE_BAD_KEYSET) { if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) { dwResult = GetLastError(); //MessageBox("Error [0x%x]: CryptAcquireContext() failed.", "Information", MB_OK); //return; } } else { dwResult = GetLastError(); //return; } } if (pbBlob) { if (!CryptImportKey(hProv, pbBlob, cbBlob, 0, 0, &hSessionKey)) { dwResult = GetLastError(); //MessageBox("Error [0x%x]: CryptImportKey() failed.", "Information", MB_OK); //return; } } else { if (!CryptGenKey(hProv, CALG_RC4, CRYPT_EXPORTABLE, &hSessionKey)) { dwResult = GetLastError(); //MessageBox("Error CryptGenKey() failed.", "Information", MB_OK); //return; } } unsigned long length = plainText.length() +1; unsigned char * cipherBlock = (unsigned char*)malloc(length); memset(cipherBlock, 0, length); memcpy(cipherBlock, plainText.c_str(), length -1); if (!CryptEncrypt(hSessionKey, 0, TRUE, 0, cipherBlock, &length, length)) { dwResult = GetLastError(); //MessageBox("Error CryptEncrypt() failed.", "Information", MB_OK); //return; } string cipherText((char*)cipherBlock); //*********************************************************** unsigned char * cipherBlock2 = (unsigned char*)malloc(length); memset(cipherBlock2, 0, length); memcpy(cipherBlock2, cipherBlock, length); if (!CryptDecrypt(hSessionKey, 0, TRUE, 0, cipherBlock2, &length)) { dwResult = GetLastError(); //MessageBox("Error CryptDecrypt() failed.", "Information", MB_OK); //return; } //free(cipherBlock); //free(cipherBlock2); if (!CryptExportKey( hSessionKey, hExchangeKeyPair, PLAINTEXTKEYBLOB, 0, NULL, &dwSize)) { ; } pbExportedKeyBlob = new BYTE[dwSize]; if (!CryptExportKey( hSessionKey, hExchangeKeyPair, PLAINTEXTKEYBLOB, 0, pbExportedKeyBlob, &dwSize)) { ; } //-------------------------------------------------------- //Let's remove the first 12 bytes of Blob information pbEncryptedKey = new BYTE [KEY_PAIR_SIZE]; for ( c = 0 ; c < KEY_PAIR_SIZE ; c++ ) { pbEncryptedKey[c] = pbExportedKeyBlob[c+12]; //printf("%02x", pbExportedKeyBlob[c + 12]); } for ( c = 0 ; c < dwSize ; c++ ) { printf("%02x", pbExportedKeyBlob[c]); } //************************************************************ /*if(!CryptExportKey( hSessionKey, NULL, PLAINTEXTKEYBLOB, 0, NULL, &dwBlobLen)) { printf("Error 0x%08x computing BLOB length.\n", GetLastError()); } if(pbKeyBlob = (BYTE*)malloc(dwBlobLen)) { printf("Memory has been allocated for the BLOB. \n"); } else { printf("Out of memory. \n"); } if(!CryptExportKey( hSessionKey, NULL, PLAINTEXTKEYBLOB, 0, pbKeyBlob, &dwBlobLen)) { printf("Error 0x%08x exporting key.\n", GetLastError()); }*/ //------------------------------------------------------------------ //key = pbKeyBlob; /*DWORD count = 0; printf("Printing Key BLOB for verification \n"); for(count=0; count < dwBlobLen;) { printf("%02x",pbKeyBlob[count]); count ++; }*/ keySize = dwSize; //key = (unsigned char*)malloc(keySize); memset(key, 0, keySize + 1); memcpy(key, pbExportedKeyBlob, keySize); // Free resources. /*if(pbKeyBlob) { free(pbKeyBlob); }*/ //pbKeyBlob = (BYTE *)plainKey.c_str(); //CryptImportKey(hProv, pbBlob, cbBlob, 0, 0, &hSessionKey) if (!CryptImportKey( hProv, pbExportedKeyBlob, dwSize, 0, 0, &hSessionKey ) ) { printf("Error 0x%08x in importing the Des key \n", GetLastError()); } //*********************************************************** unsigned char* cipherBlock3 = (unsigned char*)malloc(length); memset(cipherBlock3, 0, length); memcpy(cipherBlock3, cipherBlock, length); if (!CryptDecrypt(hSessionKey, 0, TRUE, 0, cipherBlock3, &length)) //abcd1234 { dwResult = GetLastError(); //MessageBox("Error CryptDecrypt() failed.", "Information", MB_OK); //return; } //key = "abcd"; //iv = "1234"; if(CryptDestroyKey(hSessionKey)) { printf("The public key has been released."); } else { printf("The public key has not been released."); return FALSE; } if (CryptReleaseContext(hProv,0)) { printf("The handle has been released.\n"); } else { printf("The handle could not be released.\n"); } return cipherText; } __declspec(dllexport) string DecryptStringWithReturnKeyIV(string cipherText, unsigned char* key, int keySize, string iv) { DWORD dwResult; HCRYPTPROV hProv; HCRYPTKEY hKey; HCRYPTKEY hSessionKey; HCRYPTKEY hExchangeKeyPair = 0; DWORD cbBlob; BYTE *pbBlob; BYTE *pbExportedKeyBlob = NULL; BYTE *pbEncryptedKey = NULL; DWORD dwSize; unsigned int c; LPBYTE *ppbKeyBlob, *exportedKey; LPDWORD pdwBlobLen = 0; DWORD dwBlobLen; BYTE* pbKeyBlob; dwResult= 0; hProv = 0; hKey = 0; hSessionKey = 0; cbBlob = 0; pbBlob = NULL; if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)) { } /*pbKeyBlob = (unsigned char*)malloc(keySize); memset(pbKeyBlob, 0, keySize); memcpy(pbKeyBlob, key, keySize); */ //int x = key.length(); //pbKeyBlob = key; if (!CryptImportKey( hProv, key, keySize, 0, 0, &hSessionKey ) ) { printf("Error 0x%08x in importing the Des key \n", GetLastError()); } unsigned long length = 9;//cipherText.length(); unsigned char* plainText = (unsigned char*)malloc(length); memset(plainText, 0, length); memcpy(plainText, cipherText.c_str(), length); if (!CryptDecrypt(hSessionKey, 0, TRUE, 0, plainText, &length)) //abcd1234 { dwResult = GetLastError(); //MessageBox("Error CryptDecrypt() failed.", "Information", MB_OK); //return; } if(CryptDestroyKey(hSessionKey)) { printf("The public key has been released."); } else { printf("The public key has not been released."); return FALSE; } if (CryptReleaseContext(hProv,0)) { printf("The handle has been released.\n"); } else { printf("The handle could not be released.\n"); } return string((char*)plainText); } BOOL GetExportedPLAINTEXTKEYBLOBKey( HCRYPTKEY hKey, LPBYTE *ppbKeyBlob) { HCRYPTPROV hProv = NULL; DWORD dwBlobLen; BYTE* pbKeyBlob; // For the purpose of this example, you can export the key and // print it to verify that the plain text key created above is // the key that was imported into the CSP. // Call CryptExportKey once to determine the length of the key. // Allocate memory for the BLOB, and call CryptExportKey again // to retrieve the key from the CSP key container. if(!CryptExportKey( hKey, NULL, PLAINTEXTKEYBLOB, 0, NULL, &dwBlobLen)) { printf("Error 0x%08x computing BLOB length.\n", GetLastError()); return 1; } if(pbKeyBlob = (BYTE*)malloc(dwBlobLen)) { printf("Memory has been allocated for the BLOB. \n"); } else { printf("Out of memory. \n"); return 1; } if(!CryptExportKey( hKey, NULL, PLAINTEXTKEYBLOB, 0, pbKeyBlob, &dwBlobLen)) { printf("Error 0x%08x exporting key.\n", GetLastError()); return 1; } DWORD count = 0; printf("Printing Key BLOB for verification \n"); for(count=0; count < dwBlobLen;) { printf("%02x",pbKeyBlob[count]); count ++; } // Free resources. if(pbKeyBlob) { free(pbKeyBlob); } } BOOL GetImportedPLAINTEXTKEYBLOBKey( HCRYPTKEY hKey, LPBYTE *ppbKeyBlob) { HCRYPTPROV hProv = NULL; DWORD dwBlobLen; BYTE* pbKeyBlob; if (!CryptImportKey( hProv, *ppbKeyBlob, sizeof(ppbKeyBlob), 0, CRYPT_EXPORTABLE, &hKey ) ) { printf("Error 0x%08x in importing the Des key \n", GetLastError()); } return 1; } __declspec(dllexport) string EncryptString(string plainText) { BYTE DesKeyBlob[] = { 0x08,0x02,0x00,0x00,0x01,0x68,0x00,0x00, // BLOB header 0x05,0x00,0x00,0x00, // key length, in bytes 0xd3,0xa7,0x07,0x72,0x50 // DES key with parity }; HCRYPTPROV hProv; DWORD dwSize; DWORD dwResult; HCRYPTKEY hSessionKey; HCRYPTKEY hExchangeKeyPair = 0; if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)) { dwResult = GetLastError(); if (dwResult == NTE_BAD_KEYSET) { if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) { dwResult = GetLastError(); } } else { dwResult = GetLastError(); } } if (!CryptGenKey(hProv, CALG_RC4, CRYPT_EXPORTABLE, &hSessionKey)) { dwResult = GetLastError(); //MessageBox("Error CryptGenKey() failed.", "Information", MB_OK); //return; } DWORD keySize = sizeof(DesKeyBlob); if (!CryptImportKey( hProv, DesKeyBlob, keySize, 0, 0, &hSessionKey ) ) { printf("Error 0x%08x in importing the Des key \n", GetLastError()); } unsigned long length = plainText.length() + 1; unsigned char * cipherBlock = (unsigned char*)malloc(length); memset(cipherBlock, 0, length); memcpy(cipherBlock, plainText.c_str(), length - 1); if (!CryptEncrypt(hSessionKey, 0, TRUE, 0, cipherBlock, &length, length)) { dwResult = GetLastError(); } if(CryptDestroyKey(hSessionKey)) { //printf("The public key has been released."); } else { //printf("The public key has not been released."); //return FALSE; } if (CryptReleaseContext(hProv,0)) { //printf("The handle has been released.\n"); } else { //printf("The handle could not be released.\n"); } string cipherText((char*)cipherBlock); return cipherText; } __declspec(dllexport) void EncryptStringForCS(const char* plainText, int realSize, char* cipherText, int maxLength) { BYTE DesKeyBlob[] = { 0x08,0x02,0x00,0x00,0x01,0x68,0x00,0x00, // BLOB header 0x05,0x00,0x00,0x00, // key length, in bytes 0xd3,0xa7,0x07,0x72,0x50 // DES key with parity }; HCRYPTPROV hProv; DWORD dwSize; DWORD dwResult; HCRYPTKEY hSessionKey; HCRYPTKEY hExchangeKeyPair = 0; if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)) { dwResult = GetLastError(); if (dwResult == NTE_BAD_KEYSET) { if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) { dwResult = GetLastError(); } } else { dwResult = GetLastError(); } } if (!CryptGenKey(hProv, CALG_RC4, CRYPT_EXPORTABLE, &hSessionKey)) { dwResult = GetLastError(); //MessageBox("Error CryptGenKey() failed.", "Information", MB_OK); //return; } DWORD keySize = sizeof(DesKeyBlob); if (!CryptImportKey( hProv, DesKeyBlob, keySize, 0, 0, &hSessionKey ) ) { printf("Error 0x%08x in importing the Des key \n", GetLastError()); } unsigned long length = maxLength; unsigned char * cipherBlock = (unsigned char*)malloc(length); memset(cipherBlock, 0, length); memcpy(cipherBlock, plainText, realSize); length = realSize; if (!CryptEncrypt(hSessionKey, 0, TRUE, 0, cipherBlock, &length, length)) { dwResult = GetLastError(); } strcpy_s(cipherText, maxLength, (char*)cipherBlock); free(cipherBlock); if(CryptDestroyKey(hSessionKey)) { //printf("The public key has been released."); } else { //printf("The public key has not been released."); //return FALSE; } if (CryptReleaseContext(hProv,0)) { //printf("The handle has been released.\n"); } else { //printf("The handle could not be released.\n"); } } //--------------------------------------------------------------------------------------------------- LPWSTR __declspec(dllexport) __stdcall DecryptStringW(LPCWSTR cipherText1, LPWSTR plainText3, int cipherSize) { //return lstrcpyW(plainText3, cipherText1); BYTE DesKeyBlob[] = { 0x08,0x02,0x00,0x00,0x01,0x68,0x00,0x00, // BLOB header 0x05,0x00,0x00,0x00, // key length, in bytes 0xd3,0xa7,0x07,0x72,0x50 // DES key with parity }; HCRYPTPROV hProv; DWORD dwSize; DWORD dwResult; HCRYPTKEY hSessionKey; HCRYPTKEY hExchangeKeyPair = 0; if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)) { dwResult = GetLastError(); if (dwResult == NTE_BAD_KEYSET) { if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) { dwResult = GetLastError(); } } else { dwResult = GetLastError(); } } if (!CryptGenKey(hProv, CALG_RC4, CRYPT_EXPORTABLE, &hSessionKey)) { dwResult = GetLastError(); //MessageBox("Error CryptGenKey() failed.", "Information", MB_OK); //return; } DWORD keySize = sizeof(DesKeyBlob); if (!CryptImportKey( hProv, DesKeyBlob, keySize, 0, 0, &hSessionKey ) ) { printf("Error 0x%08x in importing the Des key \n", GetLastError()); } //LPWSTR wszString = cipherText1; //L"abcd1234你我他"; //预转换,得到所需空间的大小,这次用的函数和上面名字相反 int ansiLen = ::WideCharToMultiByte(CP_UTF8, NULL, cipherText1, wcslen(cipherText1), NULL, 0, NULL, NULL); //同上,分配空间要给'\0'留个空间 char* szAnsi = new char[ansiLen + 1]; //转换 //unicode版对应的strlen是wcslen ::WideCharToMultiByte(CP_ACP, NULL, cipherText1, wcslen(cipherText1), szAnsi, ansiLen, NULL, NULL); //最后加上'\0' szAnsi[ansiLen] = '\0'; string cipherText = base64_decode(szAnsi); DWORD length = cipherText.length()+1; unsigned char* plainText = (unsigned char*)malloc(length); memset(plainText, 0, length); memcpy(plainText, cipherText.c_str(), length - 1); if (!CryptDecrypt(hSessionKey, 0, TRUE, 0, plainText, &length)) //abcd1234 { dwResult = GetLastError(); } if(CryptDestroyKey(hSessionKey)) { //printf("The public key has been released."); } else { //printf("The public key has not been released."); //return FALSE; } if (CryptReleaseContext(hProv,0)) { //printf("The handle has been released.\n"); } else { //printf("The handle could not be released.\n"); } int wcsLen = ::MultiByteToWideChar(CP_ACP, NULL, (char*)plainText, strlen((char*)plainText), NULL, 0); //分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间 //LPWSTR wszString = new WCHAR[wcsLen + 1]; //转换 ::MultiByteToWideChar(CP_UTF8, NULL, (char*)plainText, strlen((char*)plainText), plainText3, wcsLen); //最后加上'\0' plainText3[wcsLen] = '\0'; if (cipherSize > wcsLen ) { for( int k = 1; k < cipherSize - wcsLen; k++) { plainText3[wcsLen + k] = '\0'; } } delete szAnsi; free(plainText); return plainText3; } LPSTR __declspec(dllexport) __stdcall DecryptStringA(LPCSTR cipherText1, LPSTR plainText3, int cipherSize) { BYTE DesKeyBlob[] = { 0x08,0x02,0x00,0x00,0x01,0x68,0x00,0x00, // BLOB header 0x05,0x00,0x00,0x00, // key length, in bytes 0xd3,0xa7,0x07,0x72,0x50 // DES key with parity }; HCRYPTPROV hProv; DWORD dwSize; DWORD dwResult; HCRYPTKEY hSessionKey; HCRYPTKEY hExchangeKeyPair = 0; if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)) { dwResult = GetLastError(); if (dwResult == NTE_BAD_KEYSET) { if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) { dwResult = GetLastError(); } } else { dwResult = GetLastError(); } } if (!CryptGenKey(hProv, CALG_RC4, CRYPT_EXPORTABLE, &hSessionKey)) { dwResult = GetLastError(); //MessageBox("Error CryptGenKey() failed.", "Information", MB_OK); //return; } DWORD keySize = sizeof(DesKeyBlob); if (!CryptImportKey( hProv, DesKeyBlob, keySize, 0, 0, &hSessionKey ) ) { printf("Error 0x%08x in importing the Des key \n", GetLastError()); } string cipherText = base64_decode(cipherText1); DWORD length = cipherText.length()+1; unsigned char* plainText = (unsigned char*)malloc(length); memset(plainText, 0, length); memcpy(plainText, cipherText.c_str(), length - 1); if (!CryptDecrypt(hSessionKey, 0, TRUE, 0, plainText, &length)) //abcd1234 { dwResult = GetLastError(); } int wcsLen = ::MultiByteToWideChar(CP_ACP, NULL, (char*)plainText, strlen((char*)plainText), NULL, 0); //分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间 wchar_t* wszString = new wchar_t[wcsLen + 1]; //转换 ::MultiByteToWideChar(CP_UTF8, NULL, (char*)plainText, strlen((char*)plainText), wszString, wcsLen); //最后加上'\0' wszString[wcsLen] = '\0'; if (cipherSize > wcsLen ) { for( int k = 1; k < cipherSize - wcsLen; k++) { wszString[wcsLen + k] = '\0'; } } int ansiLen = ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL); //同上,分配空间要给'\0'留个空间 char* szAnsi2 = new char[ansiLen + 1]; //转换 //unicode版对应的strlen是wcslen ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), plainText3, ansiLen, NULL, NULL); if(CryptDestroyKey(hSessionKey)) { //printf("The public key has been released."); } else { //printf("The public key has not been released."); //return FALSE; } if (CryptReleaseContext(hProv,0)) { //printf("The handle has been released.\n"); } else { //printf("The handle could not be released.\n"); } string plainText2(szAnsi2); free(plainText); delete wszString; delete szAnsi2; return plainText3; } __declspec(dllexport) void DecryptStringForCS(const char* cipherText, int realSize, char* plainText, int maxLength) { BYTE DesKeyBlob[] = { 0x08,0x02,0x00,0x00,0x01,0x68,0x00,0x00, // BLOB header 0x05,0x00,0x00,0x00, // key length, in bytes 0xd3,0xa7,0x07,0x72,0x50 // DES key with parity }; HCRYPTPROV hProv; DWORD dwSize; DWORD dwResult; HCRYPTKEY hSessionKey; HCRYPTKEY hExchangeKeyPair = 0; if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)) { dwResult = GetLastError(); if (dwResult == NTE_BAD_KEYSET) { if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) { dwResult = GetLastError(); } } else { dwResult = GetLastError(); } } if (!CryptGenKey(hProv, CALG_RC4, CRYPT_EXPORTABLE, &hSessionKey)) { dwResult = GetLastError(); //MessageBox("Error CryptGenKey() failed.", "Information", MB_OK); //return; } DWORD keySize = sizeof(DesKeyBlob); if (!CryptImportKey( hProv, DesKeyBlob, keySize, 0, 0, &hSessionKey ) ) { printf("Error 0x%08x in importing the Des key \n", GetLastError()); } DWORD length = maxLength; unsigned char* plainBlock = (unsigned char*)malloc(length); memset(plainBlock, 0, length); memcpy(plainBlock, cipherText, realSize); length = realSize; if (!CryptDecrypt(hSessionKey, 0, TRUE, 0, plainBlock, &length)) //abcd1234 { dwResult = GetLastError(); } strcpy_s(plainText, maxLength, (char*)plainBlock); free(plainBlock); if(CryptDestroyKey(hSessionKey)) { //printf("The public key has been released."); } else { //printf("The public key has not been released."); //return FALSE; } if (CryptReleaseContext(hProv,0)) { //printf("The handle has been released.\n"); } else { //printf("The handle could not be released.\n"); } } __declspec(dllexport) void KeyInformationSeparation(string keyCompany, string& company, string& product, string& validateDate, string& version, string& x) { std::string delimiter = ","; size_t pos = 0; std::string token; pos = keyCompany.find(delimiter); company = keyCompany.substr(0, pos); keyCompany.erase(0, pos + delimiter.length()); pos = keyCompany.find(delimiter); product = keyCompany.substr(0, pos); keyCompany.erase(0, pos + delimiter.length()); pos = keyCompany.find(delimiter); validateDate = keyCompany.substr(0, pos); keyCompany.erase(0, pos + delimiter.length()); pos = keyCompany.find(delimiter); version = keyCompany.substr(0, pos); keyCompany.erase(0, pos + delimiter.length()); pos = keyCompany.find(delimiter); x = keyCompany.substr(0, pos); keyCompany.erase(0, pos + delimiter.length()); } __declspec(dllexport) void EncryptStringForCSharp(BYTE *plainText, int realSize, BYTE* cipherText, int maxLength) { BYTE DesKeyBlob[] = { 0x08,0x02,0x00,0x00,0x01,0x68,0x00,0x00, // BLOB header 0x05,0x00,0x00,0x00, // key length, in bytes 0xd3,0xa7,0x07,0x72,0x50 // DES key with parity }; HCRYPTPROV hProv; DWORD dwSize; DWORD dwResult; HCRYPTKEY hSessionKey; HCRYPTKEY hExchangeKeyPair = 0; if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)) { dwResult = GetLastError(); if (dwResult == NTE_BAD_KEYSET) { if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) { dwResult = GetLastError(); } } else { dwResult = GetLastError(); } } if (!CryptGenKey(hProv, CALG_RC4, CRYPT_EXPORTABLE, &hSessionKey)) { dwResult = GetLastError(); //MessageBox("Error CryptGenKey() failed.", "Information", MB_OK); //return; } DWORD keySize = sizeof(DesKeyBlob); if (!CryptImportKey( hProv, DesKeyBlob, keySize, 0, 0, &hSessionKey ) ) { printf("Error 0x%08x in importing the Des key \n", GetLastError()); } unsigned long length = maxLength; BYTE * cipherBlock = (BYTE*)malloc(length); memset(cipherBlock, 0, length); memcpy(cipherBlock, plainText, realSize); length = realSize; if (!CryptEncrypt(hSessionKey, 0, TRUE, 0, cipherBlock, &length, length)) { dwResult = GetLastError(); } //strcpy_s(cipherText, maxLength, (char*)cipherBlock); for(int i = 0; i < realSize; i++) { cipherText[i] = cipherBlock[i]; } free(cipherBlock); if(CryptDestroyKey(hSessionKey)) { //printf("The public key has been released."); } else { //printf("The public key has not been released."); //return FALSE; } if (CryptReleaseContext(hProv,0)) { //printf("The handle has been released.\n"); } else { //printf("The handle could not be released.\n"); } } __declspec(dllexport) void DecryptStringForCSharp(BYTE *cipherText, int realSize, BYTE* plainText, int maxLength) { BYTE DesKeyBlob[] = { 0x08,0x02,0x00,0x00,0x01,0x68,0x00,0x00, // BLOB header 0x05,0x00,0x00,0x00, // key length, in bytes 0xd3,0xa7,0x07,0x72,0x50 // DES key with parity }; HCRYPTPROV hProv; DWORD dwSize; DWORD dwResult; HCRYPTKEY hSessionKey; HCRYPTKEY hExchangeKeyPair = 0; if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)) { dwResult = GetLastError(); if (dwResult == NTE_BAD_KEYSET) { if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) { dwResult = GetLastError(); } } else { dwResult = GetLastError(); } } if (!CryptGenKey(hProv, CALG_RC4, CRYPT_EXPORTABLE, &hSessionKey)) { dwResult = GetLastError(); //MessageBox("Error CryptGenKey() failed.", "Information", MB_OK); //return; } DWORD keySize = sizeof(DesKeyBlob); if (!CryptImportKey( hProv, DesKeyBlob, keySize, 0, 0, &hSessionKey ) ) { printf("Error 0x%08x in importing the Des key \n", GetLastError()); } DWORD length = maxLength; BYTE* plainBlock = (BYTE*)malloc(length); memset(plainBlock, 0, length); memcpy(plainBlock, cipherText, realSize); length = realSize; if (!CryptDecrypt(hSessionKey, 0, TRUE, 0, plainBlock, &length)) //abcd1234 { dwResult = GetLastError(); } //strcpy_s(plainText, maxLength, plainBlock); for(int i = 0; i < realSize; i++) { plainText[i]= plainBlock[i]; } plainText[realSize] = 0; free(plainBlock); if(CryptDestroyKey(hSessionKey)) { //printf("The public key has been released."); } else { //printf("The public key has not been released."); //return FALSE; } if (CryptReleaseContext(hProv,0)) { //printf("The handle has been released.\n"); } else { //printf("The handle could not be released.\n"); } } wstring s2ws(const std::string& str) { typedef std::codecvt_utf8<wchar_t> convert_typeX; std::wstring_convert<convert_typeX, wchar_t> converterX; return converterX.from_bytes(str); } string ws2s(const std::wstring& wstr) { typedef std::codecvt_utf8<wchar_t> convert_typeX; std::wstring_convert<convert_typeX, wchar_t> converterX; return converterX.to_bytes(wstr); } std::string utf8_encode(const std::wstring &wstr) { int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL); std::string strTo( size_needed, 0 ); WideCharToMultiByte (CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); return strTo; } //__declspec(dllexport) std::string base64_decodeA(std::string const& encoded_string) // { // string cipherText = base64_decode(encoded_string); // string plainText = DecryptString(cipherText); // // string a = "123456"; // // int len = sizeof(a); // // std::wstring result( a.begin(), a.end() ); // // int wLen = sizeof(result); // // string aaa = utf8_encode(result); // // //int x = sprintf(buffer,"%S", plainText.c_str()); // // //const CStringA utf8 = CW2A(buffer, CP_UTF8); // // // return plainText; // } LPSTR __declspec(dllexport) __stdcall MessageA(LPSTR cipherText1, LPCSTR plainText3) { return lstrcpyA(cipherText1, plainText3); } LPWSTR __declspec(dllexport) __stdcall MessageW(LPWSTR cipherText1, LPCWSTR plainText3) { return lstrcpyW(cipherText1, plainText3); } #ifdef __cplusplus } #endif }
C# C++ 字符串传递
http://blog.csdn.net/wangweitingaabbcc/article/details/7663949
Conversion between Unicode UTF-16 and UTF-8 in C++/Win32
http://blogs.msmvps.com/gdicanio/2010/01/04/conversion-between-unicode-utf-16-and-utf-8-in-c-win32/