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); }
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); }
public void ShouldEncryptAndDecryptFullBlockByTEA() { var rnd = new Random(19); var block = new byte[8]; var key = new byte[16]; rnd.NextBytes(block); rnd.NextBytes(key); var crypter = new TEACrypter(key); var encrypt = crypter.Encrypt(block); var decrypt = crypter.Decrypt(encrypt); decrypt.Should().BeEquivalentTo(block, o => o.WithStrictOrdering()); }
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); }