import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
public class main {
private static final String MD5_KEY = "szair_mobile_app";
private static final String SECURITY_KEY = "szair01234567890";
public static String base64Encode(byte[] crypted) {
return Base64.encodeToString(crypted, 8);
static{
Security.addProvider(new BouncyCastleProvider());
}catch(Exception e){
e.printStackTrace();
public static byte[] encrypt(byte[] origData, byte[] keyData) throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException {
byte[] cryptedData = null;
try {
SecretKeySpec key = new SecretKeySpec(keyData, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
cipher.init(1, key);
cryptedData = new byte[cipher.getOutputSize(origData.length)];
int ctLength = cipher.update(origData, 0, origData.length, cryptedData, 0);
int doFinal = cipher.doFinal(cryptedData, ctLength) + ctLength;
} catch (Exception e2) {
System.out.println(e2);
return cryptedData;
public static String getNewKey() {
return MD5Util.MD5Encode(MD5_KEY);
public static String getKey() throws NoSuchPaddingException, ShortBufferException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, NoSuchProviderException, InvalidKeyException {
return base64Encode(encrypt((getNewKey() + String.valueOf(((double) System.currentTimeMillis()) / 1000.0d)).getBytes(), SECURITY_KEY.getBytes())).replaceAll("\n", "");
public static void main(String[] args) throws NoSuchPaddingException, ShortBufferException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, NoSuchProviderException, InvalidKeyException {
String result = getKey();
System.out.println(result);
需要在maven 里面加个包
<dependencies>
<dependency>