Adilol

C++ RSA Encryption

Jul 18th, 2011
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. using namespace System;
  2. using namespace System::Security::Cryptography;
  3. using namespace System::Text;
  4. array<Byte>^ RSAEncrypt( array<Byte>^DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding )
  5. {
  6.    try
  7.    {
  8.  
  9.       //Create a new instance of RSACryptoServiceProvider.
  10.       RSACryptoServiceProvider^ RSA = gcnew RSACryptoServiceProvider;
  11.  
  12.       //Import the RSA Key information. This only needs
  13.       //toinclude the public key information.
  14.       RSA->ImportParameters( RSAKeyInfo );
  15.  
  16.       //Encrypt the passed byte array and specify OAEP padding.  
  17.       //OAEP padding is only available on Microsoft Windows XP or
  18.       //later.  
  19.  
  20.       array<Byte>^encryptedData = RSA->Encrypt( DataToEncrypt, DoOAEPPadding );
  21.       delete RSA;
  22.       return encryptedData;
  23.    }
  24.    //Catch and display a CryptographicException  
  25.    //to the console.
  26.    catch ( CryptographicException^ e )
  27.    {
  28.       Console::WriteLine( e->Message );
  29.       return nullptr;
  30.    }
  31.  
  32. }
  33.  
  34. array<Byte>^ RSADecrypt( array<Byte>^DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding )
  35. {
  36.    try
  37.    {
  38.  
  39.       //Create a new instance of RSACryptoServiceProvider.
  40.       RSACryptoServiceProvider^ RSA = gcnew RSACryptoServiceProvider;
  41.  
  42.       //Import the RSA Key information. This needs
  43.       //to include the private key information.
  44.       RSA->ImportParameters( RSAKeyInfo );
  45.  
  46.       //Decrypt the passed byte array and specify OAEP padding.  
  47.       //OAEP padding is only available on Microsoft Windows XP or
  48.       //later.  
  49.      
  50.       array<Byte>^decryptedData = RSA->Decrypt( DataToDecrypt, DoOAEPPadding );
  51.       delete RSA;
  52.       return decryptedData;
  53.    }
  54.    //Catch and display a CryptographicException  
  55.    //to the console.
  56.    catch ( CryptographicException^ e )
  57.    {
  58.       Console::WriteLine( e );
  59.       return nullptr;
  60.    }
  61.  
  62. }
  63.  
  64. int main()
  65. {
  66.    try
  67.    {
  68.  
  69.       //Create a UnicodeEncoder to convert between byte array and string.
  70.       UnicodeEncoding^ ByteConverter = gcnew UnicodeEncoding;
  71.  
  72.       //Create byte arrays to hold original, encrypted, and decrypted data.
  73.       array<Byte>^dataToEncrypt = ByteConverter->GetBytes( "Data to Encrypt" );
  74.       array<Byte>^encryptedData;
  75.       array<Byte>^decryptedData;
  76.  
  77.       //Create a new instance of RSACryptoServiceProvider to generate
  78.       //public and private key data.
  79.       RSACryptoServiceProvider^ RSA = gcnew RSACryptoServiceProvider;
  80.  
  81.       //Pass the data to ENCRYPT, the public key information
  82.       //(using RSACryptoServiceProvider.ExportParameters(false),
  83.       //and a boolean flag specifying no OAEP padding.
  84.       encryptedData = RSAEncrypt( dataToEncrypt, RSA->ExportParameters( false ), false );
  85.  
  86.       //Pass the data to DECRYPT, the private key information
  87.       //(using RSACryptoServiceProvider.ExportParameters(true),
  88.       //and a boolean flag specifying no OAEP padding.
  89.       decryptedData = RSADecrypt( encryptedData, RSA->ExportParameters( true ), false );
  90.  
  91.       //Display the decrypted plaintext to the console.
  92.       Console::WriteLine( "Decrypted plaintext: {0}", ByteConverter->GetString( decryptedData ) );
  93.       delete RSA;
  94.    }
  95.    catch ( ArgumentNullException^ )
  96.    {
  97.  
  98.       //Catch this exception in case the encryption did
  99.       //not succeed.
  100.       Console::WriteLine( "Encryption failed." );
  101.    }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment