/// <summary> /// Decrypt the original data from only partially encrypted/decrypted data /// </summary> /// <typeparam name="T">Type of result</typeparam> /// <param name="encryptionService">Encryption service to use for decryption</param> /// <param name="keyPassword">Hash of main password</param> /// <param name="convert">Converter from decrypted string into type T; not needed to specify if result is string, int, double or bool</param> /// <returns>Decrypted data of type T</returns> public T Decrypt <T>(EncryptionService encryptionService, byte[] keyPassword, Func <string, T> convert = null) { T result = default(T); if (EncryptedPart != null) { if (Salt == null) { Salt = encryptionService.GenerateRandomCryptographicKey(32); } var individualKeyPassword = encryptionService.GetIndividualKeyInBytes(keyPassword, Salt); var decryptedPart = encryptionService.Decrypt(individualKeyPassword, EncryptedPart); if (typeof(T) == typeof(string)) { result = (T)Convert.ChangeType(decryptedPart, typeof(string)); } else if (typeof(T) == typeof(int)) { result = (T)Convert.ChangeType(decryptedPart, typeof(int)); } else if (typeof(T) == typeof(double)) { result = (T)Convert.ChangeType(decryptedPart, typeof(double)); } else if (typeof(T) == typeof(bool)) { result = (T)Convert.ChangeType(decryptedPart, typeof(bool)); } else if (convert != null) { result = convert(decryptedPart); } } Original = result; return(result); }
/// <summary> /// Decrypt first part of encryption layer /// </summary> /// <param name="encryptionService">Encryption service to use for decryption</param> /// <param name="keyPassword">Hash of main password</param> public void DecryptPart(EncryptionService encryptionService, byte[] keyPassword) { EncryptedPart = encryptionService.Decrypt(keyPassword, Encrypted); }