Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type VigenereCipherFunction = (Int, Int) => Int
- override def encrypt(plain: String, key: String, abc: ABC = this.alphabet) : String = {
- val func: VigenereCipherFunction = (x,y) => (x + y) % abc.length
- work(plain, key, abc, func)
- }
- override def decrypt(cipher: String, key: String, abc: ABC = this.alphabet) : String = {
- val func: VigenereCipherFunction = (x,y) => (x - y + abc.length) % abc.length
- work(cipher, key, abc, func)
- }
- private def work(text: String, key: String, abc: ABC, func: VigenereCipherFunction): String = {
- val it = Stream.continually(key.toLowerCase).flatten.iterator
- text.toLowerCase
- .map((c) => {
- val tInd = abc.indexWhere((x) => x.equals(c))
- val next = it.next
- val kInd = abc.indexWhere((x) => x.equals(next))
- func(tInd, kInd)
- })
- .map((c) => abc(c))
- .mkString
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement