0

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.

Gherbi Hicham
  • 2,416
  • 4
  • 26
  • 41
  • 1
    could you try replacing, `SecureRandom secureRandom = new SecureRandom();` with `SecureRandom secrand = SecureRandom.getInstance("SHA1PRNG", "Crypto")` ? – Sufiyan Ghori Feb 01 '15 at 13:47
  • Did that, didn't worked. – user3787605 Feb 01 '15 at 13:56
  • Which Android version are you using ? and please post your stacktrace. – Sufiyan Ghori Feb 01 '15 at 14:07
  • please post your logcat too. – Sufiyan Ghori Feb 01 '15 at 14:08
  • Why are you programming RSA yourself? You do know that RSA is not secure if not implemented correctly, [right](http://crypto.stackexchange.com/q/20085/1172)? Why not use `Cipher` with RSA and key wrapping? – Maarten Bodewes Feb 01 '15 at 17:24
  • @ Maarten I know its not safe but I've to stick with the requirement instead of doing what I want. Any solution ? @Sufiyan Tried on android 4.4.4 and 5.0. Also logcat dont say anything it just decrypts the message. There isn't any exception, thereis any logical error somewhere. – user3787605 Feb 01 '15 at 18:51
  • @user3787605 would you please read this ? http://stackoverflow.com/questions/13383006/encryption-error-on-android-4-2/28263522#28263522 – Sufiyan Ghori Feb 01 '15 at 18:55
  • Are you using the right keys at both sides? Have you checked? If so, how? I don't see any method of transferring the keys. – Maarten Bodewes Feb 01 '15 at 21:02
  • I'm voting to close this as this will turn out to be an online debug session. Currently the error description is empty and the code doesn't run on itself. We're also missing input and output parameters. Those are all problems *if* we want to be drawn into debugging this. – Maarten Bodewes Feb 07 '15 at 15:35

0 Answers0