Giter Club home page Giter Club logo

rsa_computer's Introduction

rsa_computer

msp430 based rsa computer

key generation

  • Step 1 Choose two prime numbers, P & Q.
P = 409
Q = 983
  • Step 2 Multiply them to get N, one half of your public key.
N = P * Q
N = 409 * 983
N = 402047
  • Step 3 Compute the totient of N, PHI.
PHI = (P-1) * (Q-1)
PHI = 409-1 * 983-1
PHI = 408 * 982
PHI = 400656
  • Step 4 Compute E, the second half of the public key, by choosing a small prime number that does not divide PHI evenly.
check 2, 400656 / 2 = 200328 [X]
check 3, 400656 / 3 = 133552 [X]
check 5, 400656 / 5 = 80131.2 - bingo!

E = 5
  • Step 5 Compute D, the secret key.
D = e^-1 ( mod PHI )

A numeric solution lives in the form of this function.

int modular_multiplicative_inverse(E, PHI){
  int D  = 0;
  int nt = 1;
  int r = PHI;
  int nr = E % PHI;

  // correct values to acceptable input range
  if (PHI < 0){
    PHI = -PHI;
  }
  if (E < 0){
    E = PHI - (-E % PHI);
  }

  // perform operations to calculate the MMI
  int quot;
  int tmp;
  while (nr != 0) {
    quot = (r/nr) | 0;
    tmp = nt;  
    nt = D - quot*nt;  
    D = tmp;
    tmp = nr;  
    nr = r - quot*nr;  
    r = tmp;
  }
  if (r > 1) { return -1; }
  if (D < 0) { D += PHI; }
  return D;
}
D = modular_multiplicative_inverse(5, 400656)
D = 320525

encrypt

Choose an integer to represent the bytes of your plain-text message, PT.

PT = 30

Create the cipher-text (CT) from the plain-text (PT) and the public key (E, N)

CT = PT^E mod N
CT = 30^5 mod 402047
CT = 117180

wolfram alpha link to results of this calculation

decrypt

The decryption routine is similar but involves much larger numbers.

PT = CT^D mod N
PT = 117180^320525 mod 402047

117180^320525 cannot fit into memory. It is a 1.6 million digit long number.

There is a memory efficient algorithm to perform the CT^D mod N calculation.

// memory efficient modular exponentiation for MSP430
//
// calculate PT, where - (CT^E) mod N = PT
// CT is the ciphertext
// E is the secret key
// N the the public key

//
// (CT^E) could be huge, thousands of digits, so we use this algorithm to calculate the value of PT
// after performing 'E' routines.  In this case 320525 iterations of the while loop are performed.

// this is a memory efficient encrypt and decrypt operations

#include <msp430.h>				
#define BIT0 (0x0001)
#define BIT6 (0x0040)

long long modular_exponentiation(long long cipher_text, long long exponent, long long publickey);

long long plaintext = 30;
long long ciphertext = 0;
long long exponent = 5;
long long publickey = 402047;
long long privatekey = 320525;
long long decrypted_plaintext = 0;

void main(void) {
	WDTCTL = WDTPW | WDTHOLD;		// Stop watchdog timer
	P1DIR |= (BIT0 | BIT6);					// Set P1.0 to output direction
	P1OUT = 0;

  // smaller test values
  // exponent = 17
  // public key = 3233
  // private key = 2753
  // plaintext = 65
  // ciphertext = 2790

  // larger
  // exponent = 5
  // public key = 402047
  // private key = 320525
  // plaintext = 30
  // ciphertext = 177180

  // encrypt

  // (plaintext^exponent) mod publickey
  // (PT^E) mod N
  ciphertext = modular_exponentiation(plaintext, exponent, publickey);

  // decrypt
  // (ciphertext^secretkey) mod publickey
  // (CT^D) mod N
  decrypted_plaintext = modular_exponentiation(ciphertext, privatekey, publickey);

  if(plaintext == decrypted_plaintext){
    // set the GREEN led
    P1OUT = BIT6;
  } else {
    // set the RED led
    P1OUT = BIT0;
  }

	//return 0;
}


long long modular_exponentiation (long long cipher_text, long long exponent, long long publickey){
  long long c = 1;
  long long e_prime = 0; // our counter variable
  while(e_prime < exponent){
	  e_prime = e_prime + 1;
	  if(e_prime % 2){
	   P1OUT = BIT0;
	  } else {
	   P1OUT = BIT6;
	  }
	  c = c * cipher_text;
	  c = c % publickey;
  }
  // c is the plaintext
  P1OUT = 0;
  return c;
}

rsa_computer's People

Contributors

billautomata avatar

Watchers

James Cloos avatar  avatar  avatar

rsa_computer's Issues

hilarious numbers

On an msp430g2553:

An 8 bit key can be cracked in ~100ms.
A 16 bit key can be cracked in 1.5 seconds.
A 32 bit key can be cracked in 900 minutes.

To decrypt a value using an 8 bit key, takes 1.5 minutes.
To decrypt a value using a 16 bit key, takes 2-100 days.
To decrypt a value using a 32 bit key takes ????????

Fun keys to crack, can't be used in the encrypt / decrypt operations. To decrypt you need to perform N operations where N = the private key value. The private key for two 8 bit primes is >300,000.

try diffie hellman instead of rsa

https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange

Key generation is easier, you just pick a number. It doesn't have to be prime, just large. There is also a pre-agreed upon group of numbers used in the equation. Published as a spec.

// the agreed upon components
p = 23
g = 5
my_secret_number = 6;
my_public_number = (g ^ secret) mod p
my_public_number = (5 ^ 6) mod 23
my_public_number = 8

I then publish, (8, 5, 23) as my key.

Then NSA then needs to find the value for secret in the equation.

8 = ( 5 ^ x ) mod 23
// solve for x

It does that by brute force, performing the equation with values for x to see if they equal 8.

// cracking the secret number, using only the public parts
x=1, 5^1 mod 23 = 5
x=2, 5^2 mod 23 = 2
x=3, 5^3 mod 23 = 10
x=4, 5^4 mod 23 = 4
x=5, 5^5 mod 23 = 20
x=6, 5^6 mod 23 = 8
your_secret_number = 15;
your_public_number = (g ^ secret) mod p
your_public_number = (5 ^ 15) mod 23
your_public_number = 19

You then publish, (19, 5, 23) as your public key.

You take my public key (8, 5, 23) and combine it with your secret number 15 to create

(my_public_key[0] ^ your_secret_number) mod my_public_key[2]
(8 ^ 15) mod 23 = 2
2, is our secret.

I take your public key (19, 5, 23) and combine it with my secret number 6 to create

(your_public_key[0] ^ my_secret_number) mod my_public_key[2]
(19 ^ 6) mod 23 = 2
2, is our secret.
// all combined
(my_public_key ^ your_secret_number) mod 23 = (your_public_key ^ my_secret_number) mod 23

We do not encrypt with the keys, we arrive at the same value together, using the other persons public key and our private key. We use that value to encrypt the data symmetrically.

screen shot 2016-02-19 at 9 35 08 pm

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.