/// <summary> /// Encrypts data using the provided public key /// </summary> public Data Encrypt(Data d, PublicKey publicKey) { _rsa.ImportParameters(publicKey.ToParameters()); return EncryptPrivate(d); }
/// <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); }
/// <summary> /// Encrypts data using the default public key /// </summary> public Data Encrypt(Data d) { PublicKey PublicKey = DefaultPublicKey; return Encrypt(d, PublicKey); }
/// <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); }
/// <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); }
/// <summary> /// Decrypts data using the default private key /// </summary> public Data Decrypt(Data encryptedData) { PrivateKey PrivateKey = new PrivateKey(); PrivateKey.LoadFromConfig(); return Decrypt(encryptedData, PrivateKey); }
private Data DecryptPrivate(Data encryptedData) { return new Data(_rsa.Decrypt(encryptedData.Bytes, false)); }
/// <summary> /// Encrypts the specified Data using provided key /// </summary> public Data Encrypt(Data d, Data key) { this.Key = key; return Encrypt(d); }
/// <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()); }
/// <summary> /// generates a random Key, if one was not provided /// </summary> public Data RandomKey() { _crypto.GenerateKey(); Data d = new Data(_crypto.Key); return d; }
/// <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; }
/// <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; }
/// <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); }
/// <summary> /// Calculates hash for fixed length <see cref="Data"/> /// </summary> public Data Calculate(Data d) { return CalculatePrivate(d.Bytes); }
/// <summary> /// Encrypts data using the provided public key as XML /// </summary> public Data Encrypt(Data d, string publicKeyXML) { LoadKeyXml(publicKeyXML, false); return EncryptPrivate(d); }
/// <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); }
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; } } }
/// <summary> /// Encrypts the stream to memory using specified key /// </summary> public Data Encrypt(Stream s, Data key) { this.Key = key; return Encrypt(s); }
/// <summary> /// Decrypts data using the provided private key /// </summary> public Data Decrypt(Data encryptedData, PrivateKey PrivateKey) { _rsa.ImportParameters(PrivateKey.ToParameters()); return DecryptPrivate(encryptedData); }
/// <summary> /// Decrypts data using the provided private key as XML /// </summary> public Data Decrypt(Data encryptedData, string PrivateKeyXML) { LoadKeyXml(PrivateKeyXML, true); return DecryptPrivate(encryptedData); }