Beispiel #1
0
        /// <summary>
        /// Encrypts string
        /// Size of data cannot be longer than the key used
        /// </summary>
        /// <param name="data">Data to encrypt, cannot be larger than the key used</param>
        /// <param name="key">Key to use for encryption</param>
        /// <returns>Encrypted data as string</returns>
        public static string Encrypt(string data, RsaPublicKey key)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(data);

            byte[] encrypted = Encrypt(bytes, key);

            return(Convert.ToBase64String(encrypted).BeautifyBase64());
        }
Beispiel #2
0
        /// <summary>
        /// Encrypts array of bytes
        /// Size of data cannot be longer than the key used
        /// </summary>
        /// <param name="data">Data to encrypt, cannot be larger than the key used</param>
        /// <param name="key">Key to use for encryption</param>
        /// <returns>Encrypted data as byte array</returns>
        public static byte[] Encrypt(byte[] data, RsaPublicKey key)
        {
            using (var rsa = RSA.Create())
            {
                var parms = key.GetParameters();
                rsa.KeySize = parms.Modulus.Length * 8;
                rsa.ImportParameters(parms);

                return(rsa.Encrypt(data, RSAEncryptionPadding.OaepSHA1));
            }
        }
Beispiel #3
0
 /// <summary>
 /// Constructor, accepts RSAParameters
 /// </summary>
 /// <param name="rsaParams">RSA parameters</param>
 public RsaKeyPair(RSAParameters rsaParams)
 {
     PublicKey  = new RsaPublicKey(rsaParams);
     PrivateKey = new RsaPrivateKey(rsaParams);
 }
Beispiel #4
0
 /// <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);
 }