java,php,js;AES 互通加解密
标签:commentpopoarnetkeywordjardownloadbase64tin
1,Java端(依赖 common-codec jar)
[java]view plaincopy
packagecom.jiaMi;
importjavax.crypto.Cipher;
importjavax.crypto.spec.IvParameterSpec;
importjavax.crypto.spec.SecretKeySpec;
importorg.apache.commons.codec.binary.Base64;
publicclassAESUtils{
privatefinalstaticStringKEY=\”1234123412341324\”;
privatefinalstaticStringIV=\”1234123412341234\”;
/**
*aes加密
*@paramdata
*@return
*/
publicstaticStringencryptData(Stringdata){
try{
Ciphercipher=Cipher.getInstance(\”AES/CBC/NoPadding\”);
intblockSize=cipher.getBlockSize();
byte[]dataBytes=data.getBytes();
intplaintextLength=dataBytes.length;
if(plaintextLength%blockSize!=0){
plaintextLength=plaintextLength+(blockSize-(plaintextLength%blockSize));
}
byte[]plaintext=newbyte[plaintextLength];
System.arraycopy(dataBytes,0,plaintext,0,dataBytes.length);
SecretKeySpeckeyspec=newSecretKeySpec(KEY.getBytes(),\”AES\”);
IvParameterSpecivspec=newIvParameterSpec(IV.getBytes());
cipher.init(Cipher.ENCRYPT_MODE,keyspec,ivspec);