// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Text; using System.Security.Cryptography; namespace E { public class Person { private string name; private RSACryptoServiceProvider rsa; public Person(string name) { this.name = name; rsa = new RSACryptoServiceProvider(512); } public void Recv(Person from, byte[] b) { RSACryptoServiceProvider other = new RSACryptoServiceProvider(); other.ImportParameters(from.Parameters); other.ImportCspBlob(from.PublicKey); string msg = Encoding.UTF8.GetString(other.Decrypt(b, false)); Console.WriteLine(this + " received from " + from + ": " + msg); } public void Send(Person to, String msg) { Console.WriteLine(this + " sending to " + to + ": " + msg); byte[] plain = Encoding.UTF8.GetBytes(msg); byte[] cipher = rsa.Encrypt(plain, false); Console.WriteLine(Convert.ToBase64String(cipher)); to.Recv(this, cipher); } public override string ToString() { return name; } public RSAParameters Parameters { get { return rsa.ExportParameters(false); } } public byte[] PublicKey { get { return rsa.ExportCspBlob(true); } } } public class Program { public static void Main(string[] args) { Person alice = new Person("Alice"); Person bob = new Person("Bob"); bob.Send(alice, "This is a test"); } } }