Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP上的DESede长键,而不是Java中的_Java_Php_Cryptography_3des - Fatal编程技术网

PHP上的DESede长键,而不是Java中的

PHP上的DESede长键,而不是Java中的,java,php,cryptography,3des,Java,Php,Cryptography,3des,我正在尝试将Java的DESede解密转换为PHP版本。但是,对于相同的输入,PHP不能提供相同的输出 爪哇: 给定以下输入参数,PHP无法提供相同的输出: $key = 'ASDFasdf12348983jklasdfJ2Jaf8'; $encrypted_data = 'cPap7+JIPS4='; 应将其解密为: coRef=3 Java的测试代码如下: try { String encryptedStr = encrypted_data; // same value as PH

我正在尝试将Java的
DESede
解密转换为PHP版本。但是,对于相同的输入,PHP不能提供相同的输出

爪哇:

给定以下输入参数,PHP无法提供相同的输出:

$key = 'ASDFasdf12348983jklasdfJ2Jaf8';
$encrypted_data = 'cPap7+JIPS4=';
应将其解密为:

coRef=3

Java的测试代码如下:

try {
    String encryptedStr = encrypted_data; // same value as PHP's $encrypted_data
    String decryptedString = "";
    ThreeDES desedeEncrypter = new ThreeDES("DSEede", key); // same value as PHP's $key
    decryptedString = desedeEncrypter.decrypt(encryptedStr);
    System.out.println(decryptedString);
} catch (ThreeDES.EncryptionException e) {
    e.printStackTrace();
}
输出:
coRef=3
。但是,下面的PHP代码提出了关于键长度的警告

echo decrypt($key, $encrypted_data);
此算法不支持大小为29的密钥。在…中仅支持大小为24的键

如何修改代码以使用长度超过24个字符的键?

这很奇怪

仅接受24个字节作为密钥

每个DES密钥名义上存储或传输为8个字节,每个字节 奇数奇偶校验[12],因此一个密钥包需要24字节用于选项1,16字节用于选项1 选项2或选项3的选项8

所以我认为问题在于

DESedeKeySpec
对象:

/**
     * Uses the first 24 bytes in <code>key</code> as the DES-EDE key.
     * <p>
     * The bytes that constitute the DES-EDE key are those between
     * <code>key[0]</code> and <code>key[23]</code> inclusive
     *
     * @param key the buffer with the DES-EDE key material.
     * @exception InvalidKeyException if the given key material is shorter
     * than 24 bytes.
*/
因此,我认为
DESedeKeySpec
是将29长度的键修剪为24,以符合
tribledes
的要求

编辑另一个重要注意事项是扩展已被弃用

从PHP7.1.0开始,此函数已被弃用。依靠这个 功能是非常不受欢迎的


长度为29的键在java中运行良好?是的,长度为29的键在java中运行良好。
echo decrypt($key, $encrypted_data);
/**
     * Uses the first 24 bytes in <code>key</code> as the DES-EDE key.
     * <p>
     * The bytes that constitute the DES-EDE key are those between
     * <code>key[0]</code> and <code>key[23]</code> inclusive
     *
     * @param key the buffer with the DES-EDE key material.
     * @exception InvalidKeyException if the given key material is shorter
     * than 24 bytes.
*/