/// <summary>Encrypt a string using a symmetric key algorithm.</summary> public CipherMessage EncryptStringToBytes(string plainText, int keySize) { if (plainText == null || plainText.Length <= 0) { throw new ArgumentNullException("plainText"); } // validate and set the key size try { this._algo.KeySize = keySize; } catch (CryptographicException) { this._algo.KeySize = this._algo.LegalKeySizes[0].MinSize; } this._algo.GenerateIV(); this._algo.GenerateKey(); CipherMessage msg = new CipherMessage(); msg.IV = this._algo.IV; msg.Key = this._algo.Key; msg.CipherBytes = EncryptStringToBytes(plainText, this._algo.Key, this._algo.IV); return(msg); }
/// <summary></summary> private void DoEncrypt(object sender, EventArgs e) { if (String.IsNullOrEmpty(this.pwField.Text)) { this._msg = null; return; } this._msg = this._encryptor.EncryptStringToBytes(this.pwField.Text.Trim(), this._keySize); this.FormatFields(); }
/// <summary>Decrypt cipter text using a symmetric key algorithm.</summary> public string DecryptStringFromBytes(CipherMessage msg) { return(this.DecryptStringFromBytes(msg.CipherBytes, msg.Key, msg.IV)); }