Content encryption with php and java

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
+5
source share
3 answers

, /, "blowfish/ecb/nopadding" "blowfish" .

private static final String DECRYPTION_ALGORITHM = "blowfish/ecb/nopadding";
private static final String KEY_ALGORITHM = "blowfish";
private static byte[] decrypt(byte[] keyData, byte[] valueData) throws Exception {
    SecretKeySpec keySpec = new SecretKeySpec(keyData, KEY_ALGORITHM);
    Cipher cipher = Cipher.getInstance(DECRYPTION_ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, keySpec);
    return cipher.doFinal(valueData);
}
0

If you do not want to use SSL, which I also recommend, you can try the following:

$str = 'hello world'; //your input data
$pass = 'haj83kdj843j'; //something random, the longer the better
$l = strlen($pass);

for ($i=0; $i<strlen($str); $i++)
{
  $str[$i] = chr(ord($str[$i]) + ord($pass[$i % $l]));
}

Quickly and easily write an encoder / encoder in any language you want. The resulting string is a binary string, so you can convert it using base64_encode or something like that. Should provide good security.

-1
source

All Articles