public static byte[] RSAEncrypt(this byte[] input, RSAKeySet RSAPublicKey, bool DoOAEPPadding = true) { using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) { RSA.ImportParameters(new RSAParameters() { Exponent = RSAPublicKey.Exponent, Modulus = RSAPublicKey.Modulus }); return(RSA.Encrypt(input, DoOAEPPadding)); } }
/// <summary> /// Generate RSA encrypted byte[] using system gerenated public key and private key. /// </summary> /// <param name="input">Byte[] input</param> /// <param name="RSAPublicKey">Public key output</param> /// <param name="RSAPrivateKey">Private key output</param> /// <param name="DoOAEPPadding">Specifying OAEP padding</param> /// <returns></returns> public static byte[] RSAEncrypt(this byte[] input, out RSAKeySet RSAPublicKey, out RSAKeySet RSAPrivateKey, bool DoOAEPPadding = true) { using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) { RSAParameters parameters = RSA.ExportParameters(true); RSAPublicKey = new RSAKeySet(parameters.Modulus, parameters.Exponent); RSAPrivateKey = new RSAKeySet(parameters); return(input.RSAEncrypt(RSAPublicKey, DoOAEPPadding)); } }
public static byte[] RSADecrypt(this byte[] input, RSAKeySet RSAPrivateKey, bool DoOAEPPadding = true) { using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) { RSA.ImportParameters(new RSAParameters() { Exponent = RSAPrivateKey.Exponent, Modulus = RSAPrivateKey.Modulus, D = RSAPrivateKey.D, P = RSAPrivateKey.P, Q = RSAPrivateKey.Q, DP = RSAPrivateKey.DP, DQ = RSAPrivateKey.DQ, InverseQ = RSAPrivateKey.InverseQ }); return(RSA.Decrypt(input, DoOAEPPadding)); } }
/// <summary> /// Return UTF8 decoded string after decrypt RSA byte[]. Use RSADecrypt if your original source is not a string. /// </summary> /// <param name="input">Byte[] input</param> /// <param name="RSAPrivateKey">Private key input</param> /// <param name="DoOAEPPadding">Specifying OAEP padding</param> /// <returns></returns> public static string RSAStringDecrypt(this byte[] input, RSAKeySet RSAPrivateKey, bool DoOAEPPadding = true) => input.RSADecrypt(RSAPrivateKey, DoOAEPPadding).ConvertToString();
/// <summary> /// Convert the UTF8 input string to byte[] and generate RSA encrypted array using public key provided. /// </summary> /// <param name="input">String input</param> /// <param name="RSAPublicKey">Public key output</param> /// <param name="DoOAEPPadding">Specifying OAEP padding</param> /// <returns></returns> public static byte[] RSAEncrypt(this string input, RSAKeySet RSAPublicKey, bool DoOAEPPadding = true) => input.ConvertToByteArray().RSAEncrypt(RSAPublicKey, DoOAEPPadding);