Home >Backend Development >PHP Tutorial >Detailed explanation of examples of AES encryption corresponding to SHA1PRNG encryption in Java in PHP

Detailed explanation of examples of AES encryption corresponding to SHA1PRNG encryption in Java in PHP

黄舟
黄舟Original
2017-07-27 15:02:046185browse

When doing the docking, the AES encryption done by the service provider was encrypted again through the SHA1PRNG algorithm (as long as the password is the same, the array generated each time is the same, so it can be used as the key for encryption and decryption), and it was done. For several hours, just look at the corresponding code. You can refer to it. There is only Java encryption source code


private static byte[] encrypt(byte[] byteContent, byte[] password) throws Exception
  {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
    secureRandom.setSeed(password);
    kgen.init(128, secureRandom);
    SecretKey secretKey = kgen.generateKey();    byte[] enCodeFormat = secretKey.getEncoded();
    SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(1, key);    byte[] result = cipher.doFinal(byteContent);    return result;
  }


  private function _pkcs5Pad($text, $blockSize)
    {
        $pad = $blockSize - (strlen($text) % $blockSize);
        return $text . str_repeat(chr($pad), $pad);
    }

    private function _pkcs5Unpad($text)
    {
        $end = substr($text, -1);
        $last = ord($end);
        $len = strlen($text) - $last;
        if(substr($text, $len) == str_repeat($end, $last))
        {
            return substr($text, 0, $len);
        }
        return false;
    }

    public function encrypt($encrypt, $key)
    {
        $blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
        $paddedData = $this->_pkcs5Pad($encrypt, $blockSize);
        $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
        $iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
        $key2 = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
        $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key2, $paddedData, MCRYPT_MODE_ECB, $iv);
        return base64_encode($encrypted);
    }

    public function decrypt($decrypt, $key)
    {
        $decoded = $this->hex2bin($decrypt);
        $blockSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
        $iv = mcrypt_create_iv($blockSize, MCRYPT_RAND);
        $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $decoded, MCRYPT_MODE_ECB, $iv);
        return $this->_pkcs5Unpad($decrypted);
    }

    function hex2bin($str)
    {
        $sbin = "";
        $len = strlen($str);
        for($i = 0; $i < $len; $i += 2)
        {
            $sbin .= pack("H*", substr($str, $i, 2));
        }

        return $sbin;
    }

The above is the detailed content of Detailed explanation of examples of AES encryption corresponding to SHA1PRNG encryption in Java in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn