/// <summary>Encrypt a string using a symmetric key algorithm where the key and IV are randomly generated.</summary> /// <param name="plainText">The string to encrypt</param> /// <param name="keySize">The key size to use</param> public CipherMessage Encrypt(string plainText, int keySize) { if (String.IsNullOrWhiteSpace(plainText)) { throw new ArgumentNullException(nameof(plainText)); } // validate and set the key size try { this._algo.KeySize = keySize; } catch (CryptographicException) { this._algo.KeySize = this._algo.LegalKeySizes[0].MinSize; } // make a IV and key this._algo.GenerateIV(); this._algo.GenerateKey(); CipherMessage msg = new CipherMessage(); msg.IV = this._algo.IV; msg.Key = this._algo.Key; msg.CipherBytes = this.Encrypt(plainText, this._algo.Key, this._algo.IV); return(msg); }
/// <summary>Encrypt JUST plain bytes with some key</summary> /// <param name="data"></param> /// <param name="key"></param> /// <returns></returns> public CipherMessage Encrypt(byte[] data, byte[] key) { // validate arguments if (data == null || data.Length < 1) { throw new System.ArgumentNullException(nameof(data)); } // need better validation of the key size if (key == null || key.Length < 1) { throw new System.ArgumentNullException(nameof(key)); } // setup the algorithm this.Algorithm.Mode = CipherMode.CBC; //this.Algorithm.KeySize = 128; //this.Algorithm.BlockSize = 128; //this.Algorithm.FeedbackSize = 128; //this.Algorithm.Padding = PaddingMode.Zeros; this.Algorithm.GenerateIV(); this.Algorithm.Key = key; CipherMessage results = new CipherMessage(); // create an encryptor ICryptoTransform encryptor = this.Algorithm.CreateEncryptor(this.Algorithm.Key, this.Algorithm.IV); using (var ms = new MemoryStream()) using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) { cs.Write(data, 0, data.Length); cs.FlushFinalBlock(); results.CipherBytes = ms.ToArray(); } results.IV = this.Algorithm.IV; return(results); }
/// <summary>Encrypt JUST plain bytes with some key</summary> /// <param name="data"></param> /// <param name="key"></param> /// <returns></returns> public CipherMessage Encrypt(byte[] data, byte[] key) { // validate arguments if (data == null || data.Length < 1) { throw new System.ArgumentNullException(nameof(data)); } // validation of the key if (!IsKeySizeValid(key, this.KeySizes[0])) { throw new System.ArgumentNullException(nameof(key)); } // setup the algorithm this.Algorithm.GenerateIV(); this.Algorithm.Key = key; CipherMessage results = new CipherMessage() { IV = this.Algorithm.IV, }; try { // create an encryptor ICryptoTransform encryptor = this.Algorithm.CreateEncryptor(this.Algorithm.Key, this.Algorithm.IV); // decrypt results.CipherBytes = this.DoCrypto(data, encryptor); } catch (System.Exception) { throw; } return(results); }
/// <summary>Encrypt plain text with a known key but generate an IV</summary> /// <param name="plainText">text to encrypt</param> /// <param name="key">encryption key to use</param> /// <returns>Encrypted bytes and the IV generated</returns> public CipherMessage Encrypt(string plainText, byte[] key) { // validate arguments if (String.IsNullOrWhiteSpace(plainText)) { throw new System.ArgumentNullException(nameof(plainText)); } if (key == null || key.Length < 1) { throw new System.ArgumentNullException(nameof(key)); } CipherMessage results = new CipherMessage(); // generate an IV this._algo.GenerateIV(); results.IV = this._algo.IV; results.Key = key; results.CipherBytes = this.Encrypt(plainText, results.Key, results.IV); return(results); }
/// <summary>Decrypt cipter text using a symmetric key algorithm.</summary> public string Decrypt(CipherMessage msg) { return(this.Decrypt(msg.CipherBytes, msg.Key, msg.IV)); }