Skip to content

Instantly share code, notes, and snippets.

@mumoshu
Created January 10, 2012 02:52
Show Gist options
  • Save mumoshu/1586581 to your computer and use it in GitHub Desktop.
Save mumoshu/1586581 to your computer and use it in GitHub Desktop.
DES encryption in Scala
name := "DES encryption in Scala"
version := "1.0"
scalaVersion := "2.9.1"
libraryDependencies += "commons-codec" % "commons-codec" % "1.6"
import javax.crypto.spec.DESKeySpec
import javax.crypto.{Cipher, SecretKeyFactory}
import org.apache.commons.codec.binary.Base64
object Main extends App {
def desEncrypt(data: String, secret: String) = {
val keySpec = new DESKeySpec(secret.getBytes("UTF-8"))
val secretKey = SecretKeyFactory.getInstance("DES").generateSecret(keySpec)
val encipher = Cipher.getInstance("DES/ECB/PKCS5Padding")
encipher.init(Cipher.ENCRYPT_MODE, secretKey)
Base64.encodeBase64String(encipher.doFinal(data.getBytes("UTF-8")))
}
println(desEncrypt("hoge", "012345678"))
//=> vyudTtnBJfs=
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment