微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法将文件图像、pdf 等使用 PBKDF2WithHmacSHA256 的 java 加密转换为 PHP 加密

如何解决无法将文件图像、pdf 等使用 PBKDF2WithHmacSHA256 的 java 加密转换为 PHP 加密

我有以下 JAVA 函数,我想将其转换为 PHP

JAVA 函数

private static final byte[] salt = {-44,-5,-88,82,116,-8,-64,-93};

private static  void doCrypto(int cipherMode,char[] key,File inputFile,File outputFile) throws CryptoException {
    try {
        //Returns a SecretKeyFactory object that converts secret keys of the specified algorithm.
        //get detail of 'PBKDF2WithHmacSHA256' (https://docs.oracle.com/javase/8/docs/technotes/guides/security/SunProviders.html)
        factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");

        //key specification
        //Constructor that takes a password,salt,iteration count,and to-be-derived key length for
        // generating PBEKey of variable-key-size PBE ciphers. An empty char[] is used if null is specified for password.
        KeySpec spec = new PBEKeySpec(key,1000,128);
        //Generates a SecretKey object from the provided key specification (key material).
        SecretKey tmp = factory.generateSecret(spec);

        //provide a encoded secretKey with algorithm
        secretKey = new SecretKeySpec(tmp.getEncoded(),"AES");

        //Returns a Cipher object that implements the specified transformation.
        cipher = Cipher.getInstance("AES") ; 
        cipher.init(cipherMode,secretKey); 
        FileInputStream inputStream = new FileInputStream(inputFile);
        byte[] inputBytes = new byte[(int) inputFile.length()];
        
        inputStream.read(inputBytes);
        byte[] outputBytes = cipher.doFinal(inputBytes);
         
        FileOutputStream outputStream = new FileOutputStream(outputFile);
        outputStream.write(outputBytes);
         
        inputStream.close();
        outputStream.close();
         
    } catch (Exception ex) {
        throw new CryptoException("Error encrypting/decrypting file",ex);
    }
}

以下是我翻译成的PHP函数

PHP 函数

function doCrypto( $mode,$key,$inputFile,$oputputFile  ){
    $key = mb_convert_encoding($key,"UTF-8");
    $salt = implode(array_map("chr",[ -44,-93 ] ));
    $IVbytes = NULL;
    $method = "AES-128-CBC";

    $hash = openssl_pbkdf2( $key,$salt,'128','1000','sha256' );

    $data = file_get_contents( $inputFile );

    if( 'enc' == $mode ){
        $result = openssl_encrypt($data,$method,$hash,1,$IVbytes);
    } else {
        $result = openssl_decrypt($data,$IVbytes);
    }

    if( !$result ){
        throw new Exception( "Error encrypting/decrypting file : " . openssl_error_string() );
    }
    file_put_contents( $oputputFile,$result );
    return true;
}

测试用例适用于 PHPPHP

但是当我从 java 接收加密文件并尝试在 PHP 中解密时

我不断收到以下错误 错误:0607A082:数字信封例程:EVP_CIPHER_CTX_set_key_length:密钥长度无效

我也尝试过使用二进制模式的 fopen 读取文件

$handle = fopen($inputFile,"rb");
$contents = fread($handle,filesize($inputFile)); 

谁能指出我在这里遗漏了什么。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。