fork(1) download
  1. /*
  2. * Created by ahmedengu.
  3. */
  4.  
  5. object Main extends App {
  6.  
  7.  
  8. while (0 != (scala.io.StdIn.readLine(
  9. "Choose the ceaser cipher implemintion:\n" +
  10. "1) With spicial characters \n" +
  11. "2) With key sets\n" +
  12. "3) Break 1\n" +
  13. "4) Break 2\n" +
  14. "5) Show the discussion answers\n" +
  15. "0) To exit\n") toInt match {
  16. case 1 => {
  17. withSpicialChars
  18. 1
  19. }
  20. case 2 => {
  21. withKeySets
  22. 2
  23. }
  24. case 3 => {
  25. breakWithSpecialChars
  26. 3
  27. }
  28. case 4 => {
  29. breakWithKeySets
  30. 4
  31. }
  32. case 5 => {
  33. discussionAnswers
  34. 5
  35. }
  36. case _ => {
  37. 0
  38. }
  39. })) println("\n___________")
  40.  
  41.  
  42. def withSpicialChars: Unit = {
  43. val key = scala.io.StdIn.readLine("Enter the key\n") toInt
  44.  
  45. while (3 != (scala.io.StdIn.readLine("Choose operation:\n" +
  46. "1) Encript\n" +
  47. "2) Decript\n" +
  48. "3) To return\n") toInt match {
  49. case 1 => {
  50. encryptWthSpecial(key)
  51. }
  52. case 2 => {
  53. decryptWthSpecial(key)
  54. }
  55. case _ => {
  56. 3
  57. }
  58. })) println("\n___________")
  59. }
  60.  
  61. def decryptWthSpecial(key: Int): Unit = {
  62. val cipher = scala.io.StdIn readLine("Enter cipher\n")
  63. for (c <- cipher) {
  64. print(if (c.toInt >= 32 && c.toInt <= 126) {
  65. val i1: Int = (c.toInt - 32 - key) % 95
  66. (if (i1 >= 0) 32 + i1 else 127 + i1) toChar
  67. } else c)
  68. }
  69. }
  70.  
  71. def encryptWthSpecial(key: Int): Unit = {
  72. val pt = scala.io.StdIn readLine("Enter PT\n")
  73. for (c <- pt) {
  74. print(if (c.toInt >= 32 && c.toInt <= 126) {
  75. (32 + (c.toInt - 32 + key) % 95) toChar
  76. } else c)
  77. }
  78. }
  79.  
  80.  
  81. def withKeySets: Unit = {
  82. val keyArr: Array[String] = scala.io.StdIn.readLine("Enter Key sets: (ex: [(0,2), (5, 3)] )\n").replaceAll("\\(|\\)|\\[|\\]|\\s", "") split(",")
  83. var keySet: Map[Int, Int] = Map()
  84. for (i <- 0 to keyArr.length - 2 by 2) {
  85. keySet += (keyArr(i).toInt -> keyArr(i + 1).toInt)
  86. }
  87. while (3 != (scala.io.StdIn.readLine("Choose operation:\n" +
  88. "1) Encript\n" +
  89. "2) Decript\n" +
  90. "3) To return\n") toInt match {
  91. case 1 => {
  92. encryptWthKeySets(keySet)
  93. }
  94. case 2 => {
  95. decryptWthKeySets(keySet)
  96. }
  97. case _ => {
  98. 3
  99. }
  100. })) println("\n___________")
  101. }
  102.  
  103. def encryptWthKeySets(keySet: Map[Int, Int]) = {
  104. val pt = scala.io.StdIn readLine("Enter PT\n")
  105. var key: Int = keySet(0)
  106. for (i <- 0 to pt.length - 1) {
  107. val c = pt charAt(i)
  108. print(if (c.toInt >= 65 && c.toInt <= 122) {
  109. (65 + (c.toInt - 65 + key) % 58) toChar
  110. } else c)
  111. if (keySet contains(i)) key = keySet(i)
  112. }
  113. }
  114.  
  115. def decryptWthKeySets(keySet: Map[Int, Int]) = {
  116. val cipher = scala.io.StdIn readLine("Enter cipher\n")
  117. var key: Int = keySet(0)
  118. for (i <- 0 to cipher.length - 1) {
  119. val c = cipher charAt(i)
  120. print(if (c.toInt >= 65 && c.toInt <= 122) {
  121. val i1: Int = (c.toInt - 65 - key) % 58
  122. (if (i1 >= 0) 65 + i1 else 123 + i1) toChar
  123. } else c)
  124. if (keySet contains(i)) key = keySet(i)
  125.  
  126. }
  127. }
  128.  
  129.  
  130. def breakWithSpecialChars = {
  131. val dictionary = scala.io.StdIn.readLine("Enter dictionary words:\n") split(" ")
  132. val cipher = scala.io.StdIn readLine("Enter the cipher:\n")
  133.  
  134. for (key <- 0 to 255) {
  135. var pt = ""
  136. for (c <- cipher) {
  137. pt += {
  138. val i1: Int = (c.toInt - 32 - key) % 95
  139. (if (i1 >= 0) 32 + i1 else 127 + i1) toChar
  140. }
  141. }
  142. dictionaryMatching(dictionary, pt, key)
  143. }
  144.  
  145. }
  146.  
  147. def breakWithKeySets = {
  148. val dictionary = scala.io.StdIn.readLine("Enter dictionary words:\n") split(" ")
  149. val ciphers = scala.io.StdIn.readLine("Enter the cipher (should contain spaces):\n") split(" ")
  150.  
  151. for (cipher <- ciphers) {
  152. for (key <- 0 to 255) {
  153. var pt = ""
  154. for (c <- cipher) {
  155. pt += {
  156. val i1: Int = (c.toInt - 65 - key) % 58
  157. (if (i1 >= 0) 65 + i1 else 123 + i1) toChar
  158. }
  159. }
  160. dictionaryMatching(dictionary, pt, key)
  161. }
  162. }
  163.  
  164. }
  165.  
  166. def dictionaryMatching(dictionary: Array[String], pt: String, key: Int) = {
  167. for (d <- dictionary) {
  168. if (pt.toLowerCase contains (d toLowerCase)) {
  169. println("match:\n key:" + key + "\n pt:" + pt)
  170. }
  171. }
  172. }
  173.  
  174. def discussionAnswers = {
  175.  
  176. 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" +
  177. "(cipher length * 255*number of sets) instead of (cipher length * 255).\n" +
  178. "However it could be improved by applying a transposition algorithm so it take away the characteristics of the English language ")
  179. }
  180. }
Success #stdin #stdout 0.48s 323456KB
stdin
5
1
1
1
Hello
2
Ifmmp
3
2
[(0,1)]
1
Hello
2
Ifmmp
3
3
hello
Ifmmp
4
hello
Ifmmp
0
stdout
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