コード例 #1
0
 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));
     }
 }
コード例 #2
0
        /// <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));
            }
        }
コード例 #3
0
 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));
     }
 }
コード例 #4
0
 /// <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();
コード例 #5
0
 /// <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);