Blowfish 加密 - 在 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.

Java代码:

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");

我收到错误:

"Given final block not properly padded".

问题是我应该使用 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=