PHP AES encryption corresponds to Java sha1prng mode encryption

Source: Internet
Author: User
Tags decrypt sha1

When doing docking, the service provider AES encryption through the SHA1PRNG algorithm (as long as the password, each generation of the array is the same, so can be used to do encryption and decryption key) for another time encryption, engaged for several hours, directly see the corresponding code bar, you can refer to, Java-only encrypted source

Private Static byte[] Encrypt (byte[] bytecontent,byte[] password)throwsException {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=NewSecretkeyspec (Encodeformat, "AES"); Cipher Cipher= Cipher.getinstance ("AES"); Cipher.init (1, key); byte[] result =cipher.dofinal (bytecontent); returnresult; }
  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; The Public function encrypt ($encrypt, $key) {$blockSize = Mcrypt_get_block_size (mcrypt_rijndael_128, Mcrypt_mo        DE_ECB);        $paddedData = $this->_pkcs5pad ($encrypt, $blockSize);        $ivSize = Mcrypt_get_iv_size (mcrypt_rijndael_128, MCRYPT_MODE_ECB);        $iv = Mcrypt_create_iv ($ivSize, Mcrypt_rand);        $key 2 = substr (Openssl_digest (Openssl_digest ($key, ' SHA1 ', true), ' SHA1 ', true), 0, 16);        $encrypted = Mcrypt_encrypt (mcrypt_rijndael_128, $key 2, $paddedData, MCRYPT_MODE_ECB, $IV); Return Base64_encode ($encrypted);        The 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; }

  

PHP AES encryption corresponds to Java sha1prng mode encryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.