// Create RSA key pair RSA rsa = RSA.Create(); // Encrypt data byte[] data = Encoding.ASCII.GetBytes("Hello World!"); byte[] encryptedData = rsa.Encrypt(data, RSAEncryptionPadding.OaepSHA256); // Decrypt data byte[] decryptedData = rsa.Decrypt(encryptedData, RSAEncryptionPadding.OaepSHA256); string message = Encoding.ASCII.GetString(decryptedData); Console.WriteLine(message); // Outputs "Hello World!"
// Create DSA key pair DSA dsa = DSA.Create(); // Sign data byte[] data = Encoding.ASCII.GetBytes("Hello World!"); byte[] signature = dsa.SignData(data, HashAlgorithmName.SHA256); // Verify signature bool isValid = dsa.VerifyData(data, signature, HashAlgorithmName.SHA256); Console.WriteLine(isValid); // Outputs "True"In both examples, the System.Security.Cryptography package is used to create an instance of an asymmetric algorithm (RSA or DSA). The algorithm is then used to encrypt/decrypt data or sign/verify data using a public-private key pair.