4 people like it.

Affine Cipher

simple affineCipher translated from https://github.com/asweigart/codebreaker/blob/master/affineCipher.py not sure if actually correct. I updated the below to make it easier to understand because I was bored, made some more changes, now pass in lambda as function to pxn instead of individual functions, as suggested by rwbarton for my haskell version.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
(* Affine Cipher - origional in python from http://inventwithpython.com/codebreaker (BSD Licensed) *)
let flip f a b = f b a

let findModInverse a m =
  [0..m] 
  |> Seq.filter (fun b -> a * b % m = 1)  
  |> Seq.head        

let findIndex (s : string) (c : char) =
  s.IndexOf c

let px (s : string) f x g y =
  Seq.map (findIndex s
     >> flip   f x 
     >>        g y 
     >> flip   (%) s.Length
     >> flip Seq.nth s
     >> string)

type Mode = Encrypt | Decrypt

let f M a b =             
  let x = px "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  let i = findModInverse a 26
  
  match M with
    Encrypt -> x ( * ) a ( + ) b
  | Decrypt -> x ( - ) b ( * ) i

let enc : string -> string = f Encrypt 1 2 >> String.concat ""

let dec : string -> string = f Decrypt 1 2 >> String.concat ""

assert (enc "TEST" = "VGUV")
assert (dec (enc "TEST") = "TEST")
val flip : f:('a -> 'b -> 'c) -> a:'b -> b:'a -> 'c

Full name: Script.flip
val f : ('a -> 'b -> 'c)
val a : 'b
val b : 'a
val findModInverse : a:int -> m:int -> int

Full name: Script.findModInverse
val a : int
val m : int
module Seq

from Microsoft.FSharp.Collections
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.filter
val b : int
val head : source:seq<'T> -> 'T

Full name: Microsoft.FSharp.Collections.Seq.head
val findIndex : s:string -> c:char -> int

Full name: Script.findIndex
val s : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val c : char
Multiple items
val char : value:'T -> char (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.char

--------------------
type char = System.Char

Full name: Microsoft.FSharp.Core.char
System.String.IndexOf(value: string) : int
System.String.IndexOf(value: char) : int
System.String.IndexOf(value: string, comparisonType: System.StringComparison) : int
System.String.IndexOf(value: string, startIndex: int) : int
System.String.IndexOf(value: char, startIndex: int) : int
System.String.IndexOf(value: string, startIndex: int, comparisonType: System.StringComparison) : int
System.String.IndexOf(value: string, startIndex: int, count: int) : int
System.String.IndexOf(value: char, startIndex: int, count: int) : int
System.String.IndexOf(value: string, startIndex: int, count: int, comparisonType: System.StringComparison) : int
val px : s:string -> f:(int -> 'a -> 'b) -> x:'a -> g:('c -> 'b -> int) -> y:'c -> (seq<char> -> seq<string>)

Full name: Script.px
val f : (int -> 'a -> 'b)
val x : 'a
val g : ('c -> 'b -> int)
val y : 'c
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
property System.String.Length: int
val nth : index:int -> source:seq<'T> -> 'T

Full name: Microsoft.FSharp.Collections.Seq.nth
type Mode =
  | Encrypt
  | Decrypt

Full name: Script.Mode
union case Mode.Encrypt: Mode
union case Mode.Decrypt: Mode
val f : M:Mode -> a:int -> b:int -> (seq<char> -> seq<string>)

Full name: Script.f
val M : Mode
val x : ((int -> int -> int) -> int -> (int -> int -> int) -> int -> seq<char> -> seq<string>)
val i : int
val enc : (string -> string)

Full name: Script.enc
module String

from Microsoft.FSharp.Core
val concat : sep:string -> strings:seq<string> -> string

Full name: Microsoft.FSharp.Core.String.concat
val dec : (string -> string)

Full name: Script.dec

More information

Link:http://fssnip.net/dy
Posted:12 years ago
Author:david klein
Tags: affine