mirror of
https://github.com/Rogiel/l2jserver2
synced 2025-12-06 07:32:46 +00:00
Implements dynamic strength key generation for BlowfishKeygenService
This commit is contained in:
@@ -83,7 +83,7 @@ public class CM_PROTOCOL_VERSION extends AbstractClientPacket {
|
||||
public void process(final Lineage2Client conn) {
|
||||
// generate a new key
|
||||
final Lineage2CryptographyKey inKey = new Lineage2CryptographyKey(
|
||||
keygen.generate());
|
||||
keygen.generate(128));
|
||||
|
||||
conn.enableDecrypter(inKey);
|
||||
final Lineage2CryptographyKey outKey = inKey.copy();
|
||||
|
||||
@@ -31,7 +31,10 @@ public interface BlowfishKeygenService extends Service {
|
||||
* Creates a new 128 bits key. Note that the key is not necessarily random
|
||||
* and can be a fixed key.
|
||||
*
|
||||
* @param strength
|
||||
* the key strength. Must be a multiple of 8.
|
||||
*
|
||||
* @return the 128 bits key
|
||||
*/
|
||||
byte[] generate();
|
||||
byte[] generate(int strength);
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.Random;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.l2jserver.service.AbstractService;
|
||||
import com.l2jserver.service.ServiceStartException;
|
||||
import com.l2jserver.service.ServiceStopException;
|
||||
@@ -49,10 +50,12 @@ public class PseudoRandomBlowfishKeygenService extends AbstractService
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] generate() {
|
||||
log.debug("Generating a new key");
|
||||
public byte[] generate(int strength) {
|
||||
Preconditions.checkArgument(strength % 8 == 0,
|
||||
"strength must be a multiple of 8");
|
||||
log.debug("Generating a new {}-bit key", strength);
|
||||
|
||||
final byte[] key = new byte[16];
|
||||
final byte[] key = new byte[strength / 8];
|
||||
// randomize the 8 first bytes
|
||||
for (int i = 0; i < key.length; i++) {
|
||||
key[i] = (byte) random.nextInt(255);
|
||||
|
||||
@@ -21,6 +21,7 @@ import org.apache.commons.math.random.RandomDataImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.l2jserver.service.AbstractService;
|
||||
import com.l2jserver.service.ServiceStartException;
|
||||
import com.l2jserver.service.ServiceStopException;
|
||||
@@ -50,10 +51,12 @@ public class SecureBlowfishKeygenService extends AbstractService implements
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] generate() {
|
||||
log.debug("Generating a new key");
|
||||
public byte[] generate(int strength) {
|
||||
Preconditions.checkArgument(strength % 8 == 0,
|
||||
"strength must be a multiple of 8");
|
||||
log.debug("Generating a new {}-bit key", strength);
|
||||
|
||||
final byte[] key = new byte[16];
|
||||
final byte[] key = new byte[strength / 8];
|
||||
// randomize the 8 first bytes
|
||||
for (int i = 0; i < key.length; i++) {
|
||||
key[i] = (byte) random.nextSecureInt(0, 255);
|
||||
|
||||
Reference in New Issue
Block a user