河豚加密 - 用 php 和 java 加密,我得到了不同的加密值


Blowfish encryption - encrypted in php and java, I got different encrypted values

加密测试数据:

key: 'ABC';
data:'1234567';
algorithm:  MCRYPT_BLOWFISH;
mode: MCRYPT_MODE_ECB;

PHP代码

$key  = 'ABC';
$data = '1234567';
$alg  = MCRYPT_BLOWFISH;
$mode = MCRYPT_MODE_ECB;
$encrypted_data = mcrypt_encrypt($alg, $key, $data, $mode);
$phpgeneratedtoken  = base64_encode($encrypted_data);
print "PHP generated token: " . $phpgeneratedtoken."   ";
// return
// In6uDpDqt1g=
// Decode the token just generated by php
$decoded = mcrypt_decrypt($alg,$key,base64_decode("In6uDpDqt1g="),$mode);
print "Decoded from php generated token:" . $decoded."    ";
//return
//1234567
// This is the encrypted token generated by java with same key and value
$javageneratedtoken  = "Cg8qY4gRMaI=";
// Decode the token generated by Java
$decoded = mcrypt_decrypt($alg,$key,base64_decode("Cg8qY4gRMaI="),$mode);
print "Decoded from Java Generated token: " . $decoded."    ";
// return
// 1234567
// Both tokens generated by java and php, are decrypted back to the same value.

爪哇代码:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class BlowfishTest {
    public static void main(String[] args) throws Exception {
        encrypt("1234567");
        decrypt("In6uDpDqt1g=");
    }
    private static void encrypt(String password) throws Exception {
        byte[] keyData = ("ABC").getBytes();
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyData, "Blowfish");
        Cipher cipher = Cipher.getInstance("Blowfish");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] hasil = cipher.doFinal(password.getBytes());
        System.out.println(new BASE64Encoder().encode(hasil));
    }
    private static void decrypt(String string) throws Exception {
        byte[] keyData = ("ABC").getBytes();
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyData, "Blowfish");
        Cipher cipher = Cipher.getInstance("blowfish/ecb/nopadding");
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        byte[] hasil = cipher.doFinal(new BASE64Decoder().decodeBuffer(string));
        System.out.println(new String(hasil));
    }
}

PHP 生成的加密值为:In6uDpDqt1g=

Java 生成的加密值为:Cg8qY4gRMaI=

问题出在 Java 代码

Cipher cipher = Cipher.getInstance("blowfish");

我需要找到一种方法来使我的PHP生成的加密值与Java生成的加密值相同。

这两个加密值我都可以在 PHP 中解密它们。两个加密值我也可以在java中解密它们,只有当我设置时,

Cipher cipher = Cipher.getInstance("blowfish/ecb/nopadding");

但是当我尝试在 Java 中解密 PHP 加密值时,In6uDpDqt1g=

如果我设置,

Cipher cipher = Cipher.getInstance("blowfish");

我收到错误:

"鉴于最终块未正确填充"。

问题是我应该使用 PHP 来加密值,而我的客户端会使用 java 来解密值。使用设置Cipher cipher = Cipher.getInstance("blowfish")解密我的值。

所以我想找到一种方法,我应该使用 PHP 来获得与 Java 相同的加密值,Cipher cipher = Cipher.getInstance("Blowfish") 会得到。

如果没有这样的解决方案,那么我将不得不要求我的客户更改他的 java 代码才能使用

Cipher cipher = Cipher.getInstance("blowfish/ecb/nopadding");

好的,我找到了答案。我需要将我的 php 代码更改为

$key  = 'ABC';
$data = '1234567';
$alg  = MCRYPT_BLOWFISH;
$mode = MCRYPT_MODE_ECB;
$blocksize = mcrypt_get_block_size('blowfish', 'ecb'); // get block size 
$pkcs = $blocksize - (strlen($data) % $blocksize); // get pkcs5 pad length 
$data.= str_repeat(chr($pkcs), $pkcs);
$encrypted_data = mcrypt_encrypt($alg, $key, $data, $mode);
$phpgeneratedtoken  = base64_encode($encrypted_data);
print "PHP generated token: " . $phpgeneratedtoken."   ";
// return
// Cg8qY4gRMaI=