我有一個包含 AES 加密的 c++ dll,該函數(shù)有兩個輸入:字符串:使用 AES 加密字符串:加密輸出這是外部函數(shù):extern "C" __declspec(dllexport) void aes_crypter(const char* string_in , wchar_t* string_out);void aes_crypter(const char* string_in , wchar_t* string_out){ std::string X_KEY = "abcdefghijklmnopqrstuvwxyz123412"; std::string X_PSS = "612345601234512"; //////// convert input text to LPCWSTR const wchar_t* inputtext = convertCharArrayToLPCWSTR(string_in); //////// convert LPCWSTR to string [const to string result wrong ] std::wstring ws(inputtext); std::string str(ws.begin(), ws.end()); //////// Encrypt auto encr = encrypt(str, key, X_PSS); //////// Convert and Return to Output String std::wstring widestr = std::wstring(encr.begin(), encr.end()); const wchar_t* output_crypt = widestr.c_str(); /// CONVERT STD TO WCHAR swprintf(string_out, 4096,output_crypt);}代碼工作正常,問題出在我的 C# 應(yīng)用程序中:這是導(dǎo)入功能代碼:[DllImport("simpleAES.dll",CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]private static extern void aes_crypter(string string_in, StringBuilder string_out);問題 :如果我將 CharSet 設(shè)置為 ANSI,輸入字符串工作正常,在 C++ 中返回正確但輸出字符串僅返回一個字符錯誤!如果我將 CharSet 設(shè)置為 UNICODE,則輸入字符串返回錯誤且只有一個字符。但是輸出字符串返回正確!我哪里出錯了?我該如何解決?
無法將加密文本從 C# 傳遞到 C++ 和相反
慕運維8079593
2021-06-03 18:09:43