Skip to content

Instantly share code, notes, and snippets.

@scottfrazer
Last active September 7, 2019 03:29
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save scottfrazer/80b29f147ce640977ab8 to your computer and use it in GitHub Desktop.
Save scottfrazer/80b29f147ce640977ab8 to your computer and use it in GitHub Desktop.
Scala AES 256 encryption
import javax.crypto.{KeyGenerator, Cipher}
import javax.crypto.spec.SecretKeySpec
import org.apache.commons.codec.binary.Base64
trait Encryption {
def encrypt(dataBytes: Array[Byte], secret: String): Array[Byte]
def decrypt(codeBytes: Array[Byte], secret: String): Array[Byte]
}
class JavaCryptoEncryption(algorithm: String) extends Encryption {
private def decodeBase64(string: String) = Base64.decodeBase64(string)
private def cipher(mode: Int, b64secret: String): Cipher = {
val encipher = Cipher.getInstance(algorithm + "/ECB/PKCS5Padding")
encipher.init(mode, new SecretKeySpec(decodeBase64(b64secret), algorithm))
encipher
}
def encrypt(bytes: Array[Byte], b64secret: String): Array[Byte] = {
val encoder = cipher(Cipher.ENCRYPT_MODE, b64secret)
encoder.doFinal(bytes)
}
def decrypt(bytes: Array[Byte], b64secret: String): Array[Byte] = {
val decoder = cipher(Cipher.DECRYPT_MODE, b64secret)
decoder.doFinal(bytes)
}
}
object DES extends JavaCryptoEncryption("DES")
object AES extends JavaCryptoEncryption("AES")
object crypto {
def main(args: Array[String]) {
def encodeBase64(bytes: Array[Byte]) = Base64.encodeBase64String(bytes)
def generateKey(algorithm: String, size: Int): Array[Byte] = {
val generator = KeyGenerator.getInstance(algorithm)
generator.init(size)
generator.generateKey().getEncoded
}
val string = args(0)
println(s"JAVA_HOME=${System.getProperty("java.home")}")
// For 256 to work, see http://stackoverflow.com/questions/6481627/java-security-illegal-key-size-or-default-parameters
val genKey = encodeBase64(generateKey("AES", 256))
println(s"genKey: $genKey")
println(s"string: `$string`")
val encrypted = AES.encrypt(string.getBytes("UTF-8"), genKey)
println(s"encrypted: ${encodeBase64(encrypted)}")
val decrypted = AES.decrypt(encrypted, genKey)
println(s"decrypted: ${new String(decrypted, "UTF-8")}")
}
}
# http://stackoverflow.com/questions/6481627/java-security-illegal-key-size-or-default-parameters
# maybe doesn't need to be set... but I needed to set it on my machine
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre
# Download the "Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 8 Download":
# http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html
# then, copy the JAR files into your JAVA installation:
cp *.jar $JAVA_HOME/lib/security
#!/bin/bash
rm *.class
scalac -classpath .:commons-codec-1.10.jar crypto.scala
scala -classpath .:commons-codec-1.10.jar crypto "my string"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment