gpt4 book ai didi

java - 密码/3DES/CFB/Java 和 PHP

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:30:03 26 4
gpt4 key购买 nike

我有一个 PHP 伺服器,它使用 CFB 模式在 3DES 中解密数据

我用 PHP 加密:

$montant = "500";
$message_crypte = mcrypt_encrypt(MCRYPT_3DES, "N4y1FRDRJ7wn7eJNnWaahCIS", $montant, ,CRYPT_MODE_CFB, "NCNPJDcR");
$montant = base64_encode($message_crypte);

PHP 中的此脚本可用于其他系统。

我想用 Java 加密:

public class CryptData {
private KeySpec keySpec;
private SecretKey key;
private IvParameterSpec iv;

public CryptData(String keyString, String ivString) {
try {
final MessageDigest md = MessageDigest.getInstance("md5");

final byte[] digestOfPassword = md.digest(Base64
.decodeBase64(keyString.getBytes("ISO-8859-1")));

final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
for (int j = 0, k = 16; j < 8;) {
keyBytes[k++] = keyBytes[j++];
}

//keySpec = new DESedeKeySpec(keyBytes);
keySpec = new DESedeKeySpec(keyString.getBytes());

key = SecretKeyFactory.getInstance("DESede")
.generateSecret(keySpec);

iv = new IvParameterSpec(ivString.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}

public String encrypt(String value) {
try {
Cipher ecipher = Cipher.getInstance("DESede/CFB/NoPadding");

//"SunJCE");
ecipher.init(Cipher.ENCRYPT_MODE, key, iv);

if (value == null)
return null;

// Encode the string into bytes using utf-8
byte[] valeur = value.getBytes("ISO-8859-1");
//byte[] utf8 = value.getBytes();

// Encrypt
byte[] enc = ecipher.doFinal(valeur);

// Encode bytes to base64 to get a string
return new String(Base64.encodeBase64(enc), "ISO-8859-1");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

我在 PHP 和 Java 中的结果不一样

如何修改Java处理以获得与PHP相同的结果?

最佳答案

答案是:

Cipher ecipher = Cipher.getInstance("DESede/CFB8/NoPadding");

我需要使用“CFB8”

关于java - 密码/3DES/CFB/Java 和 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10993335/

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