Advertisement
Guest User

Untitled

a guest
Oct 5th, 2015
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.89 KB | None | 0 0
  1.   type VigenereCipherFunction = (Int, Int) => Int
  2.  
  3.   override def encrypt(plain: String, key: String, abc: ABC = this.alphabet) : String = {
  4.     val func: VigenereCipherFunction = (x,y) => (x + y) % abc.length
  5.     work(plain, key, abc, func)
  6.   }
  7.  
  8.   override def decrypt(cipher: String, key: String, abc: ABC = this.alphabet)  : String = {
  9.     val func: VigenereCipherFunction = (x,y) => (x - y + abc.length) % abc.length
  10.     work(cipher, key, abc, func)
  11.   }
  12.  
  13.   private def work(text: String, key: String, abc: ABC, func: VigenereCipherFunction): String = {
  14.  
  15.     val it = Stream.continually(key.toLowerCase).flatten.iterator
  16.  
  17.     text.toLowerCase
  18.       .map((c) => {
  19.         val tInd = abc.indexWhere((x) => x.equals(c))
  20.         val next = it.next
  21.         val kInd = abc.indexWhere((x) => x.equals(next))
  22.  
  23.         func(tInd, kInd)
  24.       })
  25.       .map((c) => abc(c))
  26.       .mkString
  27.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement