Feb 18, 2009

测试 syntaxhighlighter


import java.math.BigInteger;
import java.security.SecureRandom;

public class Rsa {
private BigInteger n, d, e;

public Rsa(int bitlen)
{
SecureRandom r = new SecureRandom();
BigInteger p = new BigInteger(bitlen/2, 100, r);
BigInteger q = new BigInteger(bitlen/2, 100, r);
n = p.multiply(q);
BigInteger m = (p.subtract(BigInteger.ONE )).multiply(q.subtract(BigInteger.ONE ));
e = new BigInteger("3");
while(m.gcd(e).intValue()>1)
e = e.add(new BigInteger("2"));
d = e.modInverse(m);

System.out.println("P: "+p);
System.out.println("q: "+q);
System.out.println("m: "+m);
System.out.println("n: "+n);
System.out.println("d: "+d);
System.out.println("e: "+e);
}

public BigInteger encrypt(BigInteger message)
{
return message.modPow(e, n);
}

public BigInteger decrypt(BigInteger message)
{
return message.modPow(d, n);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BigInteger message = new BigInteger("1234567890");
//BigInteger ciphertext;
//System.out.println(message.intValue());

Rsa rsa = new Rsa(100);
System.out.println(rsa.encrypt(message)+"\n");
System.out.println(rsa.decrypt(rsa.encrypt(message)).intValue());
}

}

1 comment: