note 51223 added to ref.mcrypt

From: Date: Wed, 23 Mar 2005 21:12:24 +0000
Subject: note 51223 added to ref.mcrypt
Groups: php.notes 
Request: Send a blank email to php-notes+get-86861@lists.php.net to get a copy of this message
Just a followup note about mcrypt<-->java interop (have a php app encrypting a string and a java app decrypting it - and vice-versa!) it seems that mcrypt pads with nulls (0x00) instead of spaces so to allow the interop to work properly (where both java and php/mcrypt encode strings to the same value) - I padded the PHP strings with spaces first before encrypting and did the same on the Java side - thus the padding was the same on each side and the interop is complete. Here's sample PHP for padding: $dlen = strlen($data); $pad = fmod($dlen, 16); if ($pad > 0) { $i = (int)$pad; while ($i > 0) { $data.=" "; $i--; } } and here's the Java padding: public static String PadString(String in) { int slen = (in.length() % 16); int i = (16 - slen); if ((i > 0) && (i < 16)){ StringBuffer buf = new StringBuffer(in.length() + i); buf.insert(0, in); for (i = (16 - slen); i > 0; i--) { buf.append(" "); } return buf.toString(); } else { return in; } } With trimming on each side for decryption ... all works well. ---- Manual Page -- http://www.php.net/manual/en/ref.mcrypt.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+51223 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51223&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51223&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51223&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51223&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51223&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+51223&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#86861) next »