/// <summary> /// Import the encrypted data from a previously combined string of format "{Cipher}:{KeyName}:{Iv}:{CipherText}" /// </summary> /// <param name="combinedString"></param> /// <returns></returns> public static EncryptedField FromCombinedString(string combinedString) { var parts = combinedString.Split(":"); var encryptedField = new EncryptedField( parts[1], parts[0], parts[2], parts[3]); return(encryptedField); }
/// <summary> /// Create a new EncryptedField class from plaintext. The returned object contains the encrypted data. /// </summary> /// <param name="plainText"></param> /// <returns></returns> public static EncryptedField FromPlainText(string plainText) { var iv = Random128BitsBase64(); var key = KeyStorage.Shared.GetPrimary(); using (EncryptionHistogram.Labels(Aes256CbcString).NewTimer()) { var encryptedData = new EncryptedField( key.KeyName, Aes256CbcString, iv, Convert.ToBase64String( EncryptStringToBytes_Aes256CBC( plainText, key.KeyData, // we always encrypt with the primary Convert.FromBase64String(iv)))); return(encryptedData); } }