|
AES加密解密就不说了,网上百度一堆。需要注意的是java与php互通要选择
AES/CBC/NoPadding 下面还是只接上代码吧
package com.zns.crypto;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/**
* Created by Administrator on 2017/1/18 0018.
*/
public class AES {
/**
* AES加密
* @param key 加密需要的KEY
* @param iv 加密需要的向量
* @param data 需要加密的数据
* @return
*/
public static String encrypt(String key, String iv, String data) {
byte[] encrypted = {};
byte[] enCodeFormat = key.getBytes();
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat,"AES");
try {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec,new IvParameterSpec(iv.getBytes()));
int blockSize = cipher.getBlockSize();
System.out.println(data.length());
byte[] dataBytes = data.getBytes();
int plaintextLength = dataBytes.length;
if (plaintextLength % blockSize != 0) {
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
}
System.out.println(plaintextLength);
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
encrypted = cipher.doFinal(plaintext);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return new String(Base64.encodeBase64(encrypted));
}
/**
*
* @param key 解密需要的KEY 同加密
* @param iv 解密需要的向量 同加密
* @param data 需要解密的数据
* @return
*/
public static String decrypt(String key,String iv,byte[] data){
String content = "";
byte[] enCodeFormat = key.getBytes();
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = null;// 创建密码器
try {
cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec,new IvParameterSpec(iv.getBytes()));// 初始化
byte[] result = cipher.doFinal(data);
content = new String(result);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return content; // 加密
}
} 测试用例:
public class Main {
public static void main(String[] args) {
String key = DigestUtils.sha1Hex("123456").substring(0,16);
String iv = "1234567890123456";
String data = "steven11111";
String encryptData = AES.encrypt(key,iv ,data );
System.out.println(encryptData);
String decryptData = AES.decrypt(key,iv,Base64.decodeBase64(encryptData));
System.out.println(decryptData);
}
}
PHP的代码:
crypt方式: |
|
|