Note that there are some explanatory texts on larger screens.

plurals
  1. POAES-128 Encryption with Base64 Encoding doesn't act the same in Java and PHP with longer strings
    primarykey
    data
    text
    <p>I have been trying to get Encryption/Decryption to work the same in Java (Android) and PHP and produce the same results for client/server communications.</p> <p>I'm using the code below for encryption, but I don't know what's wrong with it. Running both with the same key and small strings produce the same encrypted value, with longer strings however, I get two different results. PHP:</p> <pre class="lang-php prettyprint-override"><code>$str = 'test1234test1234'; $key = 'TESTKEYTESTKEY12'; $block = mcrypt_get_block_size('des', 'ecb'); $pad = $block - (strlen($str) % $block); $str .= str_repeat(chr($pad), $pad); echo base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_ECB)); </code></pre> <p>and in Java ( on Android ):</p> <pre class="lang-java prettyprint-override"><code>public static String encryptTest() { String cleartext = "test1234test1234"; String key = "TESTKEYTESTKEY12"; byte[] raw = key.getBytes(); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); try { Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted; encrypted = cipher.doFinal(cleartext.getBytes()); return new String(Base64.encode(encrypted,Base64.DEFAULT)); } catch (Exception e) { e.printStackTrace(); } return null; } </code></pre> <p>Running this with String <code>test1234</code> gives: <code>8i4KEe82TQl0Zdlc14fwAg==</code> in both implementations. With string <code>test1234test1234</code> however I get <code>4s5a0edsvwWt/3/enRe0wgJQD/0zL45NRb/r3p6L/Is=</code> with PHP, and <code>4s5a0edsvwWt/3/enRe0wgA0jk78zwWJr1xsosZbYUA=</code> with Java. I'm not sure what's wrong and I'm not knowledgeable enough about Cryptography.</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