Skip to content

RSAMethods

Jordan Duerksen edited this page Jul 4, 2018 · 1 revision

RSAMethods contains static methods which can be used instead of the RSA class.

using System.Security.Cryptography;
using W.Encryption;

//return the available legal key sizes 
public static KeySizes[] LegalKeySizes();

//create a public/private key pair
public static CreateKeyPair(int keySize, out RSAParameters privateKey, out RSAParameters publicKey);

//encrypt an array of bytes with the given key (usually the public key)
public static byte[] Encrypt(byte[] bytes, RSAParameters key);

//decrypt an array of encrypted bytes with the given key (the private key)
public static byte[] Decrypt(byte[] bytes, RSAParameters key);

//asynchronously encrypt an array of bytes with the given key (usually the public key)
public static byte[] EncryptAsync(byte[] bytes, RSAParameters key);

//asynchronously decrypt an array of encrypted bytes with the given key (the private key)
public static byte[] DecryptAsync(byte[] bytes, RSAParameters key);

So encryption is fairly straightforward:

using System.Security.Cryptography;
using W.Encryption;
using W; //reference the Tungsten.As Nuget package for the AsBytes, AsBase64 and AsString extension methods

public static void Main()
{
    var message = "This is a test message";
    var bytes = message.AsBytes();

    RSAMethods.CreateKeyPair(1024, out RSAParameters privateKey, out RSAParameters publicKey);
    var cipher = RSAMethods.Encrypt(bytes, publicKey);
    var deciphered = RSAMethods.Decrypt(cipher, privateKey);

    Console.WriteLine($"Cipher = {cipher.AsBase64()}");
    Console.WriteLine($"Deciphered = {deciphered.AsString()}");
}

Clone this wiki locally