Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I RSA encrypt a string with a plaintext key using Java BouncyCastle API on Android
    primarykey
    data
    text
    <p>I am trying to encrypt a string using the BouncyCastle API in Android to send off to a server.</p> <p>I have the public key in plaintext (in memory, not in the filesystem, of course! no need to yell at me, cryptographers ;) ) and I need to use this plaintext public key to encrypt a string to an RSA encrypted string.</p> <p>This is my class:</p> <pre><code>public class RSAEncryptor { //Get certificate from base64 string public static X509Certificate getCertificateFromBase64String(String string) throws CertificateException, javax.security.cert.CertificateException { if(string.equals("") || string == null) { return null; } byte[] certBytes = Base64.decode(string.getBytes(), Base64.DEFAULT); X509Certificate cert = X509Certificate.getInstance(certBytes); return cert; } //Get public key from base64 encoded string public static PublicKey getPublicKeyFromEncodedCertData(String encodedCertData) throws CertificateException, javax.security.cert.CertificateException { if(encodedCertData == null || encodedCertData.equals("")) return null; X509Certificate cert = getCertificateFromBase64String(encodedCertData); if(cert == null) return null; return cert.getPublicKey(); } public static String rsaEncrypt(String plainText, String keyFromResources) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException // { if(plainText == null || plainText.equals("")) return null; if(keyFromResources == null || keyFromResources.equals("")) return null; byte[] encryptedBytes; Cipher cipher = Cipher.getInstance("RSA"); PublicKey publicKey = null; try { publicKey = getPublicKeyFromEncodedCertData(keyFromResources); } catch(Exception ex) { Logger.LogError("getPublicKeyFromEncodedCertData()", ex); } cipher.init(Cipher.ENCRYPT_MODE, publicKey); encryptedBytes = cipher.doFinal(plainText.getBytes()); String encrypted = new String(encryptedBytes); return encrypted; } </code></pre> <p>}</p> <p>I'm currently not getting the properly encrypted string back out, just a garbled mess like this:</p> <p><code>��RB��%����I��Q��F*�bd[@�y�_H]T{KƾuTN�Q� ��U�f��]�S �q|.t�t�9�Rˇ�����)��{�},ޱ�ª�ǥ#���@k=�WO���f�7t"yP�z�</code></p> <p>(The <code>&lt;?&gt;</code>'s are null chars)</p> <p>I should be getting back an alphanumeric string similar to this:</p> <p><code>2+tSXez8JrAIX+VJ2Dy4IsA56XhWpTwF8X2yGGaI6novucXknwykDyqJZICpmYcqx75qBRgxwrW2kY9LmQR2xU17PLqTukAu2Bna8WXYTmJJQ7CWsN3SdABlETRfsYA+g3A2rO2Qp6aR9OCBcFVJpnZJjb9kaOUj5Pcj0tNPFdM=</code> (changed obviously from the actual response :D)</p> <p>I'd appreciate any help!</p> <p>Thanks!</p> <p>Has anyone done this before? I'd love any suggestions you have as to how to fix this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload