public T Decrypt <T>(CryptedDataWrapper cryptedDataWrapper) where T : class { using (var aes = InitAes(cryptedDataWrapper.Salt)) { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(cryptedDataWrapper.CipherBytes, 0, cryptedDataWrapper.CipherBytes.Length); } var plainBytes = ms.ToArray(); return(ConvertByteArray.ByteArrayToObject <T>(plainBytes)); } } }
public CryptedDataWrapper Encrypt <T>(T data) where T : class { var wrapper = new CryptedDataWrapper(); using (var aes = InitAes(wrapper.Salt)) { using (var ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write)) { var unencrypted = ConvertByteArray.ObjectToByteArray(data); cs.Write(unencrypted, 0, unencrypted.Length); } wrapper.CipherBytes = ms.ToArray(); } } return(wrapper); }