package cn.chinaunicom.changyue.util;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.lang.StringUtils;
/**
* : AES<br/>
* : AES<br/>
* : <br/>
* : 2013-6-25<br/>
*
* : ()
*/
public class AESUtils {
/** */
public static final String ALGORITHM = "AES";
/** ECB */
public static final String PATTERN_ECB = "ECB";
/** CBC */
public static final String PATTERN_CBC = "CBC";
/** PKCS5Padding */
public static final String PADDING_PKCS5 = "PKCS5Padding";
public static final String key = "Ft08QZ9fsFUm1meK";
public static final String sp = "Fa08QZ9fsFUm1meK";
/**
* :
*
* @return
* @throws Exception
*/
public static String encrypt(String original) throws Exception {
if (StringUtils.isEmpty(original)) {
return null;
}
Cipher cipher = null;
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), ALGORITHM);
cipher = Cipher.getInstance(ALGORITHM + "/" + PATTERN_CBC + "/" + PADDING_PKCS5);
IvParameterSpec iv = new IvParameterSpec(sp.getBytes("UTF-8"));// CBCiv
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
return Base64.encode(cipher.doFinal(original.getBytes("utf-8")));
}
public static String decrypt(String encrypted) throws Exception {
if (StringUtils.isEmpty(encrypted)) {
return null;
}
Cipher cipher = null;
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("utf-8"), ALGORITHM);
cipher = Cipher.getInstance(ALGORITHM + "/" + PATTERN_CBC + "/" + PADDING_PKCS5);
IvParameterSpec iv = new IvParameterSpec(sp.getBytes("utf-8"));// CBCiv
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] byteDecrypted = cipher.doFinal(Base64.decode(encrypted));
return new String(byteDecrypted, "utf-8").trim();
}
public static void main(String[] args) throws Exception {
String vi = "Fa08QZ9fsFUm1meK";
String string = AESUtils.encrypt("15620587360");
System.out.println(string);
String string2 = AESUtils.decrypt(string);
System.out.println(string2);
}
}
the encrypted format has%, but there is no equal sign. How to configure this in js
AES encryption and decryption of js found on the Internet, and I don"t know how to configure it for a long time
var key = CryptoJS.enc.Utf8.parse("Ft08QZ9fsFUm1meK");
var iv = CryptoJS.enc.Utf8.parse("Fa08QZ9fsFUm1meK");
function Encrypt(word) {
srcs = CryptoJS.enc.Utf8.parse(word);
var encrypted = CryptoJS.AES.encrypt(srcs, key, {iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7});
return encrypted.ciphertext.toString().toUpperCase();
}
function Decrypt(word) {
var encryptedHexStr = CryptoJS.enc.Hex.parse(word);
var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
var decrypt = CryptoJS.AES.decrypt(srcs, key, {iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7});
var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr.toString();
}
var mm = Encrypt("18634332463")
console.log(mm);
var jm = Decrypt(mm);
console.log(jm)
the background also tells you that you have been cheated through encodeURI, and how to delete the problem.