Skip to content

Instantly share code, notes, and snippets.

@ecounysis
Created November 10, 2014 20:36
Show Gist options
  • Save ecounysis/6557c084505daefa269e to your computer and use it in GitHub Desktop.
Save ecounysis/6557c084505daefa269e to your computer and use it in GitHub Desktop.
String obfuscation
namespace Obfuscation
// very weak encryption
// uses a "one-time pad" that is used over and over
// http://en.wikipedia.org/wiki/One-time_pad
module Text =
type Operation =
| Encryption
| Decryption
let mutable Pad = "n6CcISLBHCTjMDahYCcQFNtBVVrDsxY3eXbEbyfveh6PA8rHbbhTMipPD07na8IJ"
let strVals = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=; "
let getShift (chr:char) :int =
match strVals.IndexOf(chr) with
| -1 -> 0
| x -> x
let intVal (chr:string) :int =
strVals.IndexOf(chr)
let rotate (c:int) (chr:char) :char =
let idx = strVals.IndexOf(chr)
let idx' = (idx + c) % strVals.Length
strVals.Substring(idx',1).ToCharArray().[0]
let toStr v =
v.ToString()
let strAppend (str1:string) (str2:string) =
str1 + str2
let encryptChr (shift:int) (value:char) :char =
if (strVals.Contains(value.ToString())) then (rotate shift value)
else value
let rec map2 fn ls1 ls2 =
match (ls1, ls2) with
| ([],_) | (_,[]) -> []
| ((x::xs), (y::ys)) -> fn x y :: map2 fn xs ys
let operate (proc:Operation) (str:string) =
let offsetFunc proc value =
match proc with
| Encryption -> value
| Decryption -> strVals.Length - value
let arr = Array.toList (str.ToCharArray())
let times = (str.Length / Pad.Length) + 1
let shiftSeq =
let passkeyArray = Pad.ToCharArray()
[ for j in 0 .. times do
for i in 0 .. ((Array.length passkeyArray) - 1) do
yield (offsetFunc proc (getShift passkeyArray.[i])) ]
map2 encryptChr shiftSeq arr
|> List.map toStr
|> List.reduce strAppend
let public Ob (str:string) :string =
operate Encryption str
let public DeOb (str:string) :string =
operate Decryption str
namespace Obfuscation
module Text =
val mutable Pad : string
val Ob : str:string -> string
val DeOb : str:string -> string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment