PHP PEAR Crypt_CBC在java中的实现

I am trying to encrypt a value using the BLOWFISH cipher. The implementation must be in java and having the same result as the php version:

require_once('/usr/share/php/Crypt/CBC.php');
....
$cipher = new Crypt_CBC($key, 'BLOWFISH');
$encryptedPassword = $cipher->encrypt($valueToEncrypt);
echo "ENCODED STRING: " . $encryptedPassword;
$encodedPassword = base64_encode($encryptedPassword);
echo "ENCRYPTED STRING: " . $encodedPassword;

I tried something like this, but I cannot manage to get something even remotely similar:

SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "Blowfish");
String IV = "RandomIV";
Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
init(Cipher.ENCRYPT_MODE, keySpec
    , new javax.crypto.spec.IvParameterSpec(IV.getBytes())
);
byte[] encryptedBytes = cipher.doFinal(toEncrypt.getBytes());
String encoded =  Base64.getEncoder().encodeToString(encryptedBytes);

Any helping idea is appreciated

dpyu7978
dpyu7978 注意2点:1。Crypt_CBC构造函数从传递的密钥生成散列,如下所示:首先,从密钥创建MD5-Hash。如果该散列的长度小于keysize-property(56Byte)中指定的长度,则创建并附加已散列值的MD5散列。重复此过程,直到散列的长度>=56Byte。前56个字节用作键。2.Crypt_CBC-encrypt-method通过连接header_spec-property(default-value:RandomIV),(随机生成的)IV和加密数据的值来返回字符串。
一年多之前 回复
duan1396
duan1396 PHP代码将IV添加到密文。但即使剥离后,结果仍然不同。我仍然需要一整套输入/输出数据来分析,包括iv,键,输入和输出。
一年多之前 回复
donqo88682
donqo88682 对于“test12”,该值应该是类似的,如果不等于“UmFuZG9tSVZpEXCgXjR0cC256TF53Q57”/UmFuZG9tSVYlqdk15MVbSgmLAwKSNGHK/UmFuZG9tSVbDA3kPFVpPvv6MQ2LmK8UG(php);在java<iget“6riMPoAB9mZ1TrDOsOaAiw==”
一年多之前 回复
duandaodao6951
duandaodao6951 你可以添加你的php和java程序的输出,用于测试目的吗?
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐