Jua*_*njo 5 php java encryption
我有一个带有java和PHP文件的应用程序.java文件将内容发送到PHP文件,然后通过HTTP将响应发送到java文件.我有JSON格式的回复.
我想对信息进行加密,并在另一端进行解码,java->php和php->java(这是最重要的),但我不知道该怎么做.
编辑:我正在尝试BLOWFISH,这是我在PHP中的代码(加密数据并发送到Java)和Java(获取数据并对其进行解码)
PHP
$key = "this is the key";
$crypttext = mcrypt_encrypt(MCRYPT_BLOWFISH, $key, $result_json, MCRYPT_MODE_ECB);
echo($crypttext);
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
System.out.println(decryptBlowfish(result, "this is the key"));
Run Code Online (Sandbox Code Playgroud)
我执行的结果是:
Input length must be multiple of 8 when encrypting with padded cipher
Run Code Online (Sandbox Code Playgroud)
或者有时候
Given final block not properly padded
Run Code Online (Sandbox Code Playgroud)
Tom*_*Tom -1
如果您不想要 SSL(我也推荐 SSL),您可以尝试以下操作:
$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]));
}
Run Code Online (Sandbox Code Playgroud)
用您想要的任何语言编写编码器/编码器既快速又容易。生成的字符串是二进制字符串,因此您可能需要使用 base64_encode 或其他方式对其进行转换。应该能提供相当好的安全性。