* added desktop client
Signed-off-by: CubeBit <denis-seredenko@ukr.net>
This commit is contained in:
@@ -63,18 +63,15 @@ public class ClientHandler implements Runnable {
|
||||
out.flush();
|
||||
|
||||
aesSent = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (prefix.equals("WLC")) {
|
||||
this.username = restMessage;
|
||||
//TODO: add better logic
|
||||
// if (Server.clientKeys.get(username) != null) {
|
||||
// in.close();
|
||||
// out.close();
|
||||
// clientSocket.close();
|
||||
// }
|
||||
continue;
|
||||
if (Server.clientKeys.get(username) != null) {
|
||||
in.close();
|
||||
out.close();
|
||||
clientSocket.close();
|
||||
}
|
||||
}
|
||||
|
||||
if (prefix.equals("TXT") && rsaReceived && aesSent) {
|
||||
|
||||
@@ -31,21 +31,22 @@ public class Server {
|
||||
|
||||
static byte[] ivKey;
|
||||
|
||||
public static void main(String[] args) throws IOException, ArgumentsException, NoSuchAlgorithmException {
|
||||
public static void main(String[] args) throws ArgumentsException, NoSuchAlgorithmException, IOException {
|
||||
Server.serverKeys = EncryptionUtil.generateRSAKeys();
|
||||
Server.ivKey = EncryptionUtil.generateIV();
|
||||
Server.aesKey = EncryptionUtil.generateAESKey();
|
||||
|
||||
int portNumber = getPortNumber(args);
|
||||
|
||||
ServerSocket server = new ServerSocket(portNumber);
|
||||
while (!server.isClosed()) {
|
||||
Socket clientSocket = server.accept();
|
||||
try (ServerSocket server = new ServerSocket(portNumber)) {
|
||||
while (!server.isClosed()) {
|
||||
Socket clientSocket = server.accept();
|
||||
|
||||
ClientHandler clientHandler = new ClientHandler(clientSocket);
|
||||
clients.add(clientHandler);
|
||||
ClientHandler clientHandler = new ClientHandler(clientSocket);
|
||||
clients.add(clientHandler);
|
||||
|
||||
executor.execute(clientHandler);
|
||||
executor.execute(clientHandler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -87,28 +87,24 @@ public class EncryptionUtil {
|
||||
}
|
||||
|
||||
public static String encryptWithAES(String plainText, SecretKey aesKey, byte[] ivKey) throws IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException {
|
||||
Cipher aesChiper = Cipher.getInstance("AES/GCM/NoPadding");
|
||||
|
||||
SecretKeySpec keySpec = new SecretKeySpec(aesKey.getEncoded(), "AES");
|
||||
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, ivKey);
|
||||
|
||||
Cipher aesChiper = Cipher.getInstance("AES/GCM/NoPadding");
|
||||
aesChiper.init(Cipher.ENCRYPT_MODE, keySpec, gcmParameterSpec);
|
||||
|
||||
byte[] byteCipherText = aesChiper.doFinal(plainText.getBytes());
|
||||
|
||||
return Base64.getEncoder().encodeToString(byteCipherText);
|
||||
}
|
||||
|
||||
public static String decryptWithAES(String encryptedMessage, SecretKey aesKey, byte[] ivKey) throws IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException {
|
||||
Cipher aesChiper = Cipher.getInstance("AES/GCM/NoPadding");
|
||||
|
||||
SecretKeySpec keySpec = new SecretKeySpec(aesKey.getEncoded(), "AES");
|
||||
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, ivKey);
|
||||
|
||||
Cipher aesChiper = Cipher.getInstance("AES/GCM/NoPadding");
|
||||
aesChiper.init(Cipher.DECRYPT_MODE, keySpec, gcmParameterSpec);
|
||||
|
||||
byte[] byteCipherText = aesChiper.doFinal(Base64.getDecoder().decode(encryptedMessage));
|
||||
|
||||
return new String(byteCipherText);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user