Skip to content

Instantly share code, notes, and snippets.

@srinivaskcg
Forked from mumoshu/build.sbt
Created December 8, 2015 06:41
Show Gist options
  • Save srinivaskcg/f21a56bb6a44bf1fc9f9 to your computer and use it in GitHub Desktop.
Save srinivaskcg/f21a56bb6a44bf1fc9f9 to your computer and use it in GitHub Desktop.

Revisions

  1. @mumoshu mumoshu created this gist Jan 10, 2012.
    7 changes: 7 additions & 0 deletions build.sbt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    name := "DES and AES encryption in Scala"

    version := "1.0"

    scalaVersion := "2.9.1"

    libraryDependencies += "commons-codec" % "commons-codec" % "1.6"
    91 changes: 91 additions & 0 deletions main.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    package protocol {

    import annotation.implicitNotFound

    import java.io.{DataOutputStream, ByteArrayOutputStream}

    @implicitNotFound(msg = "Could not find a Writes for ${T}")
    trait Writes[T] {

    def writes(value: T): Array[Byte]
    }

    class DataOutputStreamWrites[T](writeValue: (DataOutputStream, T) => Unit) extends Writes[T] {

    def writes(value: T): Array[Byte] = {
    val bos = new ByteArrayOutputStream
    val dos = new DataOutputStream(bos)
    writeValue(dos, value)
    dos.flush()
    val byteArray = bos.toByteArray
    bos.close()
    byteArray
    }
    }

    object defaults {
    implicit object WritesString extends Writes[String] {
    def writes(value: String) = value.getBytes("UTF-8")
    }
    implicit object WritesLong extends DataOutputStreamWrites[Long](_.writeLong(_))
    implicit object WritesInt extends DataOutputStreamWrites[Int](_.writeInt(_))
    implicit object WritesShort extends DataOutputStreamWrites[Short](_.writeShort(_))
    }
    }

    package crypto {

    import protocol.Writes

    import javax.crypto.spec.SecretKeySpec
    import javax.crypto.Cipher

    trait Encryption {
    def encrypt(dataBytes: Array[Byte], secret: String): Array[Byte]
    def decrypt(codeBytes: Array[Byte], secret: String): Array[Byte]

    def encrypt[T:Writes](data: T, secret: String): Array[Byte] = encrypt(implicitly[Writes[T]].writes(data), secret)
    }

    class JavaCryptoEncryption(algorithmName: String) extends Encryption {

    def encrypt(bytes: Array[Byte], secret: String): Array[Byte] = {
    val secretKey = new SecretKeySpec(secret.getBytes("UTF-8"), algorithmName)
    val encipher = Cipher.getInstance(algorithmName + "/ECB/PKCS5Padding")
    encipher.init(Cipher.ENCRYPT_MODE, secretKey)
    encipher.doFinal(bytes)
    }

    def decrypt(bytes: Array[Byte], secret: String): Array[Byte] = {
    val secretKey = new SecretKeySpec(secret.getBytes("UTF-8"), algorithmName)
    val encipher = Cipher.getInstance(algorithmName + "/ECB/PKCS5Padding")
    encipher.init(Cipher.DECRYPT_MODE, secretKey)
    encipher.doFinal(bytes)
    }
    }

    object DES extends JavaCryptoEncryption("DES")
    object AES extends JavaCryptoEncryption("AES")
    }

    object Main extends App {

    import org.apache.commons.codec.binary.Base64

    import crypto._
    import protocol.defaults._

    def encodeBase64(bytes: Array[Byte]) = Base64.encodeBase64String(bytes)

    println(encodeBase64(DES.encrypt("hoge", "01234567")))
    //=> vyudTtnBJfs=

    println(encodeBase64(AES.encrypt("hoge", "0123456789012345")))
    //=> QWSouZUMVYMfS86xFyBgtQ==

    println(encodeBase64(DES.encrypt(123L, "01234567")))
    //=> Cqw2ipxTtvIIu122s3wG1w==

    println(encodeBase64(DES.encrypt(123, "01234567")))
    //=> BV+LSCSYmUU=
    }