summaryrefslogtreecommitdiff
path: root/src/solvers/Challenge12.scala
blob: 0db336c37b167d4ca8a81e3ad5f9013b36e5ad8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package ixee.cryptopals.solvers

import scala.io.Source
import ixee.cryptopals.utils.ByteUtils._
import ixee.cryptopals.utils.CryptoUtils._
import ixee.cryptopals.utils.StreamUtils._
import ixee.cryptopals.utils.ConversionUtils._
import ixee.cryptopals.utils.FunctionUtils._
import ixee.cryptopals.utils._
import ixee.cryptopals.utils.crypto._
import io.github.marklister.base64.Base64._

object Challenge12 {
  lazy val mysteryText =
    """|Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkg
       |aGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq
       |dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUg
       |YnkK""".stripMargin.replace("\n", "").toByteArray

  val expectation = """|Rollin' in my 5.0
                       |With my rag-top down so my hair can blow
                       |The girlies on standby waving just to say hi
                       |Did you stop? No, I just drove by
                       |""".stripMargin

  val key = """|Copy your oracle function to a new function that
               | encrypts buffers under ECB mode using a consistent
               | but unknown key (for instance, assign a single
               | random key, once, to a global variable).""".stripMargin
            .replace("\n", "")
            .asBytes
            .grouped(16)
            .toSeq
            .init
            .reduce(_ xor _)

  def encryptFn(input: Seq[Byte]): Seq[Byte] =
    SchemeBuilder("AES", key).ecb.encrypt.end(input ++ mysteryText)

  def run = {
    CryptoUtils.extractUnknownViaEcbOracle(encryptFn _)
  }
}