Here's the Android side of things. I did this under 1.1 I believe, but it still compiled and worked under 1.5. I suspect it'll probably work under 2.0, but I'll leave it up to you to locate and/or implement a Base-64 encoding/decoding scheme. Again, this is basic functionality to allow interoperability between PHP and Android's encryption systems. I'm not going to say this is bug free or even optimal but it at least was functional. The Android code below was part of a utilities class I wrote, and once again, the keys were going to be swapped out on a regular basis.
final static String AES_V1_KEY = "D0QgiY8JYvx8qzKx0iaN8kwEJgwpEqAJ"; static String nullPadString(String original) { String output = original; int remain = output.length() % 16; if (remain != 0) { remain = 16 - remain; for (int i = 0; i < remain; i++) output += (char) 0; } return output; } static String encryptString(final String RAWDATA, boolean ENCODE) throws UnknownAppVersionException { // This was a custom exception class. String encrypted = null; byte[] encryptedBytes = null; byte[] key; switch (app.rootObject.getVersionCode()) { case 1: key = AES_V1_KEY.getBytes(); break; default: throw new UnknownAppVersionException(); } SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); // Instantiate the cipher Cipher cipher = null; try { String input = Integer.toString(RAWDATA.length()) + '|' + RAWDATA; cipher = Cipher.getInstance("AES/ECB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); encryptedBytes = cipher.doFinal(Utilities.nullPadString(input).getBytes()); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } // encrypted = new String(encryptedBytes); if( ENCODE) encrypted = new String(Base64.encodeBase64(encryptedBytes)); else encrypted = new String(encryptedBytes); return encrypted; } static String decryptString(final String ENCRYPTEDDATA,final boolean DECODE) throws UnknownAppVersionException { String raw = null; byte[] rawBytes = null; byte[] encryptedBytes; if( DECODE ) encryptedBytes = Base64.decodeBase64(ENCRYPTEDDATA.getBytes()); else encryptedBytes = ENCRYPTEDDATA.getBytes(); byte[] key; switch (app.rootObject.getVersionCode()) { case 1: key = AES_V1_KEY.getBytes(); break; default: throw new UnknownAppVersionException(); } SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); // Instantiate the cipher Cipher cipher = null; try { cipher = Cipher.getInstance("AES/ECB/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); rawBytes = cipher.doFinal(encryptedBytes); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } raw = new String(rawBytes); int delimiter = raw.indexOf('|'); int length = Integer.valueOf(raw.substring(0, delimiter)); raw = raw.substring(delimiter + 1, length + delimiter + 1); return raw; } Good luck! Raymond On 11/25/2009 06:44 PM, Raymond C. Rodgers wrote: > Here is PHP code I lifed out of a project that I've now abandoned. > I'll find and post the Android side of things later. The basics are > that I used the Rijndael-128+ECB encryption combined with Base-64 > encoding to transmit information back and forth between the > application and the web server. The catch, in this whole thing, is > that on the Android side of things, you probably will need to do some > manual padding at the end of your encrypted string in order to get > things to work properly. Like I said, I'll post the Android side of > things later. > > I had intended to change the encryption key with every new version of > my application, so be aware of that when you see $VERSION in my code. > This isn't a complete example, just the encryption, decryption > functions I had written. > > Raymond > > define('AES_V1_KEY','D0QgiY8JYvx8qzKx0iaN8kwEJgwpEqAJ'); > function encryptString($RAWDATA,$VERSION = 1) > { > $key = null; > switch($VERSION) > { > case 1: > $key = AES_V1_KEY; > break; > } > // encrypt string > $td = mcrypt_module_open('rijndael-128','','ecb',''); > $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); > mcrypt_generic_init($td,$key,$iv); > $encrypted_string = mcrypt_generic($td, strlen($RAWDATA) . '|' . > $RAWDATA); > mcrypt_generic_deinit($td); > mcrypt_module_close($td); > // base-64 encode > return base64_encode($encrypted_string); > } > function decryptString($ENCRYPTEDDATA,$VERSION = 1) > { > $key = null; > switch($VERSION) > { > case 1: > $key = AES_V1_KEY; > break; > } > // base-64 decode > $encrypted_string = base64_decode($ENCRYPTEDDATA); > // decrypt string > $td = mcrypt_module_open('rijndael-128','','ecb',''); > $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); > mcrypt_generic_init($td,$key,$iv); > $returned_string = mdecrypt_generic($td,$encrypted_string); > unset($encrypted_string); > list($length,$original_string) = explode('|',$returned_string,2); > unset($returned_string); > $original_string = substr($original_string,0,$length); > mcrypt_generic_deinit($td); > mcrypt_module_close($td); > return $original_string; > } > > > jax wrote: >> What would I use to: Encrypt a string in PHP and Decrypt that string >> from Android? What methods are supported by both and which is the >> most secure? >> -- 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