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: 
36: 
37: 
38: 
39: 
40: 
(* Affine Cipher - origional in python from http://inventwithpython.com/codebreaker (BSD Licensed) *)

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

let flip f a b = f b a

let p a b (message : string) (S : string) f d f' d' =
  
  message 
  |> Seq.map (fun c -> 
     S.[ S.IndexOf c 
         |> flip f  d 
         |>      f' d' 
         |> flip (%) S.Length]
         |> string)
  |> String.concat ""
  
let F M a b m =             
  let S = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  let x = p a b m S
  let i = findModInverse a S.Length
  
  match M with
  | "encrypt" -> x ( * ) a ( + ) b
  | "decrypt" -> x ( - ) b ( * ) i
  | _       -> "Invalid Mode, modes = encrypt/decrypt"

(* constraints: 
  a <> 1, 
  b <> 0, 
  gcd a 26 = 1,   
  m is uppercase or S is lowercase.
*)

F "encrypt" 1 2 "TEST" = "VGUV"
F "decrypt" 1 2 (F "encrypt" 1 2 "TEST") = "TEST"
 
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 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 p : a:'a -> b:'b -> message:string -> S:string -> f:(int -> 'c -> 'd) -> d:'c -> f':('e -> 'd -> int) -> d':'e -> string

Full name: Script.p
val a : 'a
val b : 'b
val message : 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 S : string
val f : (int -> 'c -> 'd)
val d : 'c
val f' : ('e -> 'd -> int)
val d' : 'e
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val c : 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
property System.String.Length: int
module String

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

Full name: Microsoft.FSharp.Core.String.concat
val F : M:string -> a:int -> b:int -> m:string -> string

Full name: Script.F
val M : string
val m : string
val x : ((int -> int -> int) -> int -> (int -> int -> int) -> int -> string)
val i : int
Next Version Raw view Test code New version

More information

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