diff options
author | iximeow <me@iximeow.net> | 2014-11-27 02:06:03 -0800 |
---|---|---|
committer | iximeow <me@iximeow.net> | 2014-11-27 02:06:03 -0800 |
commit | 0af5a2e4ade0b032e1ec6d25c51de2ff95dd2799 (patch) | |
tree | a4771f91a22821ffae20224dcc04269a31f73bff /src/utils/crypto | |
parent | 9877ccf7fe7df38f8ecc65d01e9b46a98528502f (diff) |
Rough cut of challenge 10
Diffstat (limited to 'src/utils/crypto')
-rw-r--r-- | src/utils/crypto/CBCCipher.scala | 23 | ||||
-rw-r--r-- | src/utils/crypto/CBCDecrypter.scala | 36 |
2 files changed, 54 insertions, 5 deletions
diff --git a/src/utils/crypto/CBCCipher.scala b/src/utils/crypto/CBCCipher.scala index e1ece8a..d6d99ff 100644 --- a/src/utils/crypto/CBCCipher.scala +++ b/src/utils/crypto/CBCCipher.scala @@ -1,21 +1,34 @@ package ixee.cryptopals.utils.crypto import javax.crypto.Cipher +import ixee.cryptopals.utils.ConversionUtils._ +import ixee.cryptopals.utils.FunctionUtils._ +import ixee.cryptopals.utils.CryptoUtils._ +import ixee.cryptopals.utils.TupleUtils._ +import ixee.cryptopals.utils.ByteUtils._ + +class CBCEncrypter(cipher: Cipher, init: Seq[Byte]) { + val blockSize = 16 -class CBCCipher(cipher: Cipher, init: Seq[Byte]) { var state: Seq[Byte] = init var leftover: Seq[Byte] = Seq() def enc(data: Seq[Byte]): Seq[Byte] = { - (blocks, leftover) = blockized(leftover append data) - blocks.map(encBlock).reduce(_ append _) + val (blocks, newLeftover) = blockized(leftover ++ data)(blockSize) + leftover = newLeftover + blocks.map(encBlock _).foldLeft(Seq[Byte]())(_ ++ _) } def encBlock(data: Seq[Byte]): Seq[Byte] = { - state = cipher.doFinal((data xor state).toArray) + state = cipher.update((data xor state).toArray) state } + def end(): Seq[Byte] = + cipher.doFinal((pkcs7pad(leftover, blockSize) xor state).toArray) + def blockized(data: Seq[Byte])(size: Int): (Seq[Seq[Byte]], Seq[Byte]) = - data.splitAt(data.length - (data.length % size)) + groupBlocks <-: data.splitAt(data.length - (data.length % size)) + + def groupBlocks: Seq[Byte] => Seq[Seq[Byte]] = _.grouped(blockSize).toSeq } diff --git a/src/utils/crypto/CBCDecrypter.scala b/src/utils/crypto/CBCDecrypter.scala new file mode 100644 index 0000000..a6cf855 --- /dev/null +++ b/src/utils/crypto/CBCDecrypter.scala @@ -0,0 +1,36 @@ + +package ixee.cryptopals.utils.crypto + +import javax.crypto.Cipher +import ixee.cryptopals.utils.ConversionUtils._ +import ixee.cryptopals.utils.FunctionUtils._ +import ixee.cryptopals.utils.CryptoUtils._ +import ixee.cryptopals.utils.TupleUtils._ +import ixee.cryptopals.utils.ByteUtils._ + +class CBCDecrypter(cipher: Cipher, init: Seq[Byte]) { + val blockSize = 16 + + var state: Seq[Byte] = init + var leftover: Seq[Byte] = Seq() + + def dec(data: Seq[Byte]): Seq[Byte] = { + val (blocks, newLeftover) = blockized(leftover ++ data)(blockSize) + leftover = newLeftover + blocks.map(decBlock _).foldLeft(Seq[Byte]())(_ ++ _) + } + + def decBlock(data: Seq[Byte]): Seq[Byte] = { + val ret = cipher.update(data.toArray).toSeq xor state + state = data + ret + } + + def end(): Seq[Byte] = + cipher.doFinal((pkcs7pad(leftover, blockSize) xor state).toArray) + + def blockized(data: Seq[Byte])(size: Int): (Seq[Seq[Byte]], Seq[Byte]) = + groupBlocks <-: data.splitAt(data.length - (data.length % size)) + + def groupBlocks: Seq[Byte] => Seq[Seq[Byte]] = _.grouped(blockSize).toSeq +} |