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); }
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); }