Added some cyphers for use in vault doors.

1. Created net.simon987.server.crypto package and moved RandomStringGenerator there.
2. Created CryptoProvider class, added a global instance to GameServer for use by game entities.
3. Created interface for cyphers, abstract class for shift substitution cyphers, classes for no cypher, Caesar cypher, Vigenere cypher and autokey cypher.
4. Created some Crypto exceptions.
5. Removed static encryption/decryption methods from VaultDoor, and moved getRandomPassword() to the global CryptoProvider instance.
This commit is contained in:
sg495
2018-01-09 17:14:31 +01:00
parent 46483b2bf8
commit a04207b5e0
14 changed files with 407 additions and 141 deletions

View File

@@ -1,16 +0,0 @@
public class InvalidCharsetException extends Exception {
public InvalidCharsetException () {
}
public InvalidCharsetException (String message) {
super (message);
}
public InvalidCharsetException (Throwable cause) {
super (cause);
}
public InvalidCharsetException (String message, Throwable cause) {
super (message, cause);
}

View File

@@ -1,68 +0,0 @@
/**
*
* RandomString class by erickson
* https://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string
*
*/
import java.security.SecureRandom;
import java.util.Locale;
import java.util.Objects;
import java.util.Random;
public class RandomString {
/**
* Generate a random string.
*/
public String nextString() {
for (int idx = 0; idx < buf.length; ++idx)
buf[idx] = symbols[random.nextInt(symbols.length)];
return new String(buf);
}
public static final String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String lower = upper.toLowerCase(Locale.ROOT);
public static final String digits = "0123456789";
public static final String alphanum = upper + lower + digits;
private final Random random;
private final char[] symbols;
private final char[] buf;
public RandomString(int length, Random random, String symbols) {
if (length < 1) throw new IllegalArgumentException();
if (symbols.length() < 2) throw new IllegalArgumentException();
this.random = Objects.requireNonNull(random);
this.symbols = symbols.toCharArray();
this.buf = new char[length];
}
/**
* Create an alphanumeric string generator.
*/
public RandomString(int length, Random random) {
this(length, random, alphanum);
}
/**
* Create an alphanumeric strings from a secure generator.
*/
public RandomString(int length) {
this(length, new SecureRandom());
}
/**
* Create session identifiers.
*/
public RandomString() {
this(21);
}
}

View File

@@ -6,7 +6,7 @@ import net.simon987.server.game.GameObject;
import net.simon987.server.game.Programmable;
import net.simon987.server.game.Updatable;
import net.simon987.server.logging.LogManager;
import net.simon987.vaultplugin.RandomString;
import net.simon987.server.crypto.CryptoProvider;
import java.util.Arrays;
@@ -32,17 +32,15 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
* Number of ticks to remain the door open
*/
private int OPEN_TIME = GameServer.INSTANCE.getConfig().getInt("vault_door_open_time");
private int openedTimer = 0;
private int password_length;
private int openedTimer = 0;
private int cypher_id;
public VaultDoor(int password_length, int cypher_id){
this.password_length = password_length;
public VaultDoor(int cypher_id){
this.cypher_id = cypher_id;
this.random_string_generator = new RandomString(password_length);
this.random_string_generator = new RandomStringGenerator(PASSWORD_LENGTH);
password = getRandomPassword();
password = GameServer.INSTANCE.getConfig().getRandomPassword();
}
@@ -51,12 +49,11 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
if (open){
if (openedTimer <= 0) {
//Door was open for OPEN_TIME, close it and regen password
password = getRandomPassword();
password = GameServer.INSTANCE.getConfig().getRandomPassword();
open = false;
openedTimer = 0;
LogManager.LOGGER.fine("Closed Vault door ID: " + getObjectId());
}
else{
} else {
openedTimer--;
}
}
@@ -69,13 +66,11 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
if (Arrays.equals(message, password)) {
if (!open) {
openVault();
}
else{
} else {
keepVaultOpen();
}
return true;
}
else {
} else {
return false;
}
}
@@ -104,63 +99,6 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
}
}
/**
* Generates a random alphanumeric string using the RandomString class
*/
private static char[] getRandomPassword() {
return random_string_generator.nextString().toCharArray();
}
private char[] encryptVernam(char[] plaintext){
String charset = random_string_generator.alphanum;
int charset_length = charset.length();
int plaintext_length = plaintext.length;
char[] cyphertext = new char[plaintext_length];
for (int i = 0; i< plaintext_length; i++){
int j = i % password_length;
char p = plaintext[i];
char q = password[j];
int p_ind = charset.indexOf(p);
if (p_ind == -1){
throw InvalidCharsetException("Plaintext contains non-alphanumeric character: "+p);
}
int q_ind = charset.indexOf(q);
if (q_ind == -1){
throw InvalidCharsetException("Password contains non-alphanumeric character: "+q); // this should NEVER happen
}
int c_int = (p_ind+q_ind)%charset_length;
char c = charset.charAt(c_int);
cyphertext[i] = c;
}
return cyphertext;
}
private char[] decryptVernam(char[] cyphertext){
String charset = random_string_generator.alphanum;
int charset_length = charset.length();
int cyphertext_length = cyphertext.length;
char[] plaintext = new char[cyphertext_length];
for (int i = 0; i< cyphertext_length; i++){
int j = i % password_length;
char c = cyphertext[i];
char q = password[j];
int c_ind = charset.indexOf(c);
if (c_ind == -1){
throw InvalidCharsetException("Cyphertext contains non-alphanumeric character: "+c);
}
int q_ind = charset.indexOf(q);
if (q_ind == -1){
throw InvalidCharsetException("Password contains non-alphanumeric character: "+q); // this should NEVER happen
}
int p_int = (c_ind-q_ind)%charset_length;
char p = charset.charAt(p_int);
plaintext[i] = p;
}
return plaintext;
}
@Override
public char getMapInfo() {