/// <summary> /// Generate a new RSA key pair. /// </summary> /// <param name="publicKey">An XML string containing ONLY THE PUBLIC RSA KEY.</param> /// <param name="privateKey">An XML string containing a PUBLIC AND PRIVATE RSA KEY.</param> public RSAKey GenerateKeys() { try { CspParameters cspParams = GetCspParameters(); cspParams.Flags = CspProviderFlags.UseArchivableKey; rsaProvider = new RSACryptoServiceProvider(KEY_SIZE, cspParams); RSAKey key = new RSAKey { publicKey = rsaProvider.ToXmlString(false), privateKey = rsaProvider.ToXmlString(true) }; return(key); } catch (Exception ex) { // Any errors? Show them throw new Exception("Exception generating a new RSA key pair! More info: " + ex.Message); } finally { // Do some clean up if needed } } // GenerateKeys method
public static void StoreKeyPairs(string containerName, RSAKey key, string password) { StorePublicKey(containerName, key.publicKey); StorePrivateKey(containerName, key.privateKey, password); }