From 31cac438b1571ba6ced29af4997cf9f1f5c3c6b7 Mon Sep 17 00:00:00 2001 From: iximeow Date: Wed, 26 Nov 2014 00:41:46 -0800 Subject: Challenge 7 --- test/Set1Spec.scala | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) (limited to 'test') diff --git a/test/Set1Spec.scala b/test/Set1Spec.scala index 628889c..b017803 100644 --- a/test/Set1Spec.scala +++ b/test/Set1Spec.scala @@ -6,6 +6,7 @@ class Set1Spec extends IxeeSpec { import ixee.cryptopals.utils.ConversionUtils._ import ixee.cryptopals.utils.ByteUtils._ + import ixee.cryptopals.utils.StreamUtils._ import ixee.cryptopals.solvers._ "Set1" - { @@ -46,6 +47,90 @@ class Set1Spec extends IxeeSpec { (("Now that the party is jumping\n", 170)) } } + + "Challenge 5: repeating-key xor" - { + val sourceText = """|Burning 'em, if you ain't quick and nimble + |I go crazy when I hear a cymbal""".stripMargin + + val key = "ICE" + + val ciphertext = + sourceText.asBytes.to[Stream] xor key.asBytes.to[Stream].continually + + ciphertext.hex mustBe( + Seq( + "0b3637272a2b2e63622c2e69", + "692a23693a2a3c6324202d62", + "3d63343c2a26226324272765", + "272a282b2f20430a652e2c65", + "2a3124333a653e2b2027630c", + "692b20283165286326302e27", + "282f" + ).mkString("") + ) + } + + "Challenge 6: break repeating-key xor" - { + + "hamming distance should be computed correctly" in { + + val textA = "this is a test" + val textB = "wokka wokka!!!" + + hammingDistance(textA, textB) mustBe 37 + + } + + "prioritizes likely key sizes" in { + + val sourceText = + """|the quick brown fox jumps over the lazy dog + |This code is going to turn out to be surprisingly useful later on. + |Breaking repeating-key XOR ("Vigenere") statistically is obviously + | an academic exercise, a "Crypto 101" thing. But more people "know + | how" to break it than can actually break it, and a similar + | technique breaks something much more important.""".stripMargin.replace("\n", "") + + val key = "ICE IS REALLY NICE" + + val ciphertext = sourceText.asByteStream xor key.asByteStream.continually + + val expectedKeySize = XorDecrypt.inferKeySize(ciphertext) + + expectedKeySize mustBe key.length + + } + + "cracks multi-byte keys" in { + val sourceText = + """|the quick brown fox jumps over the lazy dog + |This code is going to turn out to be surprisingly useful later on. + |Breaking repeating-key XOR statistically is obviously + | an academic exercise, a "Crypto 101" thing. But more people "know + | how" to break it than can actually break it, and a similar + | technique breaks something much more important.""".stripMargin.replace("\n", "") + + val key = "ICE IS REALLY NICE" + + val ciphertext = sourceText.asByteStream xor key.asByteStream.continually + + XorDecrypt.findBestMultiByteKey(ciphertext).take(key.length).asAscii mustBe key + + XorDecrypt.crackForMultiByteKey(ciphertext) mustBe sourceText + } + + "cracks the challenge text" in { + + Challenge6.run mustBe Challenge6.expectation + + } + } + + "Challenge 7: AES/ECB mode" in { + + Challenge7.run mustBe Challenge6.expectation + + } } } -- cgit v1.1