Correct client challenge encoding

First, encode the individual parameters, then add them to the command,
do the encryption and then encode the binary string again for sending.
This commit is contained in:
Tobias Eidelpes 2020-12-29 10:33:26 +01:00
parent 6666494009
commit d1bf65698d

View File

@ -174,26 +174,28 @@ public class MessageClient implements IMessageClient, Runnable {
IvParameterSpec iv = generateIv();
// Save AES cipher for subsequent communication
setAesCipher(secretKeySpec, iv);
// Concatenate challenge, secretKey and IV
byte[] concatenated = new byte[80];
System.arraycopy(clearTextChallenge, 0, concatenated, 0, 32);
System.arraycopy(secretKeySpec.getEncoded(), 0, concatenated, 32, 32);
System.arraycopy(iv.getIV(), 0, concatenated, 64, 16);
String base64Encoded = Base64.getEncoder().encodeToString(concatenated);
// Encrypt "ok <base64Encoded>"
Cipher cipher = null;
// Encode parameters to base64
String clearTextChallengeEncoded = Base64.getEncoder().encodeToString(clearTextChallenge);
String secretKeyEncoded = Base64.getEncoder().encodeToString(secretKeySpec.getEncoded());
String ivEncoded = Base64.getEncoder().encodeToString(iv.getIV());
// Concatenate command and parameters (challenge, secretKey and IV)
String concatenated = "ok" + clearTextChallengeEncoded + secretKeyEncoded + ivEncoded;
// Encrypt "<base64Encoded>"
Cipher cipher;
byte[] cipherTextChallenge;
try {
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, serverPublicKey);
cipher.update(("ok " + base64Encoded).getBytes(StandardCharsets.UTF_8));
byte[] cipherTextChallenge = cipher.doFinal();
return (new String(cipherTextChallenge, StandardCharsets.UTF_8));
cipher.update((concatenated).getBytes(StandardCharsets.UTF_8));
cipherTextChallenge = cipher.doFinal();
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
logger.severe("This should not be happening!");
e.printStackTrace();
shutdown();
return null;
}
return null;
// Return base64 encoded cipherMessage
return (Base64.getEncoder().encodeToString(cipherTextChallenge));
}
@Override