-
-
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 ;-)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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