public string SerializeEncrypt(string key, byte[] initVectorArray, object value) { var byteArrayKey = CryptoService.CreateByteArrayKey(key); var serializer = new JsonSerializer { ContractResolver = _encryptionFactory.GetContractResolver() }; string serialized; // per serialize session using (var algorithm = new AesManaged { Key = byteArrayKey, IV = initVectorArray }) { using (_encryptionFactory.GetEncryptSession(algorithm)) { var builder = new StringBuilder(); using (var writer = new StringWriter(builder)) { serializer.Serialize(writer, value); } return(serialized = builder.ToString()); } } }
public T DeserializeDecrypt <T>(string key, byte[] initVector, string value) { var byteArrayKey = CryptoService.CreateByteArrayKey(key); var serializer = new JsonSerializer { ContractResolver = _encryptionFactory.GetContractResolver() }; using (var algorithm = new AesManaged { Key = byteArrayKey, IV = initVector }) { using (_encryptionFactory.GetDecryptSession(algorithm)) using (var stringReader = new StringReader(value)) using (var jsonReader = new JsonTextReader(stringReader)) { return(serializer.Deserialize <T>(jsonReader)); } } }