public static byte[] EncryptProfileFor(string email) { var profile = ProfileFor(email); var bytes = System.Text.Encoding.ASCII.GetBytes(profile); var padded = PaddingUtil.Pad(bytes, BlockSizeBytes); return(AesEcb.Encrypt(Key, padded)); }
private static byte[] EncryptionOracle(byte[] data) { var combined = new List <byte>(); combined.AddRange(data); combined.AddRange(Convert.FromBase64String(EncodedSuffix)); var padded = PaddingUtil.Pad(combined.ToArray(), BlockSizeBytes); return(AesEcb.Encrypt(Key, padded)); }
public void TestPaddingInputSizeSmallerThanBlockSize() { int blockSize = 4; var input = new byte[] { 0x01, 0x02 }; var expected = new byte[] { 0x01, 0x02, 0x02, 0x02 }; var actual = PaddingUtil.Pad(input, blockSize); CollectionAssert.AreEqual(expected, actual); }
public void TestPaddingInputSizeEqualsBlockSize() { int blockSize = 5; var input = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 }; var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05 }; var actual = PaddingUtil.Pad(input, blockSize); CollectionAssert.AreEqual(expected, actual); }
public void TestChallenge9() { int blockSize = 20; var input = "YELLOW SUBMARINE"; var inputBytes = System.Text.Encoding.ASCII.GetBytes(input); var padding = new byte[] { 0x04, 0x04, 0x04, 0x04 }; var expected = new List <byte>(inputBytes); expected.AddRange(padding); var actual = PaddingUtil.Pad(inputBytes, blockSize); CollectionAssert.AreEqual(expected, actual); }
private static byte[] EncryptionOracle(byte[] data) { if (_randomPrefix == null || _randomPrefix.Length == 0) { var r = new Random(); var prefixLength = r.Next(1, MaxPrefixLength + 1); _randomPrefix = SecureRng.GenerateRandomBytes(prefixLength); } var combined = new List <byte>(); combined.AddRange(_randomPrefix); combined.AddRange(data); combined.AddRange(Convert.FromBase64String(EncodedSuffix)); var padded = PaddingUtil.Pad(combined.ToArray(), BlockSizeBytes); return(AesEcb.Encrypt(Key, padded)); }
public void TestChallenge15() { var blockSize = 16; var str = "ICE ICE BABY"; var bytes = System.Text.Encoding.ASCII.GetBytes(str); var valid = PaddingUtil.Pad(bytes, blockSize); Assert.IsTrue(PaddingUtil.ValidPadding(valid, blockSize)); var invalid = new List <byte>(bytes); invalid.AddRange(new byte[] { 0x05, 0x05, 0x05, 0x05 }); Assert.IsFalse(PaddingUtil.ValidPadding(invalid.ToArray(), blockSize)); invalid = new List <byte>(bytes); invalid.AddRange(new byte[] { 0x01, 0x02, 0x03, 0x04 }); Assert.IsFalse(PaddingUtil.ValidPadding(invalid.ToArray(), blockSize)); }
public static byte[] Encrypt(byte[] key, byte[] iv, byte[] data) { int paddingLength = BlockSizeBytes - (data.Length % BlockSizeBytes); paddingLength = paddingLength == 0 ? BlockSizeBytes : paddingLength; int cipherTextLength = data.Length + paddingLength; var cipherText = new byte[cipherTextLength]; var paddedData = PaddingUtil.Pad(data, BlockSizeBytes); var blocks = paddedData.Chunks(BlockSizeBytes); int index = 0; var previousBlock = iv; foreach (var block in blocks) { var xord = XorUtil.Xor(block.ToArray(), previousBlock); var encrypted = AesEcb.Encrypt(key, xord); Array.Copy(encrypted, 0, cipherText, index, BlockSizeBytes); previousBlock = encrypted; index += BlockSizeBytes; } return(cipherText); }
public static CipherMode EncryptEcbOrCbc(byte[] data, out CipherMode actualMode) { actualMode = Rand.Next(0, 2) == 0 ? CipherMode.ECB : CipherMode.CBC; var key = SecureRng.GenerateRandomBytes(KeySizeBytes); // Add prefix and suffix to data data = TransformData(data); byte[] encrypted; if (actualMode == CipherMode.ECB) { var padded = PaddingUtil.Pad(data, BlockSizeBytes); encrypted = AesEcb.Encrypt(key, padded); } else { var iv = SecureRng.GenerateRandomBytes(BlockSizeBytes); encrypted = AesCbc.Encrypt(key, iv, data); } var guessedMode = AesEcb.IsEcbEncrypted(encrypted, BlockSizeBytes) ? CipherMode.ECB : CipherMode.CBC; return(guessedMode); }