Encryption and decryption-Java AES class, can be implemented using php, turning to the php program that understands Java code

Source: Internet
Author: User
Java code: {code ...} background: The target platform language is Java, and the parameters must be encrypted. The returned data is also encrypted and then returned. My language is a php problem: can this Javaaes class be implemented using php? If yes, for more information, see. Php code I wrote: {code... Java code:

import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;public class AESSecurityUtil {    private static final String AES = "AES";      private static final String CHARSET_NAME = "utf-8";      private static SecretKeySpec getKey(String password) throws NoSuchAlgorithmException{          KeyGenerator kgen = KeyGenerator.getInstance(AES);          SecureRandom random=SecureRandom.getInstance("SHA1PRNG");        random.setSeed(password.getBytes());        kgen.init(128, random);            SecretKey secretKey = kgen.generateKey();          byte[] enCodeFormat = secretKey.getEncoded();            SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES);           return key;      }      public static String encode(String str, String password)      {          byte[] arr = encodeToArr(str, password);          return byteArrToString(arr);      }      private static byte[] encodeToArr(String str, String password)      {          try        {              Cipher cipher = Cipher.getInstance(AES);            byte[] byteContent = str.getBytes(CHARSET_NAME);              cipher.init(Cipher.ENCRYPT_MODE, getKey(password));            byte[] result = cipher.doFinal(byteContent);                return result;          }          catch (Exception e)          {              e.printStackTrace();          }            return null;      }      public static String decode(String hexStr, String password){          byte[] arr = string2ByteArr(hexStr);          return decode(arr, password);      }      private static String decode(byte[] arr, String password)  {          try{              Cipher cipher = Cipher.getInstance(AES);              cipher.init(Cipher.DECRYPT_MODE, getKey(password));            byte[] result = cipher.doFinal(arr);              return new String(result, CHARSET_NAME);          }catch (Exception e){              e.printStackTrace();          }          return null;      }      private static String byteArrToString(byte[] arr)  {          StringBuffer sb = new StringBuffer();           for (int i = 0; i 

Background: The target platform language is Java, and the parameters must be encrypted. The returned data is also encrypted and then returned. My language is php.
Question: Can this Java aes class be implemented using php? If so, how can this be implemented? Please advise.

Php code I wrote:

 _cipher, $this->_mode); $paddedData = $this->_pkcs5Pad($encrypt, $blockSize); $ivSize = mcrypt_get_iv_size($this->_cipher, $this->_mode); $iv = mcrypt_create_iv($ivSize, MCRYPT_RAND); $encrypted = mcrypt_encrypt($this->_cipher, $key, $paddedData, $this->_mode, $iv); return bin2hex($encrypted); } public function decrypt($decrypt, $key) { $decoded = hex2bin($decrypt); $blockSize = mcrypt_get_iv_size($this->_cipher, $this->_mode); $iv = mcrypt_create_iv($blockSize, MCRYPT_RAND); $decrypted = mcrypt_decrypt($this->_cipher, $key, $decoded, $this->_mode, $iv); return $this->_pkcs5Unpad($decrypted); }}$keyStr = 'UITN25LMUQC436IM';$plainText = 'this is a string will be AES_Encrypt';$aes = new Util_AesEncrypt();$encText = $aes->encrypt($plainText, $keyStr);$decString = $aes->decrypt($encText, $keyStr);echo $encText, "\n", $decString;

The running results of the two are very different, and there is no way to encrypt and decrypt each other. How should we correct them?

Reply content:

Java code:

import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;public class AESSecurityUtil {    private static final String AES = "AES";      private static final String CHARSET_NAME = "utf-8";      private static SecretKeySpec getKey(String password) throws NoSuchAlgorithmException{          KeyGenerator kgen = KeyGenerator.getInstance(AES);          SecureRandom random=SecureRandom.getInstance("SHA1PRNG");        random.setSeed(password.getBytes());        kgen.init(128, random);            SecretKey secretKey = kgen.generateKey();          byte[] enCodeFormat = secretKey.getEncoded();            SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES);           return key;      }      public static String encode(String str, String password)      {          byte[] arr = encodeToArr(str, password);          return byteArrToString(arr);      }      private static byte[] encodeToArr(String str, String password)      {          try        {              Cipher cipher = Cipher.getInstance(AES);            byte[] byteContent = str.getBytes(CHARSET_NAME);              cipher.init(Cipher.ENCRYPT_MODE, getKey(password));            byte[] result = cipher.doFinal(byteContent);                return result;          }          catch (Exception e)          {              e.printStackTrace();          }            return null;      }      public static String decode(String hexStr, String password){          byte[] arr = string2ByteArr(hexStr);          return decode(arr, password);      }      private static String decode(byte[] arr, String password)  {          try{              Cipher cipher = Cipher.getInstance(AES);              cipher.init(Cipher.DECRYPT_MODE, getKey(password));            byte[] result = cipher.doFinal(arr);              return new String(result, CHARSET_NAME);          }catch (Exception e){              e.printStackTrace();          }          return null;      }      private static String byteArrToString(byte[] arr)  {          StringBuffer sb = new StringBuffer();           for (int i = 0; i 

Background: The target platform language is Java, and the parameters must be encrypted. The returned data is also encrypted and then returned. My language is php.
Question: Can this Java aes class be implemented using php? If so, how can this be implemented? Please advise.

Php code I wrote:

 _cipher, $this->_mode); $paddedData = $this->_pkcs5Pad($encrypt, $blockSize); $ivSize = mcrypt_get_iv_size($this->_cipher, $this->_mode); $iv = mcrypt_create_iv($ivSize, MCRYPT_RAND); $encrypted = mcrypt_encrypt($this->_cipher, $key, $paddedData, $this->_mode, $iv); return bin2hex($encrypted); } public function decrypt($decrypt, $key) { $decoded = hex2bin($decrypt); $blockSize = mcrypt_get_iv_size($this->_cipher, $this->_mode); $iv = mcrypt_create_iv($blockSize, MCRYPT_RAND); $decrypted = mcrypt_decrypt($this->_cipher, $key, $decoded, $this->_mode, $iv); return $this->_pkcs5Unpad($decrypted); }}$keyStr = 'UITN25LMUQC436IM';$plainText = 'this is a string will be AES_Encrypt';$aes = new Util_AesEncrypt();$encText = $aes->encrypt($plainText, $keyStr);$decString = $aes->decrypt($encText, $keyStr);echo $encText, "\n", $decString;

The running results of the two are very different, and there is no way to encrypt and decrypt each other. How should we correct them?

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.