I am trying to decrypt a String, that I have encrypted elsewhere. Here is my code:
private void test() { try { String stringMessage="Sf3O7Lr2+WN5szGyLejL3CjuBRZtQ72+ZBmgVTgWnatQZxUElzaBqFa1p0SVBqe9VWVxCxdEkejMVtDGEr0UJSVSK8EB/ fPI6v8JE8dIu0JN0mMs4xlowhITy0tQR+1pcBtDFjzOl33xxQcq5JuPezxRDxFIp +IVkD8FdpqlttEKf2Tvqw9tqsdgiBKb5xDvKrkIDQXdLBh1gbAVZDSJYGHRkcOA8vz2ty/ PeooKkfDK6IOn7KBwOBgSRgQr/MLBF3Xk2vRWgVGRh/fRkzu21EWo99Q5moWKxWl3HW/ bbgTBQTb097XP3NTID9kSPhCfL0BEfBxonuNse5GBoeRnCw=="; //Convert String back to Byte[] and decrpt byte[] byteMessage = Base64.decodeBase64(stringMessage.getBytes("UTF-8")); System.out.println("ENCRYPTED MESSAGE byte Length: "+byteMessage.length); String decryptedMsg = decryptString(byteMessage, loadCASPrivateKey()); System.out.println(decryptedMsg); } catch (Exception e) { e.printStackTrace(); return; } } private static String decryptString(byte[] message, Key privateKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] cipherData = cipher.doFinal(message); return new String(cipherData, "UTF-8"); } private PrivateKey loadCASPrivateKey() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { InputStream is = getClass().getResourceAsStream( "/keys/app- private.key" ); if (is == null) { System.out.println("NULL"); } byte[] encodedPrivateKey = new byte[(int) 2000]; is.read(encodedPrivateKey); is.close(); // Generate KeyPair. KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey); PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); return privateKey; } This works 100% how I would like it to under my desktop JVM, however when I run it in the Android emulator I get: 04-24 22:42:21.011: I/System.out(1041): ��k���_��*�ݣ���93| @0�̍4�y)��Q�k�;*A����e�#��A� �oiu:�����W5@$�w�j��uS�R�Ocxٰ&����l �w'/ �d�8uA��ؔ�{�4$�U�0��{Ԑ��t!9��n�� ��a��'Jdt2�t�T�D��k+k�;������ ����GF�� \�rڼ��>]�y+^w�<�� ��'E{�8R]�ZHyu��ζ��軟�ށ掱�{�A�#ȟ� I assume my problem is down to encoding, but I've spent all day trying to work out what and am really stumped. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en