import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.logging.Logger; public class StreamAesUtils { private static final Logger logger = Logger.getLogger(StreamAesUtils.class.getName()); public static Aes generateFromExchangeKeyAndSuperSecretKey(String keyExchange, String superSecretKey) throws NoSuchAlgorithmException { HashMap hashMap = new HashMap<>(); String[] params = keyExchange.split(" "); for (String param : params) { String[] keyVal = param.trim().split("=", 2); if (!(keyVal.length != 2 || keyVal[0] == null || keyVal[1] == null)) { String trim = keyVal[0].trim(); String trim2 = keyVal[1].replace("\"", "").trim(); hashMap.put(trim, trim2); } } if (!hashMap.containsKey("nonce")) { return null; } logger.info("cipher=" + (hashMap.get("cipher"))); logger.info("username=" + (hashMap.get("username"))); logger.info("padding=" + (hashMap.get("padding"))); logger.info("algorithm=" + (hashMap.get("algorithm"))); logger.info("nonce=" + (hashMap.get("nonce"))); return fromUserNonceSuperSecretKey(hashMap.get("username"), hashMap.get("nonce"), superSecretKey); } public static Aes fromUserNonceSuperSecretKey(String username, String nonce, String superSecretKey) throws NoSuchAlgorithmException { if (GenKey.generateDefaultUsername().equals(username)) { logger.info("AES use User Password"); } else if ("none".equals(username)) { superSecretKey = GenKey.generateDefaultPsw(); } else { logger.info("AES key-exchange unknown username"); return null; } byte[] md5 = md5Digest(nonce + ":" + superSecretKey); return new Aes(md5, md5Digest(username + ":" + nonce)); } private static byte[] md5Digest(String str) throws NoSuchAlgorithmException { MessageDigest instance = MessageDigest.getInstance("MD5"); instance.update(str.getBytes()); return instance.digest(); } }