这几乎不会得到任何答案,但我们还是试试吧。另外,我可能只是做错了一些非常基本的事情……但无论如何:
我必须使用一些 OpenSSL 函数来使用 PHP 进行 AES/RSA 加密/解密(只是告诉这个,所以如果你知道一些节省头部的替代方法,你知道......)和 Mono。由于 OpenSSL.NET 首先不能与 Mono 一起使用,其次它不再获得支持,我被迫将我需要的函数直接从 C OpenSSL 移植到 DLL 并从 C# 调用这些函数,这是一个疼痛本身。
您可能会注意到,加密/解密函数是直接窃取的 AHEM 来自 the wiki. 的
.h 文件是:
#pragma once
#include <comdef.h>
#include <comutil.h>
#include <tchar.h>
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/applink.c>
#define EXPORT __declspec(dllexport)
namespace OpenSSL
{
extern "C" { EXPORT int NumericTest(int a, int b); }
extern "C" { EXPORT BSTR StringTest(); }
extern "C" { EXPORT BSTR StringTestReturn(char *toReturn); }
extern "C" { EXPORT int Encrypt_AES_256_CBC(unsigned char *plaintext,
int plaintext_len,
unsigned char *key,
unsigned char *iv,
unsigned char *ciphertext); }
extern "C" { EXPORT int Decrypt_AES_256_CBC(unsigned char *ciphertext,
int ciphertext_len,
unsigned char *key,
unsigned char *iv,
unsigned char *plaintext); }
extern "C" { EXPORT void HandleErrors(); }
}
.cpp 文件是:
#include "OpenSSL.h"
namespace OpenSSL
{
void HandleErrors()
{
ERR_print_errors_fp(stderr);
abort();
}
void InitOpenSSL()
{
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
OPENSSL_config(NULL);
}
int NumericTest(int a, int b)
{
return a + b;
}
BSTR StringTest()
{
return ::SysAllocString(L"StringTest successful!");
}
BSTR StringTestReturn(char *toReturn)
{
_bstr_t bstrt(toReturn);
bstrt += " (StringTestReturn)";
strcpy_s(toReturn, 256, "StringTestReturn");
return bstrt;
}
int Encrypt_AES_256_CBC(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext)
{
InitOpenSSL();
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
/* Create and initialise the context */
if (!(ctx = EVP_CIPHER_CTX_new())) HandleErrors();
/* Initialise the encryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits */
if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
HandleErrors();
/* Provide the message to be encrypted, and obtain the encrypted output.
* EVP_EncryptUpdate can be called multiple times if necessary
*/
if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
HandleErrors();
ciphertext_len = len;
/* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) HandleErrors();
ciphertext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
int Decrypt_AES_256_CBC(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
unsigned char *iv, unsigned char *plaintext)
{
InitOpenSSL();
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
/* Create and initialise the context */
if (!(ctx = EVP_CIPHER_CTX_new())) HandleErrors();
/* Initialise the decryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits */
if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
HandleErrors();
/* Provide the message to be decrypted, and obtain the plaintext output.
* EVP_DecryptUpdate can be called multiple times if necessary
*/
if (1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
HandleErrors();
plaintext_len = len;
/* Finalise the decryption. Further plaintext bytes may be written at
* this stage.
*/
if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) HandleErrors();
plaintext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
}
}
请注意这些函数是如何经过测试并在 C++ 控制台应用程序上完美运行的
最后但同样重要的是,我一直用于测试目的的 C# 代码:
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace OpenSSLTests
{
[StructLayout(LayoutKind.Sequential)]
class OpenSSL
{
const string OPENSSL_PATH = "(here i putted my dll file path (of course in the program i putted the real path, but not here))";
[DllImport(OPENSSL_PATH, CallingConvention = CallingConvention.Cdecl)]
public static extern int NumericTest(int a, int b);
[DllImport(OPENSSL_PATH, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.BStr)]
public static extern string StringTest();
[DllImport(OPENSSL_PATH, EntryPoint = "StringTestReturn", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.BStr)]
public static extern string StringTestReturn(StringBuilder toReturn);
[DllImport(OPENSSL_PATH, CallingConvention = CallingConvention.Cdecl)]
public static extern int Encrypt_AES_256_CBC(StringBuilder plainText,
int plaintext_len,
StringBuilder key,
StringBuilder iv,
StringBuilder ciphertext);
[DllImport(OPENSSL_PATH, CallingConvention = CallingConvention.Cdecl)]
public static extern int Decrypt_AES_256_CBC(StringBuilder cipherText,
int ciphertext_len,
StringBuilder key,
StringBuilder iv,
StringBuilder plaintext);
static void Main(string[] args)
{
Console.WriteLine("the numeric test result is: " + NumericTest(1, 2));
Console.WriteLine();
Console.WriteLine("the string test result is: " + StringTest());
Console.WriteLine();
StringBuilder test = new StringBuilder(256);
test.Append("test");
Console.WriteLine("test is: " + test);
Console.WriteLine();
Console.WriteLine("Calling StringTestReturn...");
string newTest = StringTestReturn(test);
Console.WriteLine("test is now: " + test);
Console.WriteLine("newTest is: " + newTest);
Console.WriteLine();
StringBuilder plainText = new StringBuilder(1024);
plainText.Append("this is a really crashy test plz halp");
StringBuilder key = new StringBuilder(1024);
StringBuilder IV = new StringBuilder(1024);
key.Append("randomkey12345asdafsEWFAWEFAERGERUGHERUIGHAEIRUGHOAEGHSD");
IV.Append("ivtoUse1248235fdghapeorughèaerjèaeribyvgnèaervgjer0vriono");
StringBuilder ciphredText = new StringBuilder(1024);
int plainTextLength = Encrypt_AES_256_CBC(plainText, plainText.Length, key, IV, ciphredText);
if (plainTextLength != -1)
{
Console.WriteLine("encrypted text length: " + plainTextLength);
Console.WriteLine("the encrypted text content is: " + ciphredText);
}
else
{
Console.WriteLine("error encrypting\n");
}
StringBuilder decryptedText = new StringBuilder(1024);
int decryptedTextLength = -1;
try
{
decryptedTextLength = Decrypt_AES_256_CBC(ciphredText, ciphredText.Length, key, IV, decryptedText);
}
catch (Exception e)
{
Console.WriteLine("error during decryption");
}
if (decryptedTextLength != -1)
{
decryptedText[decryptedTextLength] = '\0';
Console.WriteLine("decrypted text length: " + decryptedTextLength);
try
{
//here it crashes without even giving you the courtesy of printing anything. It just closes
Console.WriteLine("decrypted text is: " + decryptedText);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
else
{
Console.WriteLine("error after decryption");
}
Console.ReadLine();
}
}
}
如您所料,它不应该起作用。 不完全是。或者更好:它不起作用,但奇怪的是它不起作用的方式:与我曾经写过或听过的任何其他程序不同,当程序运行 Visual Studio 时,它只会打开一个控制台窗口,但只是短暂的,然后它自己关闭甚至没有抛出错误。你知道,就像当你学习 C# 并编写 hello world 但忘记在末尾添加 Console.ReadLine() 或在 c++ 中添加 system("PAUSE") 所以它会在一秒钟内关闭?同样的事情。
但是,如果我按 ctrl+F5 运行它,我会得到控制台,输出将是:
the numeric test result is: 3
the string test result is: StringTest successful!
test is: test
Calling StringTestReturn...
test is now: StringTestReturn
newTest is: test (StringTestReturn)
encrypted text length: 48
the encrypted text content is: 9ïÅèSðdC1¦æÌ?
OPENSSL_Uplink(00007FFFFB388000,08): no OPENSSL_Applink
Premere un tasto per continuare . . . (press a key to finish in italian)
所以错误应该是 OPENSSL_Uplink(00007FFFFB388000,08): no OPENSSL_Applink 并且它在我尝试编写解密文本时抛出它。
根据documentation of the website ,如果您忘记将 applink.c 包含到代码中,就会发生这种情况,但正如您所见,它就在 .h 文件中。
那么我应该如何摆脱它呢?包括 applink.c 不起作用。
如果你能让我脱离这永恒的 hell ,我愿意将我的灵魂出卖给你。
问候。
最佳答案
只需将 applink.c 包含在 main.c 中,不要将其包含在任何其他文件中!
关于c# - OpenSSL 代码 native 工作,但作为 DLL 导致 OPENSSL_Uplink : no OPENSSL_Applink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38933943/