I have this problem I'm trying to encrypt a string twice using this code :
KeySpec keySpec = new X509EncodedKeySpec(new BigInteger(publicKey, 36).toByteArray());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Key decodedPublicKey = keyFactory.generatePublic(keySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, decodedPublicKey);
byte a1[] = cipher.doFinal(z.getBytes());
KeySpec keySpec2 = new X509EncodedKeySpec(new BigInteger(publicKey, 36).toByteArray());
KeyFactory keyFactory2 = KeyFactory.getInstance("RSA");
Key decodedPublicKey2 = keyFactory2.generatePublic(keySpec2);
Cipher cipher2 = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher2.init(Cipher.ENCRYPT_MODE, decodedPublicKey2);
byte a2[] = cipher.doFinal(z.getBytes());
Both use the same publicKey, and same input z but each of the two blocks of code creates a different byte array (a1!=a2)
I want it to be equal but I have no idea how to do it (tried to look it up but couldn't find an answer yet).
Would really appreciate it if anyone could help me.
Thanks
EDIT :
Well, I didn't use the AES encryption since the data I encrypt is very small (just a password)
I have a client and a server, and when someone connects to theclient, I want to confirm his password (his encrypted password is in a file on the android sdcard) so I want to compare the real password with the password the user has entered in the login page
I don't have the private key on the client so I can't decrypt the encrypted pass which is already on the device, so I tried to compare encrypted password (that I thought would be the same which I now know they ain't)
Maybe you have an idea what I can do? how to compare a password and an encrypted password where I only have the public key
Thanks
cipher2instead ofcipher?