Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.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
AES CBC PKCS5P使用SecretKey从Java添加到Php_Java_Php_Aes - Fatal编程技术网

AES CBC PKCS5P使用SecretKey从Java添加到Php

AES CBC PKCS5P使用SecretKey从Java添加到Php,java,php,aes,Java,Php,Aes,我需要将此代码从Java转换为PHP: 我知道我应该按照PHP的要求将iv和SALT从字节转换为字符串 $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv_array_string); String password = "mypass"; String encoding = "UTF-8"; String cleanString = "text to encode"; byte[] salt_array = {(byte) 0x9

我需要将此代码从Java转换为PHP:

我知道我应该按照PHP的要求将iv和SALT从字节转换为字符串

$td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv_array_string);

String password = "mypass";
String encoding = "UTF-8";
String cleanString = "text to encode";

byte[] salt_array = {(byte) 0x98, (byte) 0x71, (byte) 0x1F, (byte) 0x71, (byte) 0x5D, (byte) 0x71, (byte) 0x28, (byte) 0x8F};

//Key
KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt_array, 16, 128);
SecretKey tmp = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1").generateSecret(keySpec);
SecretKey key = new SecretKeySpec(tmp.getEncoded(), "AES");

//ciphers
byte[] iv_array = {(byte) 0x98, (byte) 0x71, (byte) 0xF3, (byte) 0x52, (byte) 0x1A, (byte) 0x71, (byte) 0x38, (byte) 0x1F, (byte) 0x75, (byte) 0x1F, (byte) 0x1F, (byte) 0xE0, (byte) 0xEF, (byte) 0x39, (byte) 0x98, (byte) 0x1F};
Cipher encChiper = Cipher.getInstance("AES/CBC/PKCS5Padding");
AlgorithmParameterSpec params = new iv_arrayParameterSpec(iv_array);
encChiper.init(Cipher.ENCRYPT_MODE, key, params);
byte[] crypted = encChiper.doFinal(cleanString.getBytes(encoding));
//output encoded
String base64Crypted = new String(Base64.encodeBase64(crypted), encoding);

这里有一个可能的解决办法。在PHP=5.5中,函数hash_pbkdf2(..)


这不是真正的重复,这个问题更具体。非常感谢,这就是我需要的
<?php
     class CbcCrypt {

           private $iterations = 16;
           private $key_lenght = 16;
           private $password = "password";
           //parametro utilizzato da key per generare la chiave
           private $salt = array(0xA7, 0x71, 0x1F, 0xF5, 0x5D, 0xD2, 0x28, 0x8F);
           //parametro utilizzato dall'algoritmo per il cript
           private $iv = array(0xCB, 0x35, 0xF3, 0x52, 0x1A, 0xF7, 0x38, 0x0B, 0x75, 0x03, 0x8E, 0xE0, 0xEF, 0x39, 0x98, 0xC7);

           public function encrypt($data) {
               $ivStr = implode(array_map("chr", $this->iv));
               $saltStr = implode(array_map("chr", $this->salt));
               //key generator
               //$hash = hash_pbkdf2("sha1", $this->password, $saltStr, $this->iterations, $this->key_lenght, true);
               $hash = $this->pbkdf2($this->password, $saltStr, "sha1", $this->iterations, $this->key_lenght, true);
               $td = mcrypt_module_open('rijndael-128', '', 'cbc', $ivStr);
               //aggiunta del padding
               $toEncryptStrPadded = $this->pkcs5_pad($data);
               mcrypt_generic_init($td, $hash, $ivStr);
               $encrypted = mcrypt_generic($td, $toEncryptStrPadded);
               //print_r('base64 enc: ' . base64_encode($encrypted));
               mcrypt_generic_deinit($td);
               mcrypt_module_close($td);
               return base64_encode($encrypted);
           }

           function pbkdf2($password, $salt, $algorithm = 'sha512', $count = 20000, $key_length = 128, $raw_output = false) {
               if (!in_array($algorithm, hash_algos(), true)) {
                   exit;
               }
           if ($count <= 0 || $key_length <= 0) {
               $count = 20000;
               $key_length = 128;
           }

           $hash_length = strlen(hash($algorithm, "", true));
           $block_count = ceil($key_length / $hash_length);

           $output = "";
           for ($i = 1; $i <= $block_count; $i++) {
               $last = $salt . pack("N", $i);
               $last = $xorsum = hash_hmac($algorithm, $last, $password, true);
               for ($j = 1; $j < $count; $j++) {
                   $xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
               }
               $output .= $xorsum;
           }

           if ($raw_output) {
               return substr( $output, 0, $key_length );
           } else {
               return base64_encode(substr( $output, 0, $key_length ));
           }
       }

       function pkcs5_pad($text) {
           $blocksize = 16;
           $pad = $blocksize - (strlen( $text ) % $blocksize);
    }
}
?>