How to decrypt a data using rsa privatekey

Go To StackoverFlow.com

-1

I am using JAVA My friend uses SYMBIAN

I and my friend have same rsa modulus. If I encrypt the data using public key then my friend is able to decrypt the same. But if my friend encrypt the data with public key then I am not able to decrypt the data. I got an error as "Data must start with zero "

public static byte[] encrypt(byte[] encrptdByte) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    byte[] encryptionByte = null;
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    encryptionByte = cipher.doFinal(encrptdByte);
    return encryptionByte;
}

public static void decrypt(byte[] encrptdByte) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
    byte[] encryptionByte = null;
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, publicKey);
    encryptionByte = cipher.doFinal(encrptdByte);

    System.out.println("Recovered String     :::  " + new String(encryptionByte));
}

Thanks Sunil

2009-06-16 09:06
by Sunil
Can you post some of the Java code you are using, or which libraries - Thilo 2009-06-16 09:12
I am using RSA with no padding no cipher mode - Sunil 2009-06-16 09:22
I have posted please have a look on it - Sunil 2009-06-16 09:23


6

The decrypt function uses publicKey - where does it come from? Note that data encrypted with a public key must be decrypted with the corresponding private key and not with the same public key. Asymmetric encryption such as RSA have the notion of key pairs where each key in the pair can decrypt data encrypted with the other key, in contrast to symmetric encryption such as AES where the same key works for both encryption and decryption.

2009-06-16 10:47
by laalto
Yes, Symbian have created public key by using the modulus that I have sent them. Still that problem arised.

Thank - Sunil 2009-06-17 07:37

RSA keys are not just the modulus N but there's also an exponent E for encryption and exponent D for decryption. So a public key is essentially (E,N) and a private key is (D,N) - laalto 2009-06-17 07:50
No we can create RSA public key using modulus - Sunil 2009-06-17 11:27


2

To add the previous post, it's impractical to encrypt / decrypt data on a large scale using assymetric encryption (because it's significantly slower than symmetric encryption). The most practical use of assymmetric encryption (like RSA) is to encrypt the symmetric keys (for AES or similar algorithm) that were used to encrypt the data and also to sign a secure hash of the message digest (SHA-256 etc).

The encrypted message is typically sealed in an "envelope" that contains the encrypted message as well as the keys used for encryption. The keys are of course encrypted with the recipients public key, thereby ensuring that only the holder the private key can retrieve the keys.

Finally, the sender of the message may optionally compute a secure hash of the message and encrypt it with the sender's private key. The recipient decrypts the encrypted hash (using the sender's public key) and compares with the computed hash to verify the identity of the sender.

2009-06-16 11:04
by NoName
Maximum data that can be encrypted using RSA = 117 bytes but my data is of 14 bytes only - Sunil 2009-06-17 07:38
Sunil,

I am not very familiar with the Java Crypto APIs, but decryption requires access to the private key. I am assuming that that the cipher.Init() has some way of accessing the private key based on the publicKey that's passed in? Since your friend was able to decrypt data encrypted using your public key, did you pass the private key to him - NoName 2009-06-17 09:56

Ads