# Simple Public Key Encrytion

Made on 8-13-2014 Written in Javascript
Provided for educational and entertainment use only.

## Generate Keys

Common modulo value: I recommend a more than 128 value.

## Encrypt

### Number

Number:
Public key:
Common modulo value:

### Single Byte

Letter:
Public key:
Common modulo value:

## Decrypt

### Number

Encrypted number:
Private key:
Common modulo value:

### Single Byte

Encrypted letter:
Private key:
Common modulo value:

## The Algorithm

This method uses asymetric public key encryption.

1. Get a modulo value as 'm' (For example: 256)
```//In Javascript:
var m = 256;```
2. 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));
}```
3. Calulate `pub = m - priv`, This is the public key, what people use to encrypt the data to send to you, (For example: `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: `priv = m - pub`, but this algorithm is definitely 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)

1. 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)

1. For you to decrypt an encrypted byte you would calculate `(e + priv) % m = b` (for example, you would compute`(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;
}```