Skip to content

Instantly share code, notes, and snippets.

@ahmedengu
Last active July 17, 2016 22:40
Show Gist options
  • Save ahmedengu/216259026820b207cf3d57aeaa7f1797 to your computer and use it in GitHub Desktop.
Save ahmedengu/216259026820b207cf3d57aeaa7f1797 to your computer and use it in GitHub Desktop.
Caesar Cipher - Running here: http://ideone.com/05j5RO
/*
* Created by ahmedengu.
*/
object CaesarCipher {
def main(args: Array[String]): Unit = {
while (0 != (scala.io.StdIn.readLine(
"Choose the ceaser cipher implemintion:\n" +
"1) With spicial characters \n" +
"2) With key sets\n" +
"3) Break 1\n" +
"4) Break 2\n" +
"5) Show the discussion answers\n" +
"0) To exit\n") toInt match {
case 1 => {
withSpicialChars
1
}
case 2 => {
withKeySets
2
}
case 3 => {
breakWithSpecialChars
3
}
case 4 => {
breakWithKeySets
4
}
case 5 => {
discussionAnswers
5
}
case _ => {
0
}
})) println("\n___________")
}
def withSpicialChars: Unit = {
val key = scala.io.StdIn.readLine("Enter the key\n") toInt
while (3 != (scala.io.StdIn.readLine("Choose operation:\n" +
"1) Encript\n" +
"2) Decript\n" +
"3) To return\n") toInt match {
case 1 => {
encryptWthSpecial(key)
}
case 2 => {
decryptWthSpecial(key)
}
case _ => {
3
}
})) println("\n___________")
}
def decryptWthSpecial(key: Int): Unit = {
val cipher = scala.io.StdIn readLine("Enter cipher\n")
for (c <- cipher) {
print(if (c.toInt >= 32 && c.toInt <= 126) {
val i1: Int = (c.toInt - 32 - key) % 95
(if (i1 >= 0) 32 + i1 else 127 + i1) toChar
} else c)
}
}
def encryptWthSpecial(key: Int): Unit = {
val pt = scala.io.StdIn readLine("Enter PT\n")
for (c <- pt) {
print(if (c.toInt >= 32 && c.toInt <= 126) {
(32 + (c.toInt - 32 + key) % 95) toChar
} else c)
}
}
def withKeySets: Unit = {
val keyArr: Array[String] = scala.io.StdIn.readLine("Enter Key sets: (ex: [(0,2), (5, 3)] )\n").replaceAll("\\(|\\)|\\[|\\]|\\s", "") split(",")
var keySet: Map[Int, Int] = Map()
for (i <- 0 to keyArr.length - 2 by 2) {
keySet += (keyArr(i).toInt -> keyArr(i + 1).toInt)
}
while (3 != (scala.io.StdIn.readLine("Choose operation:\n" +
"1) Encript\n" +
"2) Decript\n" +
"3) To return\n") toInt match {
case 1 => {
encryptWthKeySets(keySet)
}
case 2 => {
decryptWthKeySets(keySet)
}
case _ => {
3
}
})) println("\n___________")
}
def encryptWthKeySets(keySet: Map[Int, Int]) = {
val pt = scala.io.StdIn readLine("Enter PT\n")
var key: Int = keySet(0)
for (i <- 0 to pt.length - 1) {
val c = pt charAt(i)
print(if (c.toInt >= 65 && c.toInt <= 122) {
(65 + (c.toInt - 65 + key) % 58) toChar
} else c)
if (keySet contains(i)) key = keySet(i)
}
}
def decryptWthKeySets(keySet: Map[Int, Int]) = {
val cipher = scala.io.StdIn readLine("Enter cipher\n")
var key: Int = keySet(0)
for (i <- 0 to cipher.length - 1) {
val c = cipher charAt(i)
print(if (c.toInt >= 65 && c.toInt <= 122) {
val i1: Int = (c.toInt - 65 - key) % 58
(if (i1 >= 0) 65 + i1 else 123 + i1) toChar
} else c)
if (keySet contains(i)) key = keySet(i)
}
}
def breakWithSpecialChars = {
val dictionary = scala.io.StdIn.readLine("Enter dictionary words:\n") split(" ")
val cipher = scala.io.StdIn readLine("Enter the cipher:\n")
for (key <- 0 to 255) {
var pt = ""
for (c <- cipher) {
pt += {
val i1: Int = (c.toInt - 32 - key) % 95
(if (i1 >= 0) 32 + i1 else 127 + i1) toChar
}
}
dictionaryMatching(dictionary, pt, key)
}
}
def breakWithKeySets = {
val dictionary = scala.io.StdIn.readLine("Enter dictionary words:\n") split(" ")
val ciphers = scala.io.StdIn.readLine("Enter the cipher (should contain spaces):\n") split(" ")
for (cipher <- ciphers) {
for (key <- 0 to 255) {
var pt = ""
for (c <- cipher) {
pt += {
val i1: Int = (c.toInt - 65 - key) % 58
(if (i1 >= 0) 65 + i1 else 123 + i1) toChar
}
}
dictionaryMatching(dictionary, pt, key)
}
}
}
def dictionaryMatching(dictionary: Array[String], pt: String, key: Int) = {
for (d <- dictionary) {
if (pt.toLowerCase contains (d toLowerCase)) {
println("match:\n key:" + key + "\n pt:" + pt)
}
}
}
def discussionAnswers = {
println("The one with the key set is more secure as it could be used to add multiple layers of encryption which gonna require more computing power to attack,\n" +
"(cipher length * 255*number of sets) instead of (cipher length * 255).\n" +
"However it could be improved by applying a transposition algorithm so it take away the characteristics of the English language ")
}
}
5
1
1
1
Hello
2
Ifmmp
3
2
[(0,1)]
1
Hello
2
Ifmmp
3
3
hello
Ifmmp
4
hello
Ifmmp
0
Choose the ceaser cipher implemintion:
1) With spicial characters
2) With key sets
3) Break 1
4) Break 2
5) Show the discussion answers
0) To exit
The one with the key set is more secure as it could be used to add multiple layers of encryption which gonna require more computing power to attack,
(cipher length * 255*number of sets) instead of (cipher length * 255).
However it could be improved by applying a transposition algorithm so it take away the characteristics of the English language
___________
Choose the ceaser cipher implemintion:
1) With spicial characters
2) With key sets
3) Break 1
4) Break 2
5) Show the discussion answers
0) To exit
Enter the key
Choose operation:
1) Encript
2) Decript
3) To return
Enter PT
Ifmmp
___________
Choose operation:
1) Encript
2) Decript
3) To return
Enter cipher
Hello
___________
Choose operation:
1) Encript
2) Decript
3) To return
___________
Choose the ceaser cipher implemintion:
1) With spicial characters
2) With key sets
3) Break 1
4) Break 2
5) Show the discussion answers
0) To exit
Enter Key sets: (ex: [(0,2), (5, 3)] )
Choose operation:
1) Encript
2) Decript
3) To return
Enter PT
Ifmmp
___________
Choose operation:
1) Encript
2) Decript
3) To return
Enter cipher
Hello
___________
Choose operation:
1) Encript
2) Decript
3) To return
___________
Choose the ceaser cipher implemintion:
1) With spicial characters
2) With key sets
3) Break 1
4) Break 2
5) Show the discussion answers
0) To exit
Enter dictionary words:
Enter the cipher:
match:
key:1
pt:Hello
match:
key:96
pt:Hello
match:
key:191
pt:Hello
___________
Choose the ceaser cipher implemintion:
1) With spicial characters
2) With key sets
3) Break 1
4) Break 2
5) Show the discussion answers
0) To exit
Enter dictionary words:
Enter the cipher (should contain spaces):
match:
key:1
pt:Hello
match:
key:59
pt:Hello
match:
key:117
pt:Hello
match:
key:175
pt:Hello
match:
key:233
pt:Hello
___________
Choose the ceaser cipher implemintion:
1) With spicial characters
2) With key sets
3) Break 1
4) Break 2
5) Show the discussion answers
0) To exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment