This is the code I'm currently using. It uses the BouncyCastle Provider.
static
{
Security.addProvider(new BouncyCastleProvider());
}
protected String encrypt(byte[] keyData, byte[] data) throws Exception {
X509EncodedKeySpec keyspec = new X509EncodedKeySpec(keyData);
KeyFactory kf = KeyFactory.getInstance("RSA", "BC");
PublicKey pk = kf.generatePublic(keyspec);
Cipher rsa = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding", "BC");
rsa.init(Cipher.ENCRYPT_MODE, pk);
byte[] output = rsa.doFinal(data);
String result = base64EncodeBytes(output);
return result;
}
I'm currently getting a
java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block
at org.bouncycastle.jce.provider.JCERSACipher.engineDoFinal(Unknown Source)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at Encryption.encrypt(RSAToken.java:60)