I am having a serious problem with the RSA asymmetric cryptography algorithm. A message encrypted using User A public key is being decrypted by the User's private key. On the computer it works fine. When I run it in java it works good but in android it doesn't. Heres the code :
public synchronized void generate_keys()
{
SecureRandom secureRandom = new SecureRandom();
BigInteger p = new BigInteger(bitLength/2,100 ,secureRandom);
BigInteger q = new BigInteger(bitLength/2,100 ,secureRandom);
n = p.multiply(q);
BigInteger v = (p.subtract(BigInteger.ONE)).
multiply(q.subtract(BigInteger.ONE));
e = new BigInteger("3");
while (v.gcd(e).intValue() > 1 )
{
e = e.add(new BigInteger("2"));
}
d = e.modInverse(v);
}
public synchronized static String encryptKey(String key, BigInteger n,
BigInteger publicKey) {
BigInteger plaintTextBytes = new BigInteger(key.getBytes());
return plaintTextBytes.modPow(publicKey, n).toString();
}
public synchronized static String decryptKey(String CypherKey,
BigInteger privateKey, BigInteger n) {
BigInteger keyBytes = new BigInteger(CypherKey);
BigInteger key = keyBytes.modPow(privateKey, n);
return new String(key.toByteArray());
}
Kindly review the code and guide me to what could be cause of the problem.