# Simple Public Key Encrytion

Made on 8-13-2014 Written in Javascript## Generate Keys

Common modulo value:Generate!

## Encrypt

### Number

Number:Public key:

Common modulo value:

Encrypt!

### Single Byte

Letter:Public key:

Common modulo value:

Encrypt!

### Whole Message (Not yet implimented)

## Decrypt

### Number

Encrypted number:Private key:

Common modulo value:

decrypt!

### Single Byte

Encrypted letter:Private key:

Common modulo value:

decrypt!

### Whole Message (Not yet implimented)

## The Algorithm

This method uses asymetric public key encryption.### Generating your keys

- Get a modulo value as '
`m`' (For example: 256)//In Javascript: var m = 256;

- Generate a random number from 0 to
`m`-1 as`priv`this is the "Private Key", The private key is what you use to decrypt the data (For example: from [0-255] so, 123)//In Javascript: var priv = Math.floor(Math.random() * (m-1)); // As a function (Using last two steps) function calc_private(modulo /* m */){ return Math.floor(Math.random() * (modulo-1)); }

- Calulate

, This is the public key, what people use to encrypt the data to send to you, (For example:`pub`=`m`-`priv``133 = 256 - 123`

)

Imagine that a 3rd party does not know how to compute the public key from the private key, but in this case anyone can calculate your private key by doing:

, but this algorithm is definitely`priv`=`m`-`pub`**not**safe (because ANYONE can get the private key from the modulo and public key, where the private key should always be very secret).//In Javascript: var pub = m - priv; // As a function function calc_public(modulo /* m */, priv){ return modulo - priv; }

### Something being sent to you (encrypting)

- For someone to send an encrypted byte to you (the byte being
`b`), they would compute`(`

,`b`+`pub`) %`m`= e`e`being the encrypted byte (For example: I want to send you the letter "L" or ascii value 76, I would compute`(76 + 133) % 256 = 209`

)//In Javascript: var e = (b + pub) % m; // As a function function encrypt(byte /* b */, pub, modulo /* m */){ return (byte + pub) % modulo; }

### Something being sent to you (decrypting)

- For you to decrypt an encrypted byte you would calculate
`(`

(for example, you would compute`e`+`priv`) %`m`= b`(209 + 123) % 256 = 76`

)//In Javascript: var b = (e + priv) % m; // As a function function decrypt(enc_byte /* e */, priv, modulo /* m */){ return (enc_byte + priv) % modulo; }