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/CryptoUtils.scala | |
parent | 9877ccf7fe7df38f8ecc65d01e9b46a98528502f (diff) |
Rough cut of challenge 10
Diffstat (limited to 'src/utils/CryptoUtils.scala')
-rw-r--r-- | src/utils/CryptoUtils.scala | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/src/utils/CryptoUtils.scala b/src/utils/CryptoUtils.scala index 2b4d38e..430934b 100644 --- a/src/utils/CryptoUtils.scala +++ b/src/utils/CryptoUtils.scala @@ -1,18 +1,27 @@ package ixee.cryptopals.utils -import ixee.cryptopals.utils.crypto.CBCCipher +import ixee.cryptopals.utils.crypto._ import javax.crypto.Cipher import javax.crypto.spec.SecretKeySpec object CryptoUtils { - def pkcs7pad(s: String, blockSize: Int) = { + def pkcs7pad(s: Seq[Byte], blockSize: Int): Seq[Byte] = { val padLength = blockSize - (s.length % blockSize) - s + s"${padLength.toChar}" * padLength + s ++ Stream.continually(padLength.toByte).take(padLength) } - def cbcDecryptInstance(ecbCipher: Cipher, key: SecretKeySpec): CBCCipher = { - ecbCipher.init(Cipher.DECRYPT_MODE, key) - new CBCCipher(ecbCipher) + def cbcDecryptInstance(cipherPrim: String, key: Seq[Byte], iv: Seq[Byte]): CBCDecrypter = { + val keySpec = new SecretKeySpec(key.toArray, cipherPrim) + val cipher = Cipher.getInstance(s"$cipherPrim/ECB/NoPadding") + cipher.init(Cipher.DECRYPT_MODE, keySpec) + new CBCDecrypter(cipher, iv) + } + + def cbcEncryptInstance(cipherPrim: String, key: Seq[Byte], iv: Seq[Byte]): CBCEncrypter = { + val keySpec = new SecretKeySpec(key.toArray, cipherPrim) + val cipher = Cipher.getInstance(s"$cipherPrim/ECB/NoPadding") + cipher.init(Cipher.ENCRYPT_MODE, keySpec) + new CBCEncrypter(cipher, iv) } } |