Code Monkey home page Code Monkey logo

cipher's Introduction

#This is a Cipher program using math concepts and Ruby

	A cipher comes from a simple math concept called a one-to-one relationship

	If we have a set A = {a, b, c} and set B = {0, 1, 2} we have
	A 		B
	a 		0
	b 		1
	c 		2

	If we map a to 0 and b to 1 and c to 2, we have a "one-to-one" relationship. 
	Also, as long as the relationship is one-to-one and everything in each set is 
	"mapped" to something and nothing is left out, then it is also called "onto"

	If we have a function that is "one-to-one" and "onto" it is also called "bijective"
	In math, if we have a one-to-one relationship, we can have what is called an "inverse"
	or the "mirror image" of the original.

	This is very useful in developing the concept of "cipher" and "decipher" or making 
	encrypted and decrypted messages.
	
	If we have an alphabet:
		{a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z}

	We can map it to a set of numbers:
		{0, 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}

	We then have our "one-to-one" relationship. It is also "onto" and "bijective"

	From math:  ƒ(x) = (ax + b) mod n 
				•	 ƒ is bijective iff gcd(a, n) = 1 
						where gcd is greatest common divisor (from Euclid's Theorem: a = bq + r)
				•	 If so, then ƒ–1(x) = (kx + c) mod n where ƒ(c) = 0 and 1 = ak + nm.
						where ƒ–1(x) is the inverse of ƒ(x) 

	From this we come up with a simple additive cipher

				•	 f(x) = (x + 5) mod 26
						this "encrypts" our message

				•	 ƒ–1(x) = (x – 5) mod 26
						this "decrypts" our message

	So, if we enter "hello" we get "mjqqt" as an output.  This is because 'h' is in the 7 position
				f(x) = (x + 5) mod 26
				f(7) = (7 + 5) mod 26
				f(7) = 12
				12 gets mapped to 'm'

	Decrypting our message goes like this:
				ƒ–1(x) = (x – 5) mod 26
				ƒ–1(12) = (12 – 5) mod 2
				ƒ–1(12) = 7 mod 26
				ƒ–1(12) = 7
				7 gets mapped back to 'h'

	Here is an example to my program using the same function:
	
		[22] pry(main)> scramble("Hey I am hungry and want to get some vegan food to eat")
		=> "mjd n fr mzslwd fsi bfsy yt ljy xtrj ajlfs ktti yt jfy"
		[23] pry(main)> descramble("mjd n fr mzslwd fsi bfsy yt ljy xtrj ajlfs ktti yt jfy")
		=> "hey i am hungry and want to get some vegan food to eat"

cipher's People

Contributors

spencerdavis2000 avatar

Stargazers

Roman avatar

Watchers

James Cloos avatar  avatar

Forkers

hmalueg

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.