private byte[] EncryptDataFile(byte[] fileBytes) { byte[] encryptedBytes; switch (Settings.Default.DataFileCryption) { case CryptoChoices.OneTimePad: OneTimePadCrypter otp = new OneTimePadCrypter(Encoding.UTF8.GetBytes(Settings.Default.CryptionDataKey)); encryptedBytes = otp.Encrypt(fileBytes); break; case CryptoChoices.TEA: TEACrypter tea = new TEACrypter(Encoding.UTF8.GetBytes(Settings.Default.CryptionDataKey)); if (TEACrypter.CheckIfDataNeedsPadding(fileBytes)) { fileBytes = TEACrypter.PadData(fileBytes); Settings.Default.DataFilePadded = true; Settings.Default.Save(); } encryptedBytes = tea.Encrypt(fileBytes); break; default: throw new InvalidOperationException("Cryption settings are not valid: Desired cryption algorithm doesn't exist."); } return(encryptedBytes); }
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[] EncryptFile(string fileName, byte[] fileBytes, CryptoChoice settings, bool replaceRecordIfConflict = false) { if (ReadRecord(fileName) != null) { if (replaceRecordIfConflict) { RemoveRecord(fileName); } else { throw new InvalidOperationException("The record for a file with this name already exists. If you want to replace it, pass another bool as an argument to the method."); } } SHA1Hasher sha1 = new SHA1Hasher(); sha1.ComputeHash(fileBytes); string fileHash = sha1.HashedString; byte[] encryptedBytes; switch (settings.Choice) { case CryptoChoices.OneTimePad: OneTimePadCrypter otp = new OneTimePadCrypter(settings.Key); encryptedBytes = otp.Encrypt(fileBytes); break; case CryptoChoices.TEA: TEACrypter tea = new TEACrypter(settings.Key); if (TEACrypter.CheckIfDataNeedsPadding(fileBytes)) { fileBytes = TEACrypter.PadData(fileBytes); settings.Depad = true; } encryptedBytes = tea.Encrypt(fileBytes); break; default: throw new InvalidOperationException("Cryption settings are not valid: Desired cryption algorithm doesn't exist."); } AddRecord(fileName, settings, fileHash); return(encryptedBytes); }
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); }