1
0
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:
2012-05-05 17:10:20 -03:00
parent 5ba7f75cf0
commit b9c07d5c27
4 changed files with 17 additions and 8 deletions

View File

@@ -83,7 +83,7 @@ public class CM_PROTOCOL_VERSION extends AbstractClientPacket {
public void process(final Lineage2Client conn) { public void process(final Lineage2Client conn) {
// generate a new key // generate a new key
final Lineage2CryptographyKey inKey = new Lineage2CryptographyKey( final Lineage2CryptographyKey inKey = new Lineage2CryptographyKey(
keygen.generate()); keygen.generate(128));
conn.enableDecrypter(inKey); conn.enableDecrypter(inKey);
final Lineage2CryptographyKey outKey = inKey.copy(); final Lineage2CryptographyKey outKey = inKey.copy();

View File

@@ -31,7 +31,10 @@ public interface BlowfishKeygenService extends Service {
* Creates a new 128 bits key. Note that the key is not necessarily random * Creates a new 128 bits key. Note that the key is not necessarily random
* and can be a fixed key. * and can be a fixed key.
* *
* @param strength
* the key strength. Must be a multiple of 8.
*
* @return the 128 bits key * @return the 128 bits key
*/ */
byte[] generate(); byte[] generate(int strength);
} }

View File

@@ -21,6 +21,7 @@ import java.util.Random;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.google.common.base.Preconditions;
import com.l2jserver.service.AbstractService; import com.l2jserver.service.AbstractService;
import com.l2jserver.service.ServiceStartException; import com.l2jserver.service.ServiceStartException;
import com.l2jserver.service.ServiceStopException; import com.l2jserver.service.ServiceStopException;
@@ -49,10 +50,12 @@ public class PseudoRandomBlowfishKeygenService extends AbstractService
} }
@Override @Override
public byte[] generate() { public byte[] generate(int strength) {
log.debug("Generating a new key"); 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 // randomize the 8 first bytes
for (int i = 0; i < key.length; i++) { for (int i = 0; i < key.length; i++) {
key[i] = (byte) random.nextInt(255); key[i] = (byte) random.nextInt(255);

View File

@@ -21,6 +21,7 @@ import org.apache.commons.math.random.RandomDataImpl;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.google.common.base.Preconditions;
import com.l2jserver.service.AbstractService; import com.l2jserver.service.AbstractService;
import com.l2jserver.service.ServiceStartException; import com.l2jserver.service.ServiceStartException;
import com.l2jserver.service.ServiceStopException; import com.l2jserver.service.ServiceStopException;
@@ -50,10 +51,12 @@ public class SecureBlowfishKeygenService extends AbstractService implements
} }
@Override @Override
public byte[] generate() { public byte[] generate(int strength) {
log.debug("Generating a new key"); 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 // randomize the 8 first bytes
for (int i = 0; i < key.length; i++) { for (int i = 0; i < key.length; i++) {
key[i] = (byte) random.nextSecureInt(0, 255); key[i] = (byte) random.nextSecureInt(0, 255);