c++ 调用c#进行RSA加解密

#include 
#include 
#include 
#include
#include  
using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Security::Cryptography;
using namespace msclr::interop;
using namespace  std;

ref class SomeClass
{
public:
	static String^ RSADecrypt(String^ privatekey, String^ content)
	{

		RSACryptoServiceProvider^ rsa = gcnew RSACryptoServiceProvider();
		rsa->FromXmlString(privatekey);
		rsa->Decrypt(Convert::FromBase64String(content), false);
		UTF8Encoding^ utf8 = gcnew UTF8Encoding;
		return utf8->GetString(rsa->Decrypt(Convert::FromBase64String(content), false));
	}

	static String^ RSAEncrypt(String^ publickey, String^ content)
	{
		RSACryptoServiceProvider^ rsa = gcnew RSACryptoServiceProvider();
		rsa->FromXmlString(publickey);
		UTF8Encoding^ utf8 = gcnew UTF8Encoding;
		return Convert::ToBase64String(rsa->Encrypt(utf8->GetBytes(content), false));
	}
};

delegate String^ MyCallbackRsa(String^ privatekey, String^ content);



static bool CheckRsa()
{
	string pubKey = "你的xml格式公钥";
	if (pubKey.empty())
		return false;	
	String^ strPubKey = marshal_as(pubKey);
	MyCallbackRsa^ callbackDec = gcnew MyCallbackRsa(SomeClass::RSADecrypt);
	MyCallbackRsa^ callbackEnc = gcnew MyCallbackRsa(SomeClass::RSAEncrypt);
	String^ enText = callbackEnc(strPubKey, "NDURJDLS00R434@8*&%@@@#LDJLDJJ4848922");
	String^ deText = callbackDec("你的xml格式的私钥", enText);
	if (deText == "NDURJDLS00R434@8*&%@@@#LDJLDJJ4848922")
	{
		return true;
	}
	return false;
}

你可能感兴趣的:(c++)