PLEASE HELP - Strange problem in RSA class

G

Gandu

Could some Java guru please provide some hints as to what the problem
might be?
I have a Java class to implement the RSA algorithm, as shown below:

import java.math.BigInteger;
import java.util.Random;
import java.security.SecureRandom;
import java.io.*;

public class RSA{
private BigInteger product;
private BigInteger primeProduct;
private BigInteger p;
private BigInteger q;
private BigInteger privKey;
private BigInteger pubKey;

private void genTestKeys(){
System.out.println("Generating test keys ...");
SecureRandom secRan = new SecureRandom();
BigInteger t1 = BigInteger.probablePrime(64, secRan);
BigInteger t2 = BigInteger.probablePrime(64, secRan);

BigInteger testProd = t1.multiply(t2);
BigInteger x = t1.subtract(BigInteger.ONE);
BigInteger y = t2.subtract(BigInteger.ONE);

BigInteger testProdNew = x.multiply(y);

BigInteger testPubKey = BigInteger.probablePrime(64, secRan);

while(testPubKey.gcd(testProdNew).intValue() != 1){
testPubKey = testPubKey.add(new BigInteger("2"));
}

BigInteger testPrivKey = testPubKey.modInverse(testProdNew);

product = testProd;
primeProduct = testProdNew;
pubKey = testPubKey;
privKey = testPrivKey;

}

private boolean testForPrime(BigInteger puk,
BigInteger prk,
BigInteger prod){
long puK = puk.longValue();
long prK = prk.longValue();
long prodd = prod.longValue();

if((puK*prK)%prodd == 1) return true;
return false;
}

public RSA(){
product = null;
primeProduct = null;
p = null;
q = null;
privKey = null;
pubKey = null;
}

public void genKeys(){
System.out.println("Starting key generation ...");
genTestKeys();
}

BigInteger encrypt(BigInteger bigInt){
if(bigInt != null){
return bigInt.modPow(pubKey, product);
}
else {
return null;
}
}

BigInteger decrypt(BigInteger bigInt){
if(bigInt != null){
}
else{
return null;
}
}

public void showKeys(){
if(pubKey != null || privKey != null){
System.out.println("Public Key: "+pubKey+" Private Key:
"+privKey);
}
else{
System.out.println("One or more keys are null ...");
}
}

public static void main(String [] args){
RSA rsa = new RSA();
rsa.genKeys();
rsa.showKeys();
String str = new String("This is another test ");
byte [] byteArray = str.getBytes();
BigInteger testBigInt = new BigInteger(byteArray);
// BigInteger testBigInt = new
BigInteger("12345678901234567890");

BigInteger result = rsa.decrypt(rsa.encrypt(testBigInt));

if(result != null){
System.out.println("Encrypted: "+testBigInt);
// String str1 = new String(result.toByteArray());
// System.out.println("Decrypted: "+str1);
System.out.println("Decrypted: "+result);
}
}
}


With the current input in 'public static void main(String [] args)'
the output is:
$ java RSA
Starting key generation ...
Generating test keys ...
Public Key: 10358682925504495457 Private Key:
85548903426018865087899176922878292449
Encrypted: 123362224183149454759410594198589705514929735562272
Decrypted: 55094910403196717837670178223901034918

CLEARLY, the encryption/decryption is NOT working!!!

However, if I change the input to: 'is another Test', the output is:
$ java RSA
Starting key generation ...
Generating test keys ...
Public Key: 16246273829459943121 Private Key:
203057061846410322238656565899373741153
Encrypted: 140166710452564844135883580065323381792
Decrypted: 140166710452564844135883580065323381792

CLEARLY, the encryption/decryption is WORKING!!!
Could someone point out what I may be doing wrong? Thanks in advance
for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top