/// <summary> /// Decrypts array of bytes (if public key is used for encryption than private key needs to be used for decryption and vice versa) /// </summary> /// <param name="data">Data to decrypt</param> /// <param name="key">Key to use for decryption</param> /// <returns>Decrypted data</returns> public static byte[] Decrypt(byte[] data, RsaPrivateKey key) { using (var rsa = RSA.Create()) { var parms = key.GetParameters(); rsa.KeySize = parms.Modulus.Length * 8; rsa.ImportParameters(parms); return(rsa.Decrypt(data, RSAEncryptionPadding.OaepSHA1)); } }
/// <summary> /// Decrypts string (if public key is used for encryption than private key needs to be used for decryption and vice versa) /// </summary> /// <param name="data">Data to decrypt</param> /// <param name="key">Key to use for decryption</param> /// <returns>Decrypted data</returns> public static string Decrypt(string data, RsaPrivateKey key) { byte[] bytes = Convert.FromBase64String(data.UglifyBase64()); byte[] decrypted = Decrypt(bytes, key); return(Encoding.UTF8.GetString(decrypted)); }
/// <summary> /// Constructor, accepts RSAParameters /// </summary> /// <param name="rsaParams">RSA parameters</param> public RsaKeyPair(RSAParameters rsaParams) { PublicKey = new RsaPublicKey(rsaParams); PrivateKey = new RsaPrivateKey(rsaParams); }
/// <summary> /// Constructor accepts private key, only stores public part of the key /// </summary> /// <param name="key">Private key</param> public RsaPublicKey(RsaPrivateKey key) : this(key?.Key) { }
/// <summary> /// Constructor accepts private key /// </summary> /// <param name="privateKey">Private key</param> public RsaKeyPair(RsaPrivateKey privateKey) { PrivateKey = privateKey ?? throw new System.ArgumentNullException(nameof(privateKey)); PublicKey = new RsaPublicKey(privateKey); }