encrypting and decrypting between different languages and character sets can be painful. It took me many hours of tweaking to get to a working robust solution.
The PHP code will encrypt and decrypt a string.
The java code will successfully decrypt a string encrypted by the PHP code.
Encrypt and decrypt in PHP:
function encryptString($pin) { $cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, ''); // The block-size of the Blowfish algorithm is 64-bits, therefore our IV // is always 8 bytes: $iv = '12345678'; $key = encryptKey(); //32chars = 256bit encryption // The strengh of the encryption is determined by the length of the key // passed to mcrypt_generic_init if (mcrypt_generic_init($cipher, $key, $iv) != -1) { $cipherText = mcrypt_generic($cipher, $pin); mcrypt_generic_deinit($cipher); return $cipherText; } else { mcrypt_generic_deinit($cipher); return ""; } } function decryptString($pin) { $iv = '12345678'; $key = encryptKey(); return mcrypt_cbc(MCRYPT_BLOWFISH, $pin, $ciphertext, MCRYPT_DECRYPT, $iv); } function hex2bin($h) { //this function is the opposite of php's bin2hex if (!is_string($h)) return null; $r=''; for ($a=0; $a<strlen($h); $a+=2) { $r.=chr(hexdec($h{$a}.$h{($a+1)})); } return $r; } //usage: $encrypted_string = encryptString('Some text to encrypt'); $decrypted_string = decryptString($encrypted_string); |
To decrypt in JAVA
private static String decryptBlowfish(String to_decrypt) { try { String iv = "12345678"; byte[] IVBytes = getBytes(iv); if (IVBytes == null) { return null; } IvParameterSpec IV = new IvParameterSpec(IVBytes); String strkey = "12345678901234567890123456789012"; //must be the same key we used to encrypt the data byte[] strkeyBytes = getBytes(strkey); if (strkeyBytes == null) { return null; } SecretKeySpec key = new SecretKeySpec(strkeyBytes, "Blowfish"); try { Cipher cipher = Cipher.getInstance("Blowfish/CBC/NoPadding"); try { cipher.init(Cipher.DECRYPT_MODE, key, IV); try { byte[] encrypted = getBytes(to_decrypt); if (encrypted == null) { return null; } byte[] decrypted = cipher.doFinal(encrypted); return new String(decrypted); } catch(Exception e) { errorMsg = "ERROR: decryptBlowfish(dofinal) : to_decrypt :" + to_decrypt + " : message : " + e; return null; } }catch (Exception e) { errorMsg = "ERROR: decryptBlowfish(init) :" + e; return null; } }catch (Exception e) { errorMsg = "ERROR: decryptBlowfish(getInstance) :" + e; return null; } } catch (Exception e) { errorMsg = "ERROR: decryptBlowfish :" + e; return null; } } private static String hexToString(String input, int groupLength) { try { StringBuilder sb = new StringBuilder(input.length() / groupLength); for (int i = 0; i < input.length() - groupLength + 1; i += groupLength) { String hex = input.substring(i, i + groupLength); sb.append((char) Integer.parseInt(hex, 16)); } return sb.toString(); } catch (Exception e) { errorMsg = "ERROR: hexToString :" + e; return null; } } private static byte[] getBytes(String toGet) { try { byte[] retVal = new byte[toGet.length()]; for (int i = 0; i < toGet.length(); i++) { char anychar = toGet.charAt(i); retVal[i] = (byte)anychar; } return retVal; }catch(Exception e) { errorMsg = "ERROR: getBytes :" + e; return null; } } //USAGE String stringSignature = hexToString(signature, 2); String decryptSignature = decryptBlowfish(stringSignature); |
public static String encryptBlowfish(String to_encrypt, String strkey) { try { SecretKeySpec key = new SecretKeySpec(strkey.getBytes(), "Blowfish"); Cipher cipher = Cipher.getInstance("Blowfish"); cipher.init(Cipher.ENCRYPT_MODE, key); return new String(cipher.doFinal(to_encrypt.getBytes())); } catch (Exception e) { return null; } } public static String decryptBlowfish(String to_decrypt, String strkey) { try { SecretKeySpec key = new SecretKeySpec(strkey.getBytes(), "Blowfish"); Cipher cipher = Cipher.getInstance("Blowfish"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decrypted = cipher.doFinal(to_decrypt.getBytes()); return new String(decrypted); } catch (Exception e) { return null; } } |

Categories
Tag Cloud
Blog RSS
Comments RSS
Last 50 Posts
Back
Void « Default
Life
Earth
Wind
Water
Fire
Light 