views:

2163

answers:

2

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

+4  A: 

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.

laalto
Yes, Symbian have created public key by using the modulus that I have sent them. Still that problem arised.Thanks
Sunil
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
No we can create RSA public key using modulus.
Sunil
+3  A: 

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.

Maximum data that can be encrypted using RSA = 117 bytes but my data is of 14 bytes only.
Sunil
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?