private byte[] DecryptDataFile(byte[] encryptedBytes) { byte[] fileBytes; switch (Settings.Default.DataFileCryption) { case CryptoChoices.OneTimePad: OneTimePadCrypter otp = new OneTimePadCrypter(Encoding.UTF8.GetBytes(Settings.Default.CryptionDataKey)); fileBytes = otp.Decrypt(encryptedBytes); break; case CryptoChoices.TEA: TEACrypter tea = new TEACrypter(Encoding.UTF8.GetBytes(Settings.Default.CryptionDataKey)); fileBytes = tea.Decrypt(encryptedBytes); if (Settings.Default.DataFilePadded) { fileBytes = TEACrypter.DepadData(fileBytes); } break; default: throw new InvalidOperationException("Cryption settings are not valid: Desired cryption algorithm doesn't exist."); } return(fileBytes); }
internal byte[] DecryptFileExplicit(byte[] encryptedBytes, CryptoChoice settings) { byte[] fileBytes; switch (settings.Choice) { case CryptoChoices.OneTimePad: OneTimePadCrypter otp = new OneTimePadCrypter(settings.Key); fileBytes = otp.Decrypt(encryptedBytes); break; case CryptoChoices.TEA: TEACrypter tea = new TEACrypter(settings.Key); fileBytes = tea.Decrypt(encryptedBytes); if (settings.Depad) { fileBytes = TEACrypter.DepadData(fileBytes); } break; default: throw new InvalidOperationException("Cryption settings are not valid: Desired cryption algorithm doesn't exist."); } return(fileBytes); }
public byte[] DecryptFile(byte[] encryptedBytes, string fileName) { var foundRecord = ReadRecord(fileName); if (foundRecord == null) { throw new ArgumentException("There is no record for this file."); } CryptoChoice settings = foundRecord.Item1; string originalFileHash = foundRecord.Item2; byte[] fileBytes; switch (settings.Choice) { case CryptoChoices.OneTimePad: OneTimePadCrypter otp = new OneTimePadCrypter(settings.Key); fileBytes = otp.Decrypt(encryptedBytes); break; case CryptoChoices.TEA: TEACrypter tea = new TEACrypter(settings.Key); fileBytes = tea.Decrypt(encryptedBytes); if (settings.Depad) { fileBytes = TEACrypter.DepadData(fileBytes); } break; default: throw new InvalidOperationException("Cryption settings are not valid: Desired cryption algorithm doesn't exist."); } SHA1Hasher sha1 = new SHA1Hasher(); sha1.ComputeHash(fileBytes); if (originalFileHash != sha1.HashedString) { throw new Exception("The hash value of the decrypted file and the original file hash value do not match. Access denied."); } return(fileBytes); }