48 lines
2.1 KiB
Java
48 lines
2.1 KiB
Java
import java.security.MessageDigest;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.util.HashMap;
|
|
import java.util.logging.Logger;
|
|
|
|
public class StreamAesUtils {
|
|
|
|
public static Aes generateFromExchangeKeyAndSuperSecretKey(String keyExchange, String superSecretKey) throws NoSuchAlgorithmException {
|
|
HashMap<String, String> 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;
|
|
}
|
|
System.err.println("cipher=" + (hashMap.get("cipher")));
|
|
System.err.println("username=" + (hashMap.get("username")));
|
|
System.err.println("padding=" + (hashMap.get("padding")));
|
|
System.err.println("algorithm=" + (hashMap.get("algorithm")));
|
|
System.err.println("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)) {
|
|
System.err.println("AES use User Password");
|
|
} else if ("none".equals(username)) {
|
|
superSecretKey = GenKey.generateDefaultPsw();
|
|
} else {
|
|
System.err.println("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();
|
|
}
|
|
} |