I have an application with java and PHP files. Java files send content to PHP files, and this sends the response to the java file, via HTTP to everyone. I have an answer with JSON format.
I would like to encrypt information and decode it on the other hand, java->phpand php->java(this is the most important), but I do not know how to do it.
Edit: I'm trying to BLOWFISH, here is my code in PHP (glue data and send to Java) and Java (get data and decode them)
Php
$key = "this is the key";
$crypttext = mcrypt_encrypt(MCRYPT_BLOWFISH, $key, $result_json, MCRYPT_MODE_ECB);
echo($crypttext);
Java
public String decryptBlowfish(String to_decrypt, String strkey) {
System.out.println(to_decrypt);
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) {
System.out.println(e.getMessage());
;
return null;
}
}
System.out.println(decryptBlowfish(result, "this is the key"));
Result on execution:
Input length must be multiple of 8 when encrypting with padded cipher
or sometimes
Given final block not properly padded
source
share