import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Map;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import edu.emory.mathcs.backport.java.util.Arrays;
public class AesMqttEncryptor {
private static final String AES_KEY_ALGORITHM = "AES";
private static final String AES_ALGORITHM = "AES/ECB/PKCS5Padding";
private static final String ROOT_SECRET = "VMvNI3RZvAMdK5asMCwMqQ==";
//private final ConfigManager configManager;
private final SecretKey rootKey;
private volatile SecretKey dynamicKey;
public AesMqttEncryptor() {
//this.configManager = configManager;
this.rootKey = this.decode(ROOT_SECRET);
SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode(ROOT_SECRET), "AES");
System.out.println(secretKeySpec.toString());
byte[] aesKey = rootKey.getEncoded();
System.out.println("aesKey length: " + aesKey.length);
System.out.println("aesKey:" + aesKey);
String aesKeyBase64 = Base64.getEncoder().encodeToString(aesKey);
System.out.println("aesKeyBase64: " + aesKeyBase64);
String dynamicSecret = null;//configManager.getConfigByGroup(GROUP_PLATFORM).get(PLATFORM_DYNAMIC_SECRET);
if (dynamicSecret == null) {
this.resetDynamicSecret();
} else {
this.dynamicKey = this.decode(dynamicSecret);
}
}
//@Override
public String getDynamicSecret() {
return encode(dynamicKey);
}
//@Override
public void resetDynamicSecret() {
KeyGenerator generator = null;
try {
generator = KeyGenerator.getInstance(AES_KEY_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
//log.error("cannot get the key generator of {}", AES_KEY_ALGORITHM);
//throw new SystemException();
}
generator.init(128);
SecretKey key = generator.generateKey();
//configManager.setConfigByGroup(GROUP_PLATFORM, Map.of(PLATFORM_DYNAMIC_SECRET, encode(key)));
this.dynamicKey = key;
byte[] aesKey = key.getEncoded();
System.out.println("aesKey length: " + aesKey.length);
System.out.println("aesKey:" + aesKey);
String aesKeyBase64 = Base64.getEncoder().encodeToString(aesKey);
System.out.println("aesKeyBase64: " + aesKeyBase64);
}
/**
* Base64编码
*/
private String encode(SecretKey key) {
byte[] bytes = Base64.getEncoder().encode(key.getEncoded());
return new String(bytes);
}
/**
* Base64解码
*/
private SecretKey decode(String secret) {
byte[] bytes = Base64.getDecoder().decode(secret.getBytes());
return new SecretKeySpec(bytes, AES_KEY_ALGORITHM);
}
public byte[] encrypt(byte[] plainBytes, boolean dynamic)
throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
SecretKey key = dynamic ? dynamicKey : rootKey;
Cipher cipher;
try {
cipher = Cipher.getInstance(AES_ALGORITHM);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherBytes = cipher.doFinal(plainBytes);
return Base64.getEncoder().encode(cipherBytes);
}
public byte[] decrypt(byte[] encodedBytes, boolean dynamic)
throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher;
try {
cipher = Cipher.getInstance(AES_ALGORITHM);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
SecretKey key = dynamic ? dynamicKey : rootKey;
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] cipherBytes = Base64.getDecoder().decode(encodedBytes);
return cipher.doFinal(cipherBytes);
}
public static void main(String[] args) {
AesMqttEncryptor ttt=new AesMqttEncryptor();
//ttt.resetDynamicSecret();
String strFrist="{\"requestId";
try {
//System.out.printf("strFrist:%s%n", strFrist);
byte[] bytes=ttt.encrypt(strFrist.getBytes(),false);
String strValue=new String(bytes);
System.out.printf("加密后 strValue:%s%n", strValue);
bytes=ttt.decrypt(strValue.getBytes(),false);
strValue=new String(bytes);
System.out.printf("解密后 strValue:%s%n", strValue);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}