public static T Decrypt <T>(string filePath, AesKeyVectorPair key) { string text; using (StreamReader sr = new StreamReader(filePath)) { text = sr.ReadToEnd(); } return(Deserialize <T>(text, key)); }
public static T Deserialize <T>(string base64EncryptedXmlString, AesKeyVectorPair key) { AesManaged rKey = new AesManaged(); rKey.IV = Convert.FromBase64String(key.IV); rKey.Key = Convert.FromBase64String(key.Key); string xml = Decrypt(base64EncryptedXmlString, key.Key, key.IV); return(SerializationExtensions.FromXml <T>(xml)); }
/// <summary> /// Get a base64 encoded encrypted xml serialization string representing the specified target object /// </summary> /// <param name="target">The object to serialize</param> /// <param name="key">The key used to encrypt and decrypt the resulting string</param> /// <returns>string</returns> public static string ToBase64EncodedEncryptedXml(this object target, out AesKeyVectorPair key) { string xml = SerializationExtensions.ToXml(target); AesManaged rm = new AesManaged(); rm.GenerateIV(); rm.GenerateKey(); key = new AesKeyVectorPair(); key.Key = Convert.ToBase64String(rm.Key); key.IV = Convert.ToBase64String(rm.IV); return(Encrypt(xml, rm.CreateEncryptor())); }
public static T Decrypt <T>(string filePath, string keyFile) { if (!File.Exists(filePath)) { throw new FileNotFoundException(string.Format("The file specified to deserialize from {0} does not exist", filePath)); } if (!File.Exists(keyFile)) { throw new FileNotFoundException(string.Format("The key file specified {0} does not exist", keyFile)); } AesKeyVectorPair key = AesKeyVectorPair.Load(keyFile); //Serialization.Deserialize<Base64EncodedRijndaelKeyVectorPair>(keyFile); return(Decrypt <T>(filePath, key)); }
/// <summary> /// Decrypts the specified base64 encoded value. /// </summary> /// <param name="base64EncodedValue">The base64 encoded value.</param> /// <param name="key">The key.</param> /// <returns></returns> public static string Decrypt(string base64EncodedValue, AesKeyVectorPair key) { return(Decrypt(base64EncodedValue, key.Key, key.IV)); }
/// <summary> /// Gets a Base64 encoded value representing the cypher of the specified /// value using the specified key. /// </summary> /// <param name="value"></param> /// <param name="key"></param> /// <returns></returns> public static string Encrypt(string value, AesKeyVectorPair key) { return(Encrypt(value, key.Key, key.IV)); }