public static T Deserialize <T>(string base64EncryptedXmlString, KeyVectorPair key) { RijndaelManaged rKey = new RijndaelManaged(); rKey.IV = Convert.FromBase64String(key.IV); rKey.Key = Convert.FromBase64String(key.Key); string xml = Decrypt(base64EncryptedXmlString, key.Key, key.IV); return(SerializationUtil.FromXmlString <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 KeyVectorPair key) { string xml = SerializationUtil.GetXml(target); RijndaelManaged rm = new RijndaelManaged(); rm.GenerateIV(); rm.GenerateKey(); key = new KeyVectorPair(); 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 specied 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)); } KeyVectorPair key = KeyVectorPair.Load(keyFile); //Serialization.Deserialize<Base64EncodedRijndaelKeyVectorPair>(keyFile); using (StreamReader sr = new StreamReader(filePath)) { string text = sr.ReadToEnd(); return(Deserialize <T>(text, key)); } }
public static string Decrypt(string base64EncodedValue, KeyVectorPair key) { return(Decrypt(base64EncodedValue, key.Key, key.IV)); }
public static string Encrypt(string value, KeyVectorPair key) { return(Encrypt(value, key.Key, key.IV)); }