rendered paste bodyTHIS IS THE MAIN
----------------------------
class RSA
{
public static void main(String[] args) {
String returnedEncrypt = "Success";
String returnedDecrypt = "SuccessDe";
BigInteger p = BigInteger.probablePrime (32, new Random ());
BigInteger q = selectPrimeNotEqualTo (p);
System.out.println (q);
System.out.println (p);
BigInteger n = p.multiply (q);
BigInteger pMinus1qMinus1 = p.subtract (BigInteger.ONE).multiply (q.subtract (BigInteger.ONE));
BigInteger e = p;
if(p.compareTo(q) < 0 ) p = q;
while(!e.gcd(pMinus1qMinus1).equals(BigInteger.ONE)) {
e = e.add(BigInteger.ONE);
}
BigInteger d = e.modInverse(pMinus1qMinus1);
Encrypt crypt = new Encrypt(n, e);
Decrypt crypted = new Decrypt(d, n);
try {
returnedEncrypt = crypt.encryptFILE();
}
catch (IOException ioex) {
System.out.println ("Input error");
System.exit (1);
}
try {
returnedDecrypt = crypted.decryptFILE();
}
catch (IOException ioex) {
System.out.println ("Input error");
System.exit (1);
}
}
private static BigInteger selectPrimeNotEqualTo (BigInteger p) {
BigInteger q;
do {
q = BigInteger.probablePrime (32, new Random ());
} while (p == q);
return q;
}
}
THE OTHER TWO CLASSES ENCRYPT AND DECRYPT
-----------------------------------------------
class Encrypt
{
private BigInteger n;
private BigInteger e;
Encrypt(BigInteger INn, BigInteger INe)
{
n = INn;
e = INe;
}
public String encryptFILE()
throws FileNotFoundException, IOException {
String hello = "hello";
BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (
"output.dat"));
try {
BufferedInputStream in = new BufferedInputStream (new FileInputStream (
"test.jpg"));
try {
while (true) {
byte [] test = n.toByteArray();
int lengthREAD = test.length - 2;
byte[] myarray = new byte[lengthREAD];
byte[] insert = new byte[lengthREAD + 1];
int read = in.read (myarray, 0 , lengthREAD);
insert[0] = 1;
System.arraycopy(myarray, 0, insert, 1, myarray.length);
byte[] resultBlock = encryptBlock (n, e, insert);
int length = resultBlock.length;
if (read == -1) {
break;
}
byte[] use = padding2(resultBlock, length, n, lengthREAD);
out.write (use, 0, lengthREAD + 2);
}
}
catch (IOException ioex) {
System.out.println ("Input error");
System.exit (1);
}
finally {
in.close ();
}
}
finally {
out.close ();
out.flush ();
}
return hello;
}
private static byte[] encryptBlock (BigInteger n, BigInteger randome,
byte[] myarray) {
BigInteger writeBigInt = new BigInteger (myarray);
BigInteger encrypt = (writeBigInt.modPow (randome, n));
return encrypt.toByteArray();
}
private static byte[] padding2 (byte[] source, int length, BigInteger n, int LengthREAD) {
byte [] writearray = new byte[LengthREAD + 2];
System.arraycopy(source, 0, writearray, writearray.length - source.length, source.length);
return writearray;
}
}
class Decrypt
{
private BigInteger d;
private BigInteger n;
Decrypt(BigInteger INd, BigInteger INn)
{
n = INn;
d = INd;
}
public String decryptFILE()
throws FileNotFoundException, IOException {
String hello = "hello";
BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (
"decrypted.jpg"));
try {
BufferedInputStream in = new BufferedInputStream (new FileInputStream (
"output.dat"));
try {
while (true) {
byte [] test = n.toByteArray();
int lengthWrite = test.length - 2;
byte[] encryptedBlock = new byte[lengthWrite + 2];
byte[] WriteBlock = new byte[lengthWrite];
int read = in.read (encryptedBlock, 0 , lengthWrite + 2);
if (read == -1) {
break;
}
byte[] decryptedBlock = decryptBlock (encryptedBlock, d, n, lengthWrite);
System.arraycopy(decryptedBlock, 1, WriteBlock, 0, WriteBlock.length);
out.write (WriteBlock, 0, WriteBlock.length);
}
}
catch (IOException ioex) {
System.out.println ("Input error");
System.exit (1);
}
finally {
in.close ();
}
} finally {
out.close ();
out.flush ();
}
return hello;
}
private static byte[] decryptBlock (byte[] blockData, BigInteger inverse,
BigInteger n, int LengthWrite) {
BigInteger blockValue = new BigInteger (blockData);
BigInteger decrypt = (blockValue.modPow (inverse, n));
return decrypt.toByteArray ();
}
}