I\'ve got some decryption problems in my Android project.
I\'m getting a string signed with a private key and I have to verify(decrypt) it with a public key. I\'d like to
Java has got the Java Cryptography Extension Framework, which is just designed for these things.
BouncyCastle is a Cryptography Provider for this framework. This means, it provides your Java Cryptography Extension with implementations of cryptography algorithms.
You'll find the basic classes for this in the packages java.security
and javax.crypto
To decrypt your message with a public key you could try the following:
// Use RSA/NONE/NoPadding as algorithm and BouncyCastle as crypto provider
Cipher asymmetricCipher = Cipher.getInstance("RSA/NONE/NoPadding", "BC");
// asume, that publicKeyBytes contains a byte array representing
// your public key
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
KeyFactory keyFactory;
keyFactory = KeyFactory.getInstance(publicKeySpec.getFormat());
Key key = keyFactory.generatePublic(publicKeySpec);
// initialize your cipher
asymmetricCipher.init(Cipher.DECRYPT_MODE, key);
// asuming, cipherText is a byte array containing your encrypted message
byte[] plainText = asymmetricCipher.doFinal(cipherText);
Please note, that this example is very basic and lacks several try catch blocks. Also, you should not use an asymmetric cipher without padding as this makes you vulnerable to replay attacks. You may also encounter issues with the key length. In some Java packages, the maximum allowed key length is restricted. This may be solved by using the unlimited strength policy files.
I hope, this helps you in getting started with the Java cryptography.