blob: 430934b7d12b9df192afd07af1b5dc7a8c2a8203 (
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
|
package ixee.cryptopals.utils
import ixee.cryptopals.utils.crypto._
import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec
object CryptoUtils {
def pkcs7pad(s: Seq[Byte], blockSize: Int): Seq[Byte] = {
val padLength = blockSize - (s.length % blockSize)
s ++ Stream.continually(padLength.toByte).take(padLength)
}
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)
}
}
|