Skip to content

Instantly share code, notes, and snippets.

@sebnozzi
Last active December 19, 2016 15:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebnozzi/4f82efaf8f637520a5ac922df54d7d30 to your computer and use it in GitHub Desktop.
Save sebnozzi/4f82efaf8f637520a5ac922df54d7d30 to your computer and use it in GitHub Desktop.
A curious way to decrypt a text (like a mobile-phone number) - if you know the key ;-)
import java.util.Base64
import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec
import scala.io.StdIn
object DecryptApp extends App {
object Decrypter {
private val decoder = Base64.getDecoder
def decrypt(text: String, key: String): String = {
val encryptedDecoded = decoder.decode(text)
new String(cipher(key).doFinal(encryptedDecoded))
}
private def cipher(key: String) = {
val cipher = Cipher.getInstance("AES")
val aesKey = new SecretKeySpec(key.getBytes, "AES")
cipher.init(Cipher.DECRYPT_MODE, aesKey)
cipher
}
}
// Key has to be 16 bytes long, with first_name + "___" + last_name
def buildKey(input: String): String = {
val Array(firstName, lastName) =
input
.split(" ")
.map(_.trim)
.map(_.toLowerCase)
.map(_.capitalize)
firstName + "___" + lastName
}
val key = {
val input = StdIn.readLine("Who created Scala (hint: M.... Od.....) ?")
buildKey(input)
}
val encrypted = StdIn.readLine("Enter text to decrypt: ")
println("Decrypted text is: " + Decrypter.decrypt(encrypted, key))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment