一、aksk生成工具类
public class SignUtil{
private static final String[] chars = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
/**
* 生成8位app_key
*/
public static String getAppKey(){
StringBuffer buffer = new StringBuffer();
String uuid = UUID.randomUUID().toString().replace("-", "");
for(int i = 0; i < 8; i++){
String str = uuid.substring(i * 4, i * 4 + 4);
int x = Integer.parseInt(str, 16);
buffer.append(chars[x % 0x3e]);
}
return buffer.toString();
}
/**
* 生成24位app_secret
*/
public static String getAppSecret(String appKey){
String appSecret = AESUtil.encrypt(appKey, AESUtil.PASSWORD);
return appSecret;
}
}
public class AESUtil{
private static final String ENCODE = "UTF-8";
private static final String KEY_ALGORITHM = "AES";
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
public static final String PASSWORD = "PUPPA%^888";
/**
* AES加密
*/
public static String encrypt(String content, String password){
try{
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
byte[] byteContent = content.getBytes(ENCODE);
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(password));
byte[] result = cipher.doFinal(byteContent);
return new String(Base64.getEncoder().encode(result));
} catch(Exception ex){
ex.printStackTrace();
}
return null;
}
/**
* AES解密
*/
public static String decrypt(String content, String password){
try{
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(password));
byte[] result = cipher.doFinal(Base64.getDecoder().decode(content.getBytes()));
return new String(result, ENCODE);
} catch (Exception ex){
ex.printStackTrace();
}
return null;
}
private static Key getSecretKey(String password) throws NoSuchAlgorithmException{
KeyGenerator keyGenerator = null;
// 修改生成密钥的方法
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(password.getBytes());
try{
keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
// AES要求密钥长度位128
keyGenerator.init(128, random);
// 生成一个密钥
SecretKey secretKey = keyGenerator.generateKey();
return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);
} catch (NoSuchAlgorithmException ex){
ex.printStackTrace();
}
return null;
}
}
二、aksk加密算法
public class SignUtil{
private static final String SignAlgorithm = "HmacSHA256";
public static String sign(String secret, String content){
try{
Mac mac = Mac.getInstance(SignAlgorithm);
mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), SignAlgorithm));
String data = BinaryUtil.toHex(hash(content));
byte[] signByte = mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
return BinaryUtil.toHex(signByte);
} catch(NoSuchAlgorithmException | InvalidKeyException ex){
return null;
}
}
public static byte[] hash(String text){
try{
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(text.getBytes(StandardCharsets.UTF_8));
return md.digest();
} catch(NoSuchAlgorithmException ex){
return null;
}
}
}
public class BinaryUtil{
public static String toHex(byte[] data){
StringBuilder sb = new StringBuilder(data.length * 2);
for(byte b : data){
String hex = Integer.toHexString(b);
if(hex.length() == 1){
sb.append("0");
} else if(hex.length() == 8){
hex = hex.substring(6);
}
sb.append(hex);
}
return sb.toString().toLowerCase(Locale.getDefault());
}
}
版权声明:本文为yujiubo2008原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。