gpt4 book ai didi

java - PHP 中的 256 位 AES 解密

转载 作者:行者123 更新时间:2023-12-01 11:01:44 24 4
gpt4 key购买 nike

我有一个 Java 工作示例,如何使用 key 解密(256 位 AES)字符串。我需要在 PHP 中复制解密算法并且几乎让它工作。

这是用 Java 解密的消息:

CSM(MCL/KEX RCV/10001031 ORG/Comdata KYN/dsNAX6hFtKvfceT KEY/C062E276949D83554D7B5198C52C4C55ED1C65370FA71F8220538A0C3BA23172)

然而,当我调用 PHP 方法时,它给出了

10001031 ORG/Comdata KYN/dsNAX6hFtKvfceT KEY/C062E276949D83554D7B5198C52C4C55ED1C65370FA71F8220538A0C3BA23172)

如您所见,距离并不遥远,但 PHP 方法不会返回前 17 个字符(即“CSM(MCL/KEX RCV/”位)。

我在这里错过了一些琐碎的事情吗?

这是 PHP 类:

class OpensslAES
{
const METHOD = 'aes-256-cbc';

public static function encrypt($message, $key)
{
if (mb_strlen($key, '8bit') !== 32) {
throw new Exception("Needs a 256-bit key!");
}
$ivsize = openssl_cipher_iv_length(self::METHOD);
$iv = openssl_random_pseudo_bytes($ivsize);

$ciphertext = openssl_encrypt(
$message,
self::METHOD,
$key,
OPENSSL_RAW_DATA,
$iv
);

return $iv.$ciphertext;
}

public static function decrypt($message, $key)
{
if (mb_strlen($key, '8bit') !== 32) {
throw new Exception("Needs a 256-bit key!");
}
$ivsize = openssl_cipher_iv_length(self::METHOD);
$iv = mb_substr($message, 0, $ivsize, '8bit');
$ciphertext = mb_substr($message, $ivsize, null, '8bit');

return openssl_decrypt(
$ciphertext,
self::METHOD,
$key,
OPENSSL_RAW_DATA,
$iv
);
}
}

$class = new OpensslAES();

var_dump($class->decrypt(base64_decode("cBVlMjBttr7DKW8fhHtqJOLyMBNrgxpIJsgFFPjkA/4MWxMIudOnYzS4WuxIhUjtgGgk4CzrkJ1G60R4OWBljNTMA9ATPKh9PXe7wXAwJfE9zc698bQv4lDkXRME+q4xCb3bK/UGQ/BPVIkmRYdHcBvIHXNzGd36Nn40giigY/g="), hex2bin("SECRET_KEY_GOES_HERE")));

这是 Java 类:

package javaapplication1;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;
import org.apache.commons.codec.binary.Hex;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import javax.xml.bind.DatatypeConverter;

public class JavaApplication1 {

public static void main(String[] args) {
byte[] ENCRYPTION_KEY = JavaApplication1.toByteArray("SECRET_KEY_GOES_HERE");
String INPUT = new String("cBVlMjBttr7DKW8fhHtqJOLyMBNrgxpIJsgFFPjkA/4MWxMIudOnYzS4WuxIhUjtgGgk4CzrkJ1G60R4OWBljNTMA9ATPKh9PXe7wXAwJfE9zc698bQv4lDkXRME+q4xCb3bK/UGQ/BPVIkmRYdHcBvIHXNzGd36Nn40giigY/g=");

try {
System.out.println(JavaApplication1.decryptStringAES(INPUT, ENCRYPTION_KEY));
} catch (Exception exception) {
System.out.println("Error occured: " + exception);
}
}

public static byte[] toByteArray(String s) {
return DatatypeConverter.parseHexBinary(s);
}

public static String decryptStringAES(String input, byte[] key) throws Exception {
byte[] IV = JavaApplication1.toByteArray("00000000000000000000000000000000");

byte[] inputBytes = Base64.decodeBase64(input.getBytes());
Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
decryptCipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new
IvParameterSpec(IV));
byte[] decrypt = decryptCipher.doFinal(inputBytes);
return new String(decrypt);
}
}

最佳答案

您的 Java 实现希望 IV 始终为零,而您的 PHP 实现则希望 IV 被添加到消息前面。

如果您希望 PHP 实现与 Java 代码兼容,那么您可以像这样更改它:

class OpensslAES
{
const METHOD = 'aes-256-cbc';

public static function encrypt($message, $key)
{
if (mb_strlen($key, '8bit') !== 32) {
throw new Exception("Needs a 256-bit key!");
}
$ivsize = openssl_cipher_iv_length(self::METHOD);
$iv = hex2bin('00000000000000000000000000000000');

return openssl_encrypt(
$message,
self::METHOD,
$key,
OPENSSL_RAW_DATA,
$iv
);
}

public static function decrypt($message, $key)
{
if (mb_strlen($key, '8bit') !== 32) {
throw new Exception("Needs a 256-bit key!");
}
$ivsize = openssl_cipher_iv_length(self::METHOD);
$iv = hex2bin('00000000000000000000000000000000');

return openssl_decrypt(
$message,
self::METHOD,
$key,
OPENSSL_RAW_DATA,
$iv
);
}
}

$class = new OpensslAES();

var_dump($class->decrypt(base64_decode("cBVlMjBttr7DKW8fhHtqJOLyMBNrgxpIJsgFFPjkA/4MWxMIudOnYzS4WuxIhUjtgGgk4CzrkJ1G60R4OWBljNTMA9ATPKh9PXe7wXAwJfE9zc698bQv4lDkXRME+q4xCb3bK/UGQ/BPVIkmRYdHcBvIHXNzGd36Nn40giigY/g="), hex2bin("SECRET_KEY_GOES_HERE")));

关于java - PHP 中的 256 位 AES 解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33284294/

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