/** * myRSA.java * * A simple implementation of the RSA algorithm. * * Example usage: * * myRSA rsa = new myRSA(100); * rsa.setPublicKey(1,2); * rsa.setPrivateExponent(3); * rsa.encode(); * BigInteger result = rsa.getResult(); * System.out.println('The encoded value is: ' + result); * rsa.decode(); * BigInteger result2 = rsa.getResult(); * System.out.println('The decoded value is: ' + result2); * * @see http://www.rsasecurity.com/rsalabs/node.asp?id=2125 * @author Nubsauce */ import java.lang.Object; import java.math.BigInteger; public class myRSA { /** * The public key. * @var integer */ private int publicKey; /** * The second part of the public key. * @var integer */ private int keyPart; /** * The private exponent. * @var integer */ private int pExponent; /** * The value that is to be encoded/decoded. * @var integer */ private BigInteger rsaValue; /** * Constructor for objects of class ownversion. * Sets the value that is to be encoded/decoded. * * @param integer x The value that is to be manipulated. * @return void */ public myRSA(int x) { rsaValue = x; } /** * Sets the public key. * * @param integer firstPart The first part of the public key. * @param integer secondPart The Second part of the public key. * @return void */ public void setPublicKey(int firstPart, int secondPart) { publicKey = firstPart; keyPart = secondPart; } /** * Sets the private exponent. * @param integer exp The private exponent. * @return void */ public void setPrivateExponent(int exp) { pExponent = exp; } /** * Encodes the stored value using an RSA algorithm. * * @return void */ public void encode() { BigInteger y = BigInteger.valueOf(rsaValue); BigInteger e1 = BigInteger.valueOf(keyPart); BigInteger publicKey1 = BigInteger.valueOf(publicKey); BigInteger result = y.modPow(e1, publicKey1); rsaValue = result; } /** * Decodes the stored value using an RSA algorithm. * * @return void */ public void decode() { BigInteger z1 = BigInteger.valueOf(rsaValue); BigInteger d1 = BigInteger.valueOf(privateExponent); BigInteger publicKey1 = BigInteger.valueOf(publicKey); BigInteger result = z1.modPow(d1, publickey1); rsaValue = result; } /** * Returns the processed value. * * @return integer */ public BigInteger getResult() { return rsaValue; } }