using System; using System.Security.Cryptography; class RSASample { public static string Base64Encode(byte[] plainTextBytes) { return System.Convert.ToBase64String(plainTextBytes); } static void Main() { try { //Create a new instance of RSA. using (RSA rsa = RSA.Create()) { //The hash to sign. byte[] hash; using (SHA256 sha256 = SHA256.Create()) { byte[] data = System.Text.Encoding.UTF8.GetBytes("hello world!"); hash = sha256.ComputeHash(data); } String publicKey = rsa.ToXmlString(false); String privateKey = rsa.ToXmlString(true); //Create an RSASignatureFormatter object and pass it the //RSA instance to transfer the key information. RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(rsa); //Set the hash algorithm. RSAFormatter.SetHashAlgorithm("SHA256"); //Create a signature for HashValue and return it. byte[] SignedHash = RSAFormatter.CreateSignature(hash); Console.WriteLine("Public Key:"); Console.WriteLine(publicKey); Console.WriteLine("Private Key:"); Console.WriteLine(privateKey); Console.WriteLine("Sign:"); Console.WriteLine(Base64Encode(SignedHash)); } } catch (CryptographicException e) { Console.WriteLine(e.Message); } } }