gpt4 book ai didi

java - Blowfish 加密 - 在 php 和 java 中加密,我得到了不同的加密值

转载 作者:行者123 更新时间:2023-12-01 09:41:38 29 4
gpt4 key购买 nike

加密测试数据:

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=

关于java - Blowfish 加密 - 在 php 和 java 中加密,我得到了不同的加密值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38407929/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com