Encryption and decryption-use php to implement aes encryption in java

Source: Internet
Author: User
The aes encryption algorithm in java is as follows: {code...} How can I use php to implement? I have tested many online examples and cannot obtain the same ciphertext. the aes encryption algorithm in java is as follows:

    public static byte[] decrypt(byte[] data, byte[] key)            throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {        Security.addProvider(new BouncyCastleProvider());        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");        cipher.init(2, new SecretKeySpec(key, "AES"));        return cipher.doFinal(data);    }    public static byte[] encrypt(byte[] data, byte[] key)            throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {        Security.addProvider(new BouncyCastleProvider());        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");        cipher.init(1, new SecretKeySpec(key, "AES"));        return cipher.doFinal(data);    }

How can I implement it using php?
I tested a lot of online examples, but they cannot get the same ciphertext.

Reply content:

The aes encryption algorithm in java is as follows:

    public static byte[] decrypt(byte[] data, byte[] key)            throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {        Security.addProvider(new BouncyCastleProvider());        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");        cipher.init(2, new SecretKeySpec(key, "AES"));        return cipher.doFinal(data);    }    public static byte[] encrypt(byte[] data, byte[] key)            throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {        Security.addProvider(new BouncyCastleProvider());        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");        cipher.init(1, new SecretKeySpec(key, "AES"));        return cipher.doFinal(data);    }

How can I implement it using php?
I tested a lot of online examples, but they cannot get the same ciphertext.

It may be encoding problems. java uses GBK encoding by default;
The php encoding type is related to the page storage encoding.

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.