Understand java and php, aes encryption and decryption convert java to php

Source: Internet
Author: User
* ** AES encryption ** @ paramkey * key Information * @ paramcontent * information to be encrypted * publicstaticbyte [] encodeAES (byte [] key, byte [] content) throwsException {not a multiple of 16, complement intbase16; if (key. length % base! 0) {intgroupskey. lengthbase + (key. length %... phpjavaaes

/**
* AES encryption
*
* @ Param key
* Key Information
* @ Param content
* Information to be encrypted
*/
Public static byte [] encodeAES (byte [] key, byte [] content) throws Exception {
// Not a multiple of 16, complement
Int base = 16;
If (key. length % base! = 0 ){
Int groups = key. length/base + (key. length % base! = 0? 1: 0 );
Byte [] temp = new byte [groups * base];
Arrays. fill (temp, (byte) 0 );
System. arraycopy (key, 0, temp, 0, key. length );
Key = temp;
}

    SecretKey secretKey = new SecretKeySpec(key, "AES");    IvParameterSpec iv = new IvParameterSpec(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");    cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);    byte[] tgtBytes = cipher.doFinal(content);    return tgtBytes;}

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.