summaryrefslogtreecommitdiff
path: root/src/utils/CryptoUtils.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/CryptoUtils.scala')
-rw-r--r--src/utils/CryptoUtils.scala21
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)
}
}