Beispiel #1
0
        /// <summary>
        /// Decrypts data using the provided private key.
        /// </summary>
        /// <exception cref="T:System.ArgumentNullException">data or privateKey is null.</exception>
        public Hash.PreEncryptData Decrypt(Hash.PreEncryptData data, PrivateKey privateKey)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (privateKey == null)
            {
                throw new ArgumentNullException(nameof(privateKey));
            }

            _rsa.ImportParameters(privateKey.ToParameters());
            return(DecryptPrivate(data));
        }
Beispiel #2
0
        /// <summary>
        /// Decrypts data using the provided private key XML.
        /// </summary>
        /// <exception cref="T:System.ArgumentNullException">data or privateKeyXml is null.</exception>
        public Hash.PreEncryptData Decrypt(Hash.PreEncryptData data, string privateKeyXml)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (privateKeyXml == null)
            {
                throw new ArgumentNullException(nameof(privateKeyXml));
            }

            LoadKeyXml(privateKeyXml, true);
            return(DecryptPrivate(data));
        }
Beispiel #3
0
        private Hash.PreEncryptData EncryptPrivate(Hash.PreEncryptData data)
        {
            Debug.Assert(data != null);

            try
            {
                return(new Hash.PreEncryptData(_rsa.Encrypt(data.Bytes, false)));
            }
            catch (CryptographicException ex)
            {
                if (ex.Message.IndexOf("bad length", StringComparison.InvariantCultureIgnoreCase) > -1)
                {
                    throw new CryptographicException("Your data is too large; RSA encryption is designed to encrypt relatively small amounts of data. The exact byte limit depends on the key size. To encrypt more data, use symmetric encryption and then encrypt that symmetric key with asymmetric RSA encryption.", ex);
                }
                throw;
            }
        }
Beispiel #4
0
 private Hash.PreEncryptData DecryptPrivate(Hash.PreEncryptData data)
 {
     Debug.Assert(data != null);
     return(new Hash.PreEncryptData(_rsa.Decrypt(data.Bytes, false)));
 }