Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Security.Cryptography;
- using System.Text;
- namespace Lab04_cryptoAPI_
- {
- class RSACSPSample
- {
- static void Main()
- {
- Console.WriteLine("Введите строку для шифрования:");
- var original = Console.ReadLine();
- var dataToEncrypt = Encoding.Unicode.GetBytes(original);
- try
- {
- using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
- {
- //Pass the data to ENCRYPT, the public key information
- //(using RSACryptoServiceProvider.ExportParameters(false),
- //and a boolean flag specifying no OAEP padding.
- var encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false);
- Console.WriteLine($"Зашифрованное сообщение: {Encoding.Unicode.GetString(encryptedData)}");
- //Pass the data to DECRYPT, the private key information
- //(using RSACryptoServiceProvider.ExportParameters(true),
- //and a boolean flag specifying no OAEP padding.
- var decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false);
- //Display the decrypted plaintext to the console.
- Console.WriteLine($"Рвсшифрованное сообщение: {Encoding.Unicode.GetString(decryptedData)}");
- }
- }
- catch (ArgumentNullException)
- {
- //Catch this exception in case the encryption did
- //not succeed.
- Console.WriteLine("Encryption failed.");
- }
- }
- static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
- {
- try
- {
- byte[] encryptedData;
- //Create a new instance of RSACryptoServiceProvider.
- using (RSACryptoServiceProvider RSA = new 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.
- encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
- }
- return encryptedData;
- }
- //Catch and display a CryptographicException
- //to the console.
- catch (CryptographicException e)
- {
- Console.WriteLine(e.Message);
- return null;
- }
- }
- static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
- {
- try
- {
- byte[] decryptedData;
- //Create a new instance of RSACryptoServiceProvider.
- using (RSACryptoServiceProvider RSA = new 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.
- decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
- }
- return decryptedData;
- }
- //Catch and display a CryptographicException
- //to the console.
- catch (CryptographicException e)
- {
- Console.WriteLine(e.ToString());
- return null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment