Thursday, April 10, 2008

Cryptography: RSA : Java Implementation

hi all,
RSA is a well known encryption algorithm for encryption and decryption
this is public key cryptography

// choose tho prime number p and q
BigInteger p,q,n,phi;

p = prime(16);
q = prime(16);

// calculate modulus: n and totient: phi
n= p.multiply(q);
BigInteger temp1,temp2;
temp1=p.subtract(BigInteger.ONE);
temp2=q.subtract(BigInteger.ONE);
phi = (temp1).
multiply(temp2);

// choose a prime number and give it name private key.
// and using private key then compute public key

// actually public = private-1mod(phi)
BigInteger private, public;

private = prime(128);

public = private.modInverse(phi);

// now we have both keys private key and public key

//public key is known to anyone and is used to encrypt data
// private key is not disclosed to anyone.. and generally used for decryption



// encryption
BigInteger message;
BigInteger cypherText;

cypherText = message.modPow(public,n);
// cyphertext is encrypted message

// decryption
message =
cypherText .modPow(private,n);

more about cryptography you can get from wikipedia - RSA

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.