php - Java decrypt error: data not block size aligned -
i'm trying encrypt data between android application , php webservice.
i found next piece of code in website: http://schneimi.wordpress.com/2008/11/25/aes-128bit-encryption-between-java-and-php/
but when try decrypt exception of title "data not block size aligned"
this method in mcrypt class
public string encrypt(string text) throws exception { if(text == null || text.length() == 0) throw new exception("empty string"); cipher cipher; byte[] encrypted = null; try { cipher = cipher.getinstance("aes/cbc/nopadding"); cipher.init(cipher.encrypt_mode, keyspec, ivspec); encrypted = cipher.dofinal(padstring(text).getbytes()); } catch (exception e) { throw new exception("[encrypt] " + e.getmessage()); } return new string( encrypted ); } public string decrypt(string code) throws exception { if(code == null || code.length() == 0) throw new exception("empty string"); cipher cipher; byte[] decrypted = null; try { cipher = cipher.getinstance("aes/cbc/nopadding"); cipher.init(cipher.decrypt_mode, keyspec, ivspec); decrypted = cipher.dofinal(hextobytes(code)); } catch (exception e) { throw new exception("[decrypt] " + e.getmessage()); } return new string( decrypted ); } private static byte[] hextobytes(string hex) { string hexindex = "0123456789abcdef"; int l = hex.length() / 2; byte data[] = new byte[l]; int j = 0; (int = 0; < l; i++) { char c = hex.charat(j++); int n, b; n = hexindex.indexof(c); b = (n & 0xf) << 4; c = hex.charat(j++); n = hexindex.indexof(c); b += (n & 0xf); data[i] = (byte) b; } return data; } private static string padstring(string source) { char paddingchar = ' '; int size = 16; int x = source.length() % size; int padlength = size - x; (int = 0; < padlength; i++) { source += paddingchar; } return source; }
and how i'm using in activity test:
string encrypted = mcrypt.encrypt(jsonuser.tostring()); string decrypted = mcrypt.decrypt(encrypted);
the encrypt method works fine, second throws exception.
at last! made work! suggestion. share code in case stuck me:
java
import java.security.nosuchalgorithmexception; import javax.crypto.cipher; import javax.crypto.nosuchpaddingexception; import javax.crypto.spec.ivparameterspec; import javax.crypto.spec.secretkeyspec; public class mcrypt { private string iv = "fedcba9876543210";//dummy iv (change it!) private ivparameterspec ivspec; private secretkeyspec keyspec; private cipher cipher; private string secretkey = "0123456789abcdef";//dummy secretkey (change it!) public mcrypt() { ivspec = new ivparameterspec(iv.getbytes()); keyspec = new secretkeyspec(secretkey.getbytes(), "aes"); try { cipher = cipher.getinstance("aes/cbc/nopadding"); } catch (nosuchalgorithmexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (nosuchpaddingexception e) { // todo auto-generated catch block e.printstacktrace(); } } public byte[] encrypt(string text) throws exception { if(text == null || text.length() == 0) throw new exception("empty string"); byte[] encrypted = null; try { cipher.init(cipher.encrypt_mode, keyspec, ivspec); encrypted = cipher.dofinal(padstring(text).getbytes()); } catch (exception e) { throw new exception("[encrypt] " + e.getmessage()); } return encrypted; } public byte[] decrypt(string code) throws exception { if(code == null || code.length() == 0) throw new exception("empty string"); byte[] decrypted = null; try { cipher.init(cipher.decrypt_mode, keyspec, ivspec); decrypted = cipher.dofinal(hextobytes(code)); } catch (exception e) { throw new exception("[decrypt] " + e.getmessage()); } return decrypted; } public static string bytestohex(byte[] data) { if (data==null) { return null; } int len = data.length; string str = ""; (int i=0; i<len; i++) { if ((data[i]&0xff)<16) str = str + "0" + java.lang.integer.tohexstring(data[i]&0xff); else str = str + java.lang.integer.tohexstring(data[i]&0xff); } return str; } public static byte[] hextobytes(string str) { if (str==null) { return null; } else if (str.length() < 2) { return null; } else { int len = str.length() / 2; byte[] buffer = new byte[len]; (int i=0; i<len; i++) { buffer[i] = (byte) integer.parseint(str.substring(i*2,i*2+2),16); } return buffer; } } private static string padstring(string source) { char paddingchar = ' '; int size = 16; int x = source.length() % size; int padlength = size - x; (int = 0; < padlength; i++) { source += paddingchar; } return source; } }
how use (java)
mcrypt = new mcrypt(); /* encrypt */ string encrypted = mcrypt.bytestohex( mcrypt.encrypt("text encrypt") ); /* decrypt */ string decrypted = new string( mcrypt.decrypt( encrypted ) );
====================================================
php
<?php class mcrypt { private $iv = 'fedcba9876543210'; #same in java private $key = '0123456789abcdef'; #same in java function __construct() { } function encrypt($str) { //$key = $this->hex2bin($key); $iv = $this->iv; $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv); mcrypt_generic_init($td, $this->key, $iv); $encrypted = mcrypt_generic($td, $str); mcrypt_generic_deinit($td); mcrypt_module_close($td); return bin2hex($encrypted); } function decrypt($code) { //$key = $this->hex2bin($key); $code = $this->hex2bin($code); $iv = $this->iv; $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv); mcrypt_generic_init($td, $this->key, $iv); $decrypted = mdecrypt_generic($td, $code); mcrypt_generic_deinit($td); mcrypt_module_close($td); return utf8_encode(trim($decrypted)); } protected function hex2bin($hexdata) { $bindata = ''; ($i = 0; $i < strlen($hexdata); $i += 2) { $bindata .= chr(hexdec(substr($hexdata, $i, 2))); } return $bindata; } }
how use (php)
<?php $mcrypt = new mcrypt(); #encrypt $encrypted = $mcrypt->encrypt("text encrypt"); #decrypt $decrypted = $mcrypt->decrypt($encrypted);
Comments
Post a Comment