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 IvParameterSpec iv;
// 用户信息
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
*/
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));
}
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);
}
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");
}
return source.replace("+", "-").replace("/", "_");
}
}
aW1wb3J0IGphdmEuaW8uVW5zdXBwb3J0ZWRFbmNvZGluZ0V4Y2VwdGlvbjsKaW1wb3J0IGphdmEuc2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