maximwebb / cryptography

A set of tools for breaking classical ciphers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cryptography

A set of tools for breaking classical ciphers. This will include Monoalphabetic substitutions, vigenere encrpytion/decryption, string manipulation tools and more.

General-purpose Methods

removePunc(str)

Takes a string str, converts it to lowercase and strips out punctuation and spaces.

Example:

removePunc("Hello, World!");
//"helloworld"

splitString(str, n)

Takes a string str, removes punctuation and inserts a space every n letters.

Example:

splitString("Matrices are a type of data structure.", 4);
//"matr ices area type ofda tast ruct ure"

extractStrings(str, n)

Takes every n-th letter in a string str and generates a new string, then takes every (n+1)-th string, and generates another string and so on. Returns all n strings as an array of strings.

Example:

extractStrings("Apple orange pears lemons", 3);
//["alrgesms", "peaealo", "ponpren"]

cloneArr(arr)

Performs a deep clone on an array

Example:

cloneArr([1, 2, 4, 7]);
//[1, 2, 4, 7]

permuteArr(arr)

Recursively generates every permutation of an array, and returns the list of permutations as an array of arrays.

Exmaple:

permuteArray(['a', 'b', 'c']);
//[
  //["a", "b", "c"],
  //["b", "a", "c"],
  //["b", "c", "a"],
  //["a", "c", "b"],
  //["c", "a", "b"],
  //["c", "b", "a"]
//]

Cipher Encryption/Decryption Methods

caesarShift(str, type, rot)

Encrypts/decrypts (depending on type) a string str using a Caesar Shift of rot shifts. Preserves formatting.

Example:

caesarShift("Lorem ipsum dolor sit amet, consectetur adipiscing elit", "encrypt", 2);
//"Nqtgo kruwo fqnqt ukv cogv, eqpugevgvwt cfkrkuekpi gnkv"

monoSub(str, type, key)

Encrypts/decrypts a string str using a monoalphabetic substitution cipher, using alphabet key. Preserves formatting.

Example:

monoSub("Qdn fls zsk qdn bzs es qdn btts.", "decrypt", "zijknopdelmcbstughfqlmryxwav");
//"The sun and the man in the moon."

viginere(str, type, key)

Encrypts/decrypts a string str using the viginere cipher, with keyword key. Preserves formatting.

Example:

viginere("Attack the fort at noon.", "encrypt", "sword");
//"Sphrfc pvv ignh rw fkce."

playfair(str, key)

Encrypts a string str using the playfair cipher, with the grid generated by keyword key. Does not preserve formatting.

Example:

playfair("Elementary, my dear Watson.", "phone");
//"nmfupeqdtwlzfnbqvbutne"

columnarTranspose(inputStr, type, key)

Encrypts/decryption a string inputStr, using a columnar transpose. Does not preserve formatting.

Example:

columnarTranspose("applebanana", "encrypt", "cat")
//"penaalanpba"

Cipher Analysis Methods

freqAnalysis(str)

Performs a frequency analysis on a string str, returning the data as an ordered array of letter-frequencies, with greatest frequency first.

Example:

freqAnalysis("There are twenty-six letters in the English Language.");
//[["e", 20.45], ["t", 13.64], ["n", 9.09], ["a", 6.82], ["g", 6.82], ["h", 6.82], ...]

nGramAnalysis(input, len, outputNum, string)

Performs an n-gram analysis on a string str, where n = len. The number of results returned can be specified by outputNum, and if the input is a string (ie. not an array), string should be set to true.

Example:

nGramAnalysis("Journey to the centre of the earth", 2, 6, true);
//[th, 11.11], [he, 7.41], [nt: 3.7], [ec, 3.7], [ee, 3.7], [en, 3.7]

printFreqArr(arr)

Formats freqAnalysis/nGramAnalysis output arr as a string.

Example:

printFreqArr(freqAnalysis("There are twenty-six letters in the English Language."));
//"e: 20.45%, t: 13.64%, n: 9.09%, a: 6.82%, g: 6.82%, h: 6.82%, ..."

substringGaps(str, substr_length)

Searches string str for substrings of length substr_length, and returns a sorted array of all the distances between repeated substrings.

Example:

substringGaps("hesnotthemessiahhesaverynaughtboy", 3);
//[16]

determineShift(arr)

Finds the caesar shift ROT with the closest match to a frequency analysis output.

Example:

determineShift(freqAnalysis('Mrxuqhb wr wkh fhqwuh ri wkh hduwk'));
//3

solveCaesar(str)

Completely solves a Caesar shift cipher, given ciphertext only.

Example:

solveCaesar('Wklv lv zkdw zh gr khuh');
//This is what we do here

solveViginere(str)

Completely solves a Viginere cipher, given ciphertext only. More reliable on longer texts.

Example:

solveViginere("Yvutu avu tzml mm kq nidv avu i wqymccc moz jwjcmju? Pt’a hwptm jktptv, tlatca: Kocsnl ywlt yabv qm fiznbrm. Pqb azv voivbkug ww hhitltl aa kjl evvof on jwjcmju. Iub zv psv’k ca atc. Avu krp ie lzujocicnel sa maqcwye wi avu krp seiip mrwd ka, sw xq hhmrf hnl dcre uzuaasvu. Tasv csl gfw jav. Sgjacjg yeuvoiez kjht’a njlrm pqb wqcn mivu ubckvuz.");

//Would you like me to give you a formula for success? It’s quite simple, really: Double your rate of failure. You are thinking of failure as the enemy of success. But it isn’t at all. You can be discouraged by failure or you can learn from it, so go ahead and make mistakes. Make all you can. Because remember that’s where you will find success.

About

A set of tools for breaking classical ciphers

License:MIT License


Languages

Language:JavaScript 73.3%Language:HTML 15.7%Language:CSS 11.0%