加密部分需要用c++语言实现,解密部分需要用c#实现!
所以看了下msdn的介绍
http://msdn.microsoft.com/zh-cn/library/system.security.cryptography.rsacryptoserviceprovider(VS.80).aspx#Mtps_DropDownFilterText现在的问题是C#的代码我都能用,但是c++的代码我就不知道该如何用了!
要运行这段代码需要什么条件吗?using namespace System;
using namespace System::Security::Cryptography;
using namespace System::Text;
array<Byte>^ RSAEncrypt( array<Byte>^DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding )
{
try
{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider^ RSA = gcnew RSACryptoServiceProvider;
//Import the RSA Key information. This only needs
//toinclude the public key information.
RSA->ImportParameters( RSAKeyInfo );
//Encrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
return RSA->Encrypt( DataToEncrypt, DoOAEPPadding );
}
//Catch and display a CryptographicException
//to the console.
catch ( CryptographicException^ e )
{
Console::WriteLine( e->Message );
return nullptr;
}}array<Byte>^ RSADecrypt( array<Byte>^DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding )
{
try
{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider^ RSA = gcnew RSACryptoServiceProvider;
//Import the RSA Key information. This needs
//to include the private key information.
RSA->ImportParameters( RSAKeyInfo );
//Decrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
return RSA->Decrypt( DataToDecrypt, DoOAEPPadding );
}
//Catch and display a CryptographicException
//to the console.
catch ( CryptographicException^ e )
{
Console::WriteLine( e );
return nullptr;
}}int main()
{
try
{
//Create a UnicodeEncoder to convert between byte array and string.
UnicodeEncoding^ ByteConverter = gcnew UnicodeEncoding;
//Create byte arrays to hold original, encrypted, and decrypted data.
array<Byte>^dataToEncrypt = ByteConverter->GetBytes( "Data to Encrypt" );
array<Byte>^encryptedData;
array<Byte>^decryptedData;
//Create a new instance of RSACryptoServiceProvider to generate
//public and private key data.
RSACryptoServiceProvider^ RSA = gcnew RSACryptoServiceProvider;
//Pass the data to ENCRYPT, the public key information
//(using RSACryptoServiceProvider.ExportParameters(false),
//and a boolean flag specifying no OAEP padding.
encryptedData = RSAEncrypt( dataToEncrypt, RSA->ExportParameters( false ), false );
//Pass the data to DECRYPT, the private key information
//(using RSACryptoServiceProvider.ExportParameters(true),
//and a boolean flag specifying no OAEP padding.
decryptedData = RSADecrypt( encryptedData, RSA->ExportParameters( true ), false );
//Display the decrypted plaintext to the console.
Console::WriteLine( "Decrypted plaintext: {0}", ByteConverter->GetString( decryptedData ) );
}
catch ( ArgumentNullException^ )
{
//Catch this exception in case the encryption did
//not succeed.
Console::WriteLine( "Encryption failed." );
}}
具体步骤是怎么样的?
运行这段代码需要什么样的项目?
本人对c++一窍不通!c++项目的文件安排和c#的完全不是一回事!头大!
所以看了下msdn的介绍
http://msdn.microsoft.com/zh-cn/library/system.security.cryptography.rsacryptoserviceprovider(VS.80).aspx#Mtps_DropDownFilterText现在的问题是C#的代码我都能用,但是c++的代码我就不知道该如何用了!
要运行这段代码需要什么条件吗?using namespace System;
using namespace System::Security::Cryptography;
using namespace System::Text;
array<Byte>^ RSAEncrypt( array<Byte>^DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding )
{
try
{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider^ RSA = gcnew RSACryptoServiceProvider;
//Import the RSA Key information. This only needs
//toinclude the public key information.
RSA->ImportParameters( RSAKeyInfo );
//Encrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
return RSA->Encrypt( DataToEncrypt, DoOAEPPadding );
}
//Catch and display a CryptographicException
//to the console.
catch ( CryptographicException^ e )
{
Console::WriteLine( e->Message );
return nullptr;
}}array<Byte>^ RSADecrypt( array<Byte>^DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding )
{
try
{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider^ RSA = gcnew RSACryptoServiceProvider;
//Import the RSA Key information. This needs
//to include the private key information.
RSA->ImportParameters( RSAKeyInfo );
//Decrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
return RSA->Decrypt( DataToDecrypt, DoOAEPPadding );
}
//Catch and display a CryptographicException
//to the console.
catch ( CryptographicException^ e )
{
Console::WriteLine( e );
return nullptr;
}}int main()
{
try
{
//Create a UnicodeEncoder to convert between byte array and string.
UnicodeEncoding^ ByteConverter = gcnew UnicodeEncoding;
//Create byte arrays to hold original, encrypted, and decrypted data.
array<Byte>^dataToEncrypt = ByteConverter->GetBytes( "Data to Encrypt" );
array<Byte>^encryptedData;
array<Byte>^decryptedData;
//Create a new instance of RSACryptoServiceProvider to generate
//public and private key data.
RSACryptoServiceProvider^ RSA = gcnew RSACryptoServiceProvider;
//Pass the data to ENCRYPT, the public key information
//(using RSACryptoServiceProvider.ExportParameters(false),
//and a boolean flag specifying no OAEP padding.
encryptedData = RSAEncrypt( dataToEncrypt, RSA->ExportParameters( false ), false );
//Pass the data to DECRYPT, the private key information
//(using RSACryptoServiceProvider.ExportParameters(true),
//and a boolean flag specifying no OAEP padding.
decryptedData = RSADecrypt( encryptedData, RSA->ExportParameters( true ), false );
//Display the decrypted plaintext to the console.
Console::WriteLine( "Decrypted plaintext: {0}", ByteConverter->GetString( decryptedData ) );
}
catch ( ArgumentNullException^ )
{
//Catch this exception in case the encryption did
//not succeed.
Console::WriteLine( "Encryption failed." );
}}
具体步骤是怎么样的?
运行这段代码需要什么样的项目?
本人对c++一窍不通!c++项目的文件安排和c#的完全不是一回事!头大!
解决方案 »
- 关于日常
- Windows有关GDI映射的函数,哪些函数的参数用逻辑坐标,哪些函数的参数用的是设备坐标
- 用vc编写更改系统时间
- 大吼一声:csdn怎么提交自己的软件?
- LPCTSTR 类型的变量怎么定义和付值?
- 简单的程序关于Sleep函数
- 请问如何给 Droplist 添加CString成员变量? (不是看起来的简单问题)
- 直奔主题->Come IN--------------------->>
- 线程实现绘制图形逐步显示??
- 基于CObject定义了类CStu,现在我想删除此类。我删除了Stu.cpp,Stu.h。但FileView中的Stu.cpp,Stu.h却仍存在?如何彻底删除呀。
- Cominterop
- 基础问题,connect函数什么时候返回?
这段代码是c++的代码,但是就像ljz888666555说的,好像是托管代码!
不知道如何运行!
这段代码我是从MSDN上抄来的!还有C#版的!