fork(6) download
import java.io.UnsupportedEncodingException;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Encoder;

class AES {
  static Cipher cipher;
  static final String KEY_ALGORITHM = "AES";
  static final String CIPHER_ALGORITHM_CBC = "AES/CBC/PKCS5Padding";
  static final String CIPHER_ALGORITHM_ECB = "AES/ECB/PKCS5Padding";
  static Key secretKey;
  static IvParameterSpec iv;
    
  public static void main(String[] args) throws Exception {
    // 用户信息
    String customerData = "{\"uid\":\"test@youhaosuda.com\",\"type\":\"email\",\"name\":\"test\"}";
    // 接入密钥
    String strKey = "F789BF878B624AE3BA6A99B33BBCCFF6";
    // 店铺主域名
    String shopDomain = "youhaosuda.com";
    String strEncrypt = aesEncrypt(strKey, customerData);
    String redirectUrl = "http://" + shopDomain + "/account/multipass/login/" + strEncrypt;

    System.out.println("customerData: " + customerData);
    System.out.println("customerDataEncrypt: " + strEncrypt);
    System.out.println("redirectUrl: " + redirectUrl);
  }
  
  /**
   * 使用AES 算法 加密,默认模式 AES/CBC/PKCS5Padding
   */
  static String aesEncrypt(String strKey, String strData) throws Exception {
    cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
    secretKey = getKey(strKey);
    iv = getIV(strKey);

    cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
    byte[] encrypt = cipher.doFinal(strData.getBytes());
    return urlsafeBase64(new BASE64Encoder().encode(encrypt));
  }
  
  static IvParameterSpec getIV(String strIv) throws UnsupportedEncodingException {
    byte[] arrBTmp = strIv.getBytes("UTF-8");
    byte[] arrB = new byte[16];
    for (int i = 0; i + 16 < arrBTmp.length && i < arrB.length; i++) {
        arrB[i] = arrBTmp[i + 16];
    }
    return new IvParameterSpec(arrB);
  }
  
  static Key getKey(String strKey) throws Exception {
    byte[] arrBTmp = strKey.getBytes("UTF-8");
    byte[] arrB = new byte[16];
    for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
        arrB[i] = arrBTmp[i];
    }
    return new SecretKeySpec(arrB, "AES");
  }

  static String urlsafeBase64(String source) throws Exception {
    return source.replace("+", "-").replace("/", "_");
  }
}
Success #stdin #stdout 0.5s 321600KB
stdin
Standard input is empty
stdout
customerData: {"uid":"test@youhaosuda.com","type":"email","name":"test"}
customerDataEncrypt: 17FErOXQwR8IC2FFxESVkFcdUra_eFXyDpjYuKcP6MgrVsJxv4_dmu5HzgtqROup6YzVAMXakItC
IhN7_gIoFA==
redirectUrl: http://y...content-available-to-author-only...a.com/account/multipass/login/17FErOXQwR8IC2FFxESVkFcdUra_eFXyDpjYuKcP6MgrVsJxv4_dmu5HzgtqROup6YzVAMXakItC
IhN7_gIoFA==