import com.qiniu.util.Base64; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.security.spec.AlgorithmParameterSpec;

/ * *

  • AES is a reversible encryption algorithm that encrypts sensitive user information
  • After AES encryption of the original data, Base64 encoding is transformed.
  • @author
  • @time 2017/11/27 15:54
  • Fill: ZeropAdding 128 bits
  • Pattern: CBC
  • Password: Kbj. 543 lbwb5knbp
  • Offset: B* L032YKLs9-G858

/ public class AesCBCUtil {// Private key AES The fixed format is 128/192/256 bits. 16/24/32bytes public static final String APP_LOGIN_IN_KEY = “kbJ.543lbwb5KNbp “; 16/24/32bytes public static final String APP_LOGIN_IN_KEY =” kbJ.543lbwb5KNbp “; Public static final String GIV = “bl032yKLs9-g858 “;

/**
 * CBC
 *
 * @param data
 * @param key
 * @return
 * @throws Exception
 */
public static String encrypt(String data, String key) {
Copy the code

// Logger.getInstace().i(“[Data-Original]”, data); try { Cipher cipher = Cipher.getInstance(“AES/CBC/NoPadding”); int blockSize = cipher.getBlockSize();

byte[] dataBytes = data.getBytes(); int plaintextLength = dataBytes.length; if (plaintextLength % blockSize ! = 0) { plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize)); } byte[] plaintext = new byte[plaintextLength]; System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivspec = new IvParameterSpec(GIV.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); byte[] encrypted = cipher.doFinal(plaintext); //return new sun.misc.BASE64Encoder().encode(encrypted); String datakey = Base64.encodeToString(encrypted, 0) .replace("\n", "").trim();Copy the code

// Logger.getInstace().i(“[Data-AesCBC]”, datakey); return datakey; } catch (Exception e) { e.printStackTrace(); return null; }}

/** * replaces the specified string */Copy the code

// public static String replaceStr(String str) { // return str.replace(“+”, “__2B”).replace(“/”, “__2F”).replace(“=”, “__2C”); // }

Public static String AES_Decode(String STR, String key) {try {Copy the code

// str = replaceEnStr(str); byte[] textBytes = Base64.decode(str, 0); // byte[] textBytes = str.getBytes(“UTF-8”); AlgorithmParameterSpec ivSpec = new IvParameterSpec(GIV.getBytes(“UTF-8”)); SecretKeySpec newKey = new SecretKeySpec(key.getBytes(“UTF-8”), “AES”); Cipher cipher = Cipher.getInstance(“AES/CBC/NoPadding”); cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec); return new String(cipher.doFinal(textBytes), “UTF-8”).replace(“\n”, “”).trim(); } catch (Exception e) { e.printStackTrace(); return null; }}

/** * replaces the specified string */Copy the code

// public static String replaceEnStr(String str) { // return str.replaceAll(“__2B”, “+”).replaceAll(“__2F”, “/”).replaceAll(“__2C”, “=”); // } public static void main(String[] args) { //866297039791111 String str = “40p9Gs8NBkxCChR8UgoU/KXkOnn7qs5yzdP52B4iT231uTcrst3fCAezj7Gmkz+yC6EGBWyCp+U+iRJKtN3nT3WuSWh7iWn25svTVsFS23SE7+91oJPjit3 n26FlwE9soFP8WUYkeek//TemeBIWD0bIfhJ0IgfyL77A7muYwnF/Yy/CTwY0G7gpjhCOz73hHMXr2bpDngciNSkB9R7WMnlRirl+ouE4PFZ+98j4clhFzHz WAxOqhmtVc8j1T+WL1e+NPmwyfNJVPTNlA4kQcwSuWWO+KLv7KMgG0OMKmkKfL3vXUgZ2/cRV9xLV1zsClNpBNqZybV1YFGXKLlHIQ70XKNbR1hT06kjASQN /GdN12LVTbQqfLp/ffViDylgzHWzYt/TD8Ai8aM3gA4I7mqT44gpOt+CtxdtTJFXxiz4Ht/WxU1dssiCjnnomc8CuXq3p2wT5mLomhR/79BteQiFfSqLa6ga mtYyyche0NFTalnXas4D11yqJ1wP9NOSEiKIZrDRXY0jxmhjUemkCoQKHfbhSOwoWj7y93bV/7zE+AFRv/tmfxxSmqpc0VENNiwM+yeXtzN5ryQBLy6Wzp9N rzEta9g9/ARylBnesxCTkotMiYS32No84T17XjIRemu03iuawMAMKQ02YofcJT7wPU3CAcTO6jQ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jPtJl0N6TDpkoZTfIsNzY7sB6A89FQYB09II9AQQ6F6/bpfdeDpMyvDhEskuQuAdi lLFhVCLxUYlZHgpHO7kx2CfSyynPF7IZzDjwnVVb2nqdpDylxKc5uFGKRlOMidvxytlCqYO9kZoOPy1zSR1qe3uvogHAUmfwjKnSu5+MId4Z73dH2p9wCzvC fs/QTrskIHXdaz3SY9h4Yznd2FX6/mqsTnrnz4vGn+6F4kiEBOpqeVyjFvnKaHTZmhYIp1gxl8Qas+sYQm+3rgjTdb3AjUGK11x8UkBGwyJuowXGLv4JaXo7 +HA4GBZQMdzPDgl+oY+mvg6bejKdtQ8Uaaj55Xsw6TNyzCaIU5bNzpidyvPHpbiQwK/jaI/AutOuJmaCbPKXCfZMBm8aYdVczO2EA9LWZiOsMXtE3dIqIXaI +N+EvrPJ+F4NoujypNGDJCgmPlhE6QqONa6WOdw==”; String en = AesCBCUtil.encrypt(str, “Kbj.543Lbwb5kNbP”); System.out.println(” en:” + en);

String dr = AesCBCUtil.AES_Decode(str, "Kbj.543Lbwb5kNbP"); System.out.println(" Dr :" + Dr); }Copy the code

}