コード例 #1
0
 /// <summary>
 /// Encrypts data using the provided public key
 /// </summary>
 public Data Encrypt(Data d, PublicKey publicKey)
 {
     _rsa.ImportParameters(publicKey.ToParameters());
     return EncryptPrivate(d);
 }
コード例 #2
0
        /// <summary>
        /// Decrypts the specified data using preset key and preset initialization vector
        /// </summary>
        public Data Decrypt(Data encryptedData)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream(encryptedData.Bytes, 0, encryptedData.Bytes.Length);
            byte[] b = new byte[encryptedData.Bytes.Length];

            ValidateKeyAndIv(false);
            CryptoStream cs = new CryptoStream(ms, _crypto.CreateDecryptor(), CryptoStreamMode.Read);

            try
            {
                cs.Read(b, 0, encryptedData.Bytes.Length - 1);
            }
            catch (CryptographicException ex)
            {
                throw new CryptographicException("Unable to decrypt data. The provided key may be invalid.", ex);
            }
            finally
            {
                cs.Close();
            }
            return new Data(b);
        }
コード例 #3
0
 /// <summary>
 /// Encrypts data using the default public key
 /// </summary>
 public Data Encrypt(Data d)
 {
     PublicKey PublicKey = DefaultPublicKey;
     return Encrypt(d, PublicKey);
 }
コード例 #4
0
 /// <summary>
 /// Decrypts the specified data using provided key and preset initialization vector
 /// </summary>
 public Data Decrypt(Data encryptedData, Data key)
 {
     this.Key = key;
     return Decrypt(encryptedData);
 }
コード例 #5
0
 /// <summary>
 /// Decrypts the specified stream using provided key and preset initialization vector
 /// </summary>
 public Data Decrypt(Stream encryptedStream, Data key)
 {
     this.Key = key;
     return Decrypt(encryptedStream);
 }
コード例 #6
0
 /// <summary>
 /// Decrypts data using the default private key
 /// </summary>
 public Data Decrypt(Data encryptedData)
 {
     PrivateKey PrivateKey = new PrivateKey();
     PrivateKey.LoadFromConfig();
     return Decrypt(encryptedData, PrivateKey);
 }
コード例 #7
0
 private Data DecryptPrivate(Data encryptedData)
 {
     return new Data(_rsa.Decrypt(encryptedData.Bytes, false));
 }
コード例 #8
0
 /// <summary>
 /// Encrypts the specified Data using provided key
 /// </summary>
 public Data Encrypt(Data d, Data key)
 {
     this.Key = key;
     return Encrypt(d);
 }
コード例 #9
0
        /// <summary>
        /// Encrypts the specified Data using preset key and preset initialization vector
        /// </summary>
        public Data Encrypt(Data d)
        {
            MemoryStream ms = new MemoryStream();

            ValidateKeyAndIv(true);

            CryptoStream cs = new CryptoStream(ms, _crypto.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(d.Bytes, 0, d.Bytes.Length);
            cs.Close();
            ms.Close();

            return new Data(ms.ToArray());
        }
コード例 #10
0
 /// <summary>
 /// generates a random Key, if one was not provided
 /// </summary>
 public Data RandomKey()
 {
     _crypto.GenerateKey();
     Data d = new Data(_crypto.Key);
     return d;
 }
コード例 #11
0
 /// <summary>
 /// Ensures that _crypto object has valid Key and IV
 /// prior to any attempt to encrypt/decrypt anything
 /// </summary>
 private void ValidateKeyAndIv(bool isEncrypting)
 {
     if (_key.IsEmpty)
     {
         if (isEncrypting)
         {
             _key = RandomKey();
         }
         else
         {
             throw new CryptographicException("No key was provided for the decryption operation!");
         }
     }
     if (_iv.IsEmpty)
     {
         if (isEncrypting)
         {
             _iv = RandomInitializationVector();
         }
         else
         {
             throw new CryptographicException("No initialization vector was provided for the decryption operation!");
         }
     }
     _crypto.Key = _key.Bytes;
     _crypto.IV = _iv.Bytes;
 }
コード例 #12
0
 /// <summary>
 /// generates a random Initialization Vector, if one was not provided
 /// </summary>
 public Data RandomInitializationVector()
 {
     _crypto.GenerateIV();
     Data d = new Data(_crypto.IV);
     return d;
 }
コード例 #13
0
 /// <summary>
 /// Calculates hash for a string with a prefixed salt value. 
 /// A "salt" is random data prefixed to every hashed value to prevent 
 /// common dictionary attacks.
 /// </summary>
 public Data Calculate(Data d, Data salt)
 {
     byte[] nb = new byte[d.Bytes.Length + salt.Bytes.Length];
     salt.Bytes.CopyTo(nb, 0);
     d.Bytes.CopyTo(nb, salt.Bytes.Length);
     return CalculatePrivate(nb);
 }
コード例 #14
0
 /// <summary>
 /// Calculates hash for fixed length <see cref="Data"/>
 /// </summary>
 public Data Calculate(Data d)
 {
     return CalculatePrivate(d.Bytes);
 }
コード例 #15
0
 /// <summary>
 /// Encrypts data using the provided public key as XML
 /// </summary>
 public Data Encrypt(Data d, string publicKeyXML)
 {
     LoadKeyXml(publicKeyXML, false);
     return EncryptPrivate(d);
 }
コード例 #16
0
 /// <summary>
 /// Encrypts the stream to memory using provided key and provided initialization vector
 /// </summary>
 public Data Encrypt(Stream s, Data key, Data iv)
 {
     this.IntializationVector = iv;
     this.Key = key;
     return Encrypt(s);
 }
コード例 #17
0
 private Data EncryptPrivate(Data d)
 {
     try
     {
         return new Data(_rsa.Encrypt(d.Bytes, false));
     }
     catch (CryptographicException ex)
     {
         if (ex.Message.ToLower().IndexOf("bad length") > -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);
         }
         else
         {
             throw;
         }
     }
 }
コード例 #18
0
 /// <summary>
 /// Encrypts the stream to memory using specified key
 /// </summary>
 public Data Encrypt(Stream s, Data key)
 {
     this.Key = key;
     return Encrypt(s);
 }
コード例 #19
0
 /// <summary>
 /// Decrypts data using the provided private key
 /// </summary>
 public Data Decrypt(Data encryptedData, PrivateKey PrivateKey)
 {
     _rsa.ImportParameters(PrivateKey.ToParameters());
     return DecryptPrivate(encryptedData);
 }
コード例 #20
0
 /// <summary>
 /// Decrypts data using the provided private key as XML
 /// </summary>
 public Data Decrypt(Data encryptedData, string PrivateKeyXML)
 {
     LoadKeyXml(PrivateKeyXML, true);
     return DecryptPrivate(encryptedData);
 }