Initialize the decryption cipher only once

(Related #259)
This commit is contained in:
games647
2019-07-09 12:52:54 +02:00
parent 4110ce2fa2
commit fbbe7a735a
2 changed files with 10 additions and 14 deletions

View File

@@ -8,7 +8,6 @@ import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Random;
@@ -83,28 +82,24 @@ public class EncryptionUtil {
/**
* Decrypts the content and extracts the key spec.
*
* @param cipher decryption cipher
* @param privateKey private key of the server
* @param cipher decryption cipher initialized with the private key
* @param sharedKey the encrypted shared key
* @return shared secret key
* @throws GeneralSecurityException
* @throws GeneralSecurityException if it fails to decrypt the data
*/
public static SecretKey decryptSharedKey(Cipher cipher, PrivateKey privateKey, byte[] sharedKey)
throws GeneralSecurityException {
return new SecretKeySpec(decrypt(cipher, privateKey, sharedKey), "AES");
public static SecretKey decryptSharedKey(Cipher cipher, byte[] sharedKey) throws GeneralSecurityException {
return new SecretKeySpec(decrypt(cipher, sharedKey), "AES");
}
/**
* Decrypted the given data using the cipher.
*
* @param cipher decryption cypher
* @param key server private key
* @param cipher decryption cypher initialized with the private key
* @param data the encrypted data
* @return clear text data
* @throws GeneralSecurityException if it fails to initialize and decrypt the data
* @throws GeneralSecurityException if it fails to decrypt the data
*/
public static byte[] decrypt(Cipher cipher, PrivateKey key, byte[] data) throws GeneralSecurityException {
cipher.init(Cipher.DECRYPT_MODE, key);
public static byte[] decrypt(Cipher cipher, byte[] data) throws GeneralSecurityException {
return cipher.doFinal(data);
}

View File

@@ -81,8 +81,9 @@ public class VerifyResponseTask implements Runnable {
SecretKey loginKey;
try {
cipher = Cipher.getInstance(privateKey.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
loginKey = EncryptionUtil.decryptSharedKey(cipher, privateKey, sharedSecret);
loginKey = EncryptionUtil.decryptSharedKey(cipher, sharedSecret);
} catch (GeneralSecurityException securityEx) {
disconnect("error-kick", false, "Cannot decrypt received contents", securityEx);
return;
@@ -148,7 +149,7 @@ public class VerifyResponseTask implements Runnable {
byte[] responseVerify = packetEvent.getPacket().getByteArrays().read(1);
//https://github.com/bergerkiller/CraftSource/blob/master/net.minecraft.server/LoginListener.java#L182
if (!Arrays.equals(requestVerify, EncryptionUtil.decrypt(cipher, privateKey, responseVerify))) {
if (!Arrays.equals(requestVerify, EncryptionUtil.decrypt(cipher, responseVerify))) {
//check if the verify token are equal to the server sent one
disconnect("invalid-verify-token", true
, "GameProfile {0} ({1}) tried to login with an invalid verify token. Server: {2} Client: {3}"