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

/**
 * Project RSA Algorithm.
 * Copyright Michał Szczygieł.
 * Created at Feb 24, 2014.
 */

/**
 * This class represents implementation of RSA encryption algorithm.
 * 
 * @author Michał Szczygieł <michal.szczygiel@wp.pl>
 * 
 */
public class RSA implements Encryption {

    /**
     * The base for the operation.
     */
    private static final Integer BASE = 27;

    /**
     * Field keeps the value of the multiplication of numbers p and q.
     */
    private BigInteger n;

    /**
     * Field stores private key for RSA encryption algorithm.
     */
    private BigInteger privateKey;

    /**
     * Field stores public key for RSA encryption algorithm.
     */
    public BigInteger publicKey;

    /**
     * The constructor for {@link RSA}. This method create an instance that can
     * both encrypt and decrypt.
     * 
     * @param bits
     *            The bits size for RSA key.
     */
    public RSA(Integer bits) {
        SecureRandom r = new SecureRandom();

        BigInteger p = new BigInteger(bits, 100, r); // Large prime number
        BigInteger q = new BigInteger(bits, 100, r); // Large prime number
        BigInteger e = euler(p, q); // Field stores value of Euler operations
                                    // for p and q, where p and q are large
                                    // prime numbers.

        n = p.multiply(q);
        publicKey = new BigInteger(Integer.toString(r.nextInt()));

        while (e.gcd(publicKey).intValue() > 1) {
            publicKey = publicKey.add(BigInteger.ONE);
        }

        privateKey = publicKey.modInverse(e);
    }

    /**
     * This method makes operation to achieve decrypted message.
     * 
     * @param valueToDecrypt
     *            The value of which will be performed the decryption process.
     * @return Decrypted value.
     */
    public synchronized BigInteger decrypt(BigInteger valueToDecrypt) {
        return valueToDecrypt.modPow(privateKey, n);
    }

    /**
     * This method makes operation to achieve decrypted message.
     * 
     * @param messageToDecrypt
     *            The message of which will be performed the decryption process.
     * @return Decrypted message.
     */
    public synchronized String decrypt(String messageToDecrypt) {
        return new String((new BigInteger(messageToDecrypt)).modPow(privateKey,
                n).toByteArray());
    }

    /**
     * This method makes operation to create encrypted message.
     * 
     * @param valueToEncrypt
     *            The value of which will be performed the encryption process
     * @return Encrypted value.
     */
    public synchronized BigInteger encrypt(BigInteger valueToEncrypt) {
        return valueToEncrypt.modPow(publicKey, n);
    }

    /**
     * This method makes operation to create encrypted message.
     * 
     * @param messageToEncrypt
     *            The message of which will be performed the encryption process
     * @return Encrypted message.
     */
    public synchronized String encrypt(String messageToEncrypt) {
        return (new BigInteger(messageToEncrypt.getBytes())).modPow(publicKey,
                n).toString();
    }

    /**
     * Gets value of Euler for p and q, where p and q are large prime numbers.
     * 
     * @param p
     *            The large prime number.
     * @param q
     *            The large prime number.
     * @return The value of Euler operations for p and q.
     */
    private BigInteger euler(BigInteger p, BigInteger q) {
        return (p.subtract(BigInteger.ONE)).multiply((q
                .subtract(BigInteger.ONE)));
    }

}

@@ -1,69 +1,4 @@
-import java.math.BigInteger;%0Aimport java.security.SecureRandom;%0A%0A
 /**%0A
@@ -87,16 +87,81 @@
 4.%0A */%0A%0A
+import java.math.BigInteger;%0Aimport java.security.SecureRandom;%0A%0A
 /**%0A * T
@@ -351,16 +351,25 @@
   * The 
+constant 
 base for
@@ -1669,32 +1669,599 @@
 erse(e);%0A    %7D%0A%0A
+    /**%0A     * This method calculates a numeric translation of the letter block.%0A     * %0A     * @param text%0A     *            The text to calculate a numeric value.%0A     * @param blockSize%0A     *            The size of text block.%0A     * @return The calculated value for given text.%0A     */%0A    public Long calculateNumber(String text, Integer blockSize) %7B%0A        // TODO%0A        Integer result = 0;%0A%0A        for (int i = 0; i %3C blockSize; i++) %7B%0A            result += text.getBytes()%5Bi%5D * (int) Math.pow(BASE, i);%0A        %7D%0A%0A        return new Long(result);%0A    %7D%0A%0A
     /**%0A     * T
@@ -4194,16 +4194,16 @@
 iply((q%0A
-
         
@@ -4246,10 +4246,433 @@
 %0A    %7D%0A%0A
+    /**%0A     * This method translate back from given number to block of characters.%0A     * %0A     * @param number%0A     *            The number to translate.%0A     * @param blockSize%0A     *            The size of text block.%0A     * @return The translated number to string.%0A     */%0A    public String translateToString(Long number, Integer blockSize) %7B%0A        // TODO%0A        String result = %22%22;%0A%0A        return result;%0A    %7D%0A%0A
 %7D%0A

Improve RSA encryption algorithm