Using system;
Using system.security.cryptography;
Using system.text;
Class Rsacspsample
{
Public void maintest ()
{
Try
{
/ / Create a UnicodeEncoder in order to implement byte arrays to character.
UnicodeEncoding Byteconverter = new unicodeEncoding ();
// Create a byte array to keep the original, encrypted and decrypted data
// byte [] datatoencrypt = byteconverter.getbytes ("Data that needs to be encrypted");
Byte [] datatoencrypt = byteconverter.getbytes ("requires encrypted data");
Byte [] encrypteddata;
DecryptedData;
// Create an instance of RSacryptoServiceProvider to generate public and private key data
RSacryptoServiceProvider RSA = New RsacryptoServiceProvider ();
// encrypt, public key information through this data
// Use RSacryptoServiceProvider.exportParameters (False),
// and a Boolean variable indicate whether it is filled with OAEP.
EncryptedData = RSAENCRYPT (DatatoEncrypt, RSA.ExportParameters (false), false;
Console.writeline ("Encrypted Plaintext: {0}", Convert.TOBASE64String (EncryptedData);
// Decrypt, private key is used through this data
//RsacryptoServiceProvider.exportParameters (true),
// and a Boolean variable indicate whether it is filled with OAEP.
DecryptedData = Rsadecrypt (EncryptedData, RSA.ExportParameters (TRUE), FALSE
// Display the decrypted information to the console.
Console.writeline ("Decrypted PlainText: {0}", byteconverter.getstring (decryptedData);
Console.read ();
}
Catch (argumentnullexception)
{
Console.writeline ("Encryption Failed.");
}
}
Public Byte [] rsaencrypt (byte [] DataToencrypt, Rsaparameters RsakeyInfo, Bool DooaEppadding)
{
Try
{
RSacryptoServiceProvider RSA = New RsacryptoServiceProvider ();
// Enter the RSA Key information. Includes public key information.
Rsa.importParameters (RsakeyInfo);
// Encrypt this byte array and whether it is filled with OAEP
// OAEP fills only in Microsoft Windows XP or
// useful in future versions
Return RSA.Encrypt (DatatoEncrypt, DooaEppAdding);
}
Catch (CryptographicException E)
{
Console.writeLine (E.MESSAGE);
Return NULL;
}
}
Public Byte [] Rsadecrypt (Byte [] DataTodecrypt, Rsaparameters Rsakeyinfo, Bool DooaEppadding) {
Try
{
RSacryptoServiceProvider RSA = New RsacryptoServiceProvider ();
// Enter the RSA Key information. This needs include
// Private Key Information
Rsa.importParameters (RsakeyInfo);
// Decrypt this byte array and whether it is filled with OAEP.
Return RSA.Decrypt (Datatodecrypt, DooAEppAdding);
}
Catch (CryptographicException E)
{
Console.writeline (E.TOString ());
Return NULL;
}
}
}