Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to decrypt the encrypted sms (encrypted in 2.2 os )in Android...(trying to decrypt in 2.3 os)
    primarykey
    data
    text
    <p>I am able to encrypt an SMS and send it from one simulator (Android 2.2) to another. On the receiving end I am able to do the decryption successfully. But the problem is if do the encryption in one OS version (i.e Android 2.2) and trying to decrypt in another OS version ( Android 2.3 ) i am getting 'Bad padding exception'. I checked that i used the same key on both ends. The code is shown below</p> <pre><code>public class ED { private String Key; public ED() { Key = "abc12"; // Assigning default key. } public ED(String key) { // TODO Auto-generated constructor stub Key = key; } public String encrypt(String toEncrypt) throws Exception { byte[] rawKey = getRawKey(Key.getBytes("UTF-8")); byte[] result = encrypt(rawKey, toEncrypt.getBytes("UTF-8")); return toHex(result); } public byte[] encrypt(byte[] key, byte[] toEncodeString) throws Exception { SecretKeySpec sKeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, sKeySpec); byte[] encrypted = cipher.doFinal(toEncodeString); return encrypted; } private byte[] getRawKey(byte[] key) throws Exception { KeyGenerator kGen = KeyGenerator.getInstance("AES"); SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); sr.setSeed(key); kGen.init(128, sr); SecretKey sKey = kGen.generateKey(); byte[] raw = sKey.getEncoded(); return raw; } /************************************* Decription *********************************************/ public String decrypt(String encryptedString) throws Exception { byte[] rawKey = getRawKey(Key.getBytes("UTF-8")); System.out.println("Decrypted Key in bytes : "+rawKey); System.out.println("Key in decryption :"+rawKey); SecretKeySpec sKeySpec = new SecretKeySpec(rawKey, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, sKeySpec); byte[] decrypted = cipher.doFinal(toByte(encryptedString)); System.out.println("Decrypted mess in bytes----------&gt;" +decrypted); return new String(decrypted); } public String toHex(byte[] buf) { if (buf == null) return ""; StringBuffer result = new StringBuffer(2*buf.length); for (int i = 0; i &lt; buf.length; i++) { appendHex(result, buf[i]); } return result.toString(); } private final String HEX = "0123456789ABCDEF"; private void appendHex(StringBuffer sb, byte b) { sb.append(HEX.charAt((b&gt;&gt;4)&amp;0x0f)).append(HEX.charAt(b&amp;0x0f)); } public byte[] toByte(String hexString) { int len = hexString.length()/2; byte[] result = new byte[len]; for (int i = 0; i &lt; len; i++) result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue(); return result; } } </code></pre> <p>And I am using sendTextMessage() function to send an sms. I read that encryption/decryption doesn't depend on OS but in this case that is not true. Am I missing any important things while configuring the Cipher (in AES) ? Please let me know.</p>
    singulars
    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.
 

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