rsa11
rsa11
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
//using System.Runtime.InteropServices;
//using System.Runtime.CompilerServices;
namespace ConsoleApplication2GT {
class Program {
private static RSAParameters publickey;
private static RSAParameters privatekey;
};
static void Main(string[] args) {
Console.WriteLine("enter your massage");
string message =Console.ReadLine() ;
//string message = "My Name is Mohammed Al_Wsapi ";
generateKeys();
byte[] encrypted = Encrpt(Encoding.UTF8.GetBytes(message));
byte[] decrypted = Decrpt(encrypted);
Console.WriteLine("original\n\t" + message + "\n");
Console.WriteLine("Encrypted\n\t" +
BitConverter.ToString(encrypted).Replace("-","")+ "\n");
Console.WriteLine("Decrypted\n\t" + Encoding.UTF8.GetString(decrypted));
Console.ReadLine();
}
static void generateKeys()
{
using (var rsa = new RSACryptoServiceProvider(2048))
{
publickey = rsa.ExportParameters(true);
privatekey = rsa.ExportParameters(true);
}
}
static byte[] Encrpt(byte[] input)
{
byte[] encrypted;
using (var rsa = new RSACryptoServiceProvider(2048)) {
rsa.ImportParameters(publickey);
encrypted=rsa.Encrypt(input,true);
}
return encrypted;
}
static byte[] Decrpt(byte[] input)
{
byte[] decrypted;
using (var rsa = new RSACryptoServiceProvider(2048)) {
rsa.ImportParameters(privatekey);
decrypted = rsa.Decrypt(input, true);
}
return decrypted;
}
} }