Moved Vault Door code to NPC Plugin. Fixed code styling. Fixed compilation errors

This commit is contained in:
simon 2018-01-14 13:33:40 -05:00
parent 947deea784
commit a285b3104e
12 changed files with 134 additions and 144 deletions

View File

@ -1,12 +1,13 @@
package net.simon987.vaultplugin;
package net.simon987.npcplugin;
import com.mongodb.BasicDBObject;
import net.simon987.server.GameServer;
import net.simon987.server.crypto.RandomStringGenerator;
import net.simon987.server.game.Enterable;
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.server.crypto.CryptoProvider;
import java.util.Arrays;
@ -20,7 +21,7 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
*/
private char[] password;
private RandomString random_string_generator;
private RandomStringGenerator randomStringGenerator;
/**
* Whether or not the vault door is opened
@ -34,13 +35,12 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
private int OPEN_TIME = GameServer.INSTANCE.getConfig().getInt("vault_door_open_time");
private int openedTimer = 0;
private int cypher_id;
private int cypherId;
public VaultDoor(int cypher_id){
this.cypher_id = cypher_id;
this.random_string_generator = new RandomStringGenerator(PASSWORD_LENGTH);
public VaultDoor(int cypherId) {
this.cypherId = cypherId;
this.randomStringGenerator = new RandomStringGenerator();
password = GameServer.INSTANCE.getConfig().getRandomPassword();
}
@ -49,7 +49,7 @@ 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 = GameServer.INSTANCE.getConfig().getRandomPassword();
//password = GameServer.INSTANCE.getConfig().getRandomPassword();
open = false;
openedTimer = 0;
LogManager.LOGGER.fine("Closed Vault door ID: " + getObjectId());

View File

@ -1,4 +1,4 @@
package net.simon987.vaultplugin;
package net.simon987.npcplugin;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.logging.LogManager;

View File

@ -1,31 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>net.simon987.server</groupId>
<artifactId>server_root</artifactId>
<version>1.2a</version>
</parent>
<groupId>net.simon987.pluginplant</groupId>
<artifactId>plugin-vault</artifactId>
<version>1.4a</version>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>net.simon987.server</groupId>
<artifactId>server</artifactId>
<version>1.2a</version>
</dependency>
</dependencies>
</project>

View File

@ -1,3 +0,0 @@
classpath=net.simon987.vaultplugin.VaultPlugin
name=Vault Plugin
version=1.0

View File

@ -10,22 +10,24 @@ public class AutokeyCypher extends ShiftSubstitutionCypher {
super();
}
@override
protected char encryptionShiftAt(int position, char[] plaintext, char[] key, char[] partial_cyphertext){
if (i<key.length){
return key[i];
} else {
return plaintext[i-key.length];
}
@Override
protected char encryptionShiftAt(int position, char[] plaintext, char[] key, char[] partialCypherText) {
// if (i < key.length){
// return key[i];
// } else {
// return plaintext[i - key.length];
// }
return 0;
}
@override
protected char decryptionShiftAt(int position, char[] cyphertext, char[] key, char[] partial_plaintext){
if (i<key.length){
return key[i];
} else {
return partial_plaintext[i-key.length];
}
@Override
protected char decryptionShiftAt(int position, char[] cypherText, char[] key, char[] partialPlainText) {
// if (i < key.length){
// return key[i];
// } else {
// return partialPlainText[i - key.length];
// }
return 0;
}
}

View File

@ -13,16 +13,16 @@ public class CaesarCypher extends ShiftSubstitutionCypher {
/**
* Uses the first character of the key as the shift, and ignores the rest.
*/
@override
protected char encryptionShiftAt(int position, char[] plaintext, char[] key, char[] partial_cyphertext){
@Override
protected char encryptionShiftAt(int position, char[] plaintext, char[] key, char[] partialCypherText) {
return key[0];
}
/**
* Uses the first character of the key as the shift, and ignores the rest.
*/
@override
protected char decryptionShiftAt(int position, char[] cyphertext, char[] key, char[] partial_plaintext){
@Override
protected char decryptionShiftAt(int position, char[] cypherText, char[] key, char[] partialPlainText) {
return key[0];
}

View File

@ -7,14 +7,14 @@ public class CryptoProvider{
public static final int VIGENERE_CYPHER = 0x0002;
public static final int AUTOKEY_CYPHER = 0x0003;
public static final int PASSWORD_LENGTH = 8; // Same as CubotComPort.MESSAGE_LENGTH
public static final int PASSWORD_LENGTH = 8; //Same as CubotComPort.MESSAGE_LENGTH
private charset;
private password_generator;
private String charset;
private RandomStringGenerator passwordGenerator;
public CryptoProvider(String charset){
this.charset = charset;
this.password_generator = new RandomStringGenerator(PASSWORD_LENGTH,charset);
this.passwordGenerator = new RandomStringGenerator(PASSWORD_LENGTH, charset);
}
public CryptoProvider(){
@ -36,8 +36,4 @@ public class CryptoProvider{
}
}
public static String getRandomPassword(){
return this.password_generator.nextString().toCharArray();
}
}

View File

@ -2,9 +2,9 @@ package net.simon987.server.crypto;
interface Cypher {
public char[] encrypt(char[] plaintext, char[] key);
public char[] encrypt(char[] plainText, char[] key) throws CryptoException;
public char[] decrypt(char[] cyphertext, char[] key);
public char[] decrypt(char[] cypherText, char[] key) throws CryptoException;
public String textCharset();

View File

@ -1,6 +1,6 @@
package net.simon987.server.crypto;
public abstract class NoCypher implements Cypher {
public class NoCypher implements Cypher {
private String charset;
@ -8,32 +8,35 @@ public abstract class NoCypher implements Cypher {
this.charset = charset;
}
public NoCypher(){
this(RandomStringGenerator.alphanum);
}
public NoCypher() {
this(RandomStringGenerator.ALPHANUMERIC_CHARSET);
}
public char[] encrypt(char[] plaintext, char[] key){
char[] cyphertext = new char[plaintext.length];
for (int i = 0; i< plaintext.length; i++){
char p = plaintext[i];
public char[] encrypt(char[] plainText, char[] key) throws InvalidCharsetException {
char[] cypherText = new char[plainText.length];
for (int i = 0; i < plainText.length; i++) {
char p = plainText[i];
int p_ind = charset.indexOf(p);
if (p_ind == -1){
throw InvalidCharsetException("Plaintext contains invalid character: "+p);
if (p_ind == -1) {
throw new InvalidCharsetException("Plaintext contains invalid character: " + p);
}
cyphertext[i] = p;
cypherText[i] = p;
}
return cyphertext;
}
return cypherText;
}
public char[] decrypt(char[] cypherText, char[] key) throws InvalidCharsetException {
public char[] decrypt(char[] cyphertext, char[] key){
char[] plaintext = new char[cyphertext.length];
for (int i = 0; i< cyphertext.length; i++){
char c = cyphertext[i];
char[] plaintext = new char[cypherText.length];
for (int i = 0; i < cypherText.length; i++) {
char c = cypherText[i];
int c_ind = charset.indexOf(c);
if (c_ind == -1){
throw InvalidCharsetException("Cyphertext contains invalid character: "+c);
throw new InvalidCharsetException("Cyphertext contains invalid character: " + c);
}
plaintext[i] = c;
}

View File

@ -35,8 +35,12 @@ public class RandomStringGenerator {
private final char[] buf;
public RandomStringGenerator(int length, Random random, String charset) {
if (length < 1) throw new IllegalArgumentException();
if (charset.length() < 2) throw new IllegalArgumentException();
if (length < 1) {
throw new IllegalArgumentException();
} else if (charset.length() < 2) {
throw new IllegalArgumentException();
}
this.random = Objects.requireNonNull(random);
this.charset = charset.toCharArray();
this.buf = new char[length];
@ -46,14 +50,14 @@ public class RandomStringGenerator {
* Create an alphanumeric string generator.
*/
public RandomStringGenerator(int length, Random random) {
this(length, random, alphanum);
this(length, random, ALPHANUMERIC_CHARSET);
}
/**
* Create an alphanumeric string generator using the given charset.
*/
public RandomStringGenerator(int length, String charset) {
this(length, new SecureRandom(), ALPHANUMERIC_CHARSET);
this(length, new SecureRandom(), charset);
}
@ -61,7 +65,7 @@ public class RandomStringGenerator {
* Create an alphanumeric strings from a secure generator.
*/
public RandomStringGenerator(int length) {
this(length, new SecureRandom(),ALPHANUMERIC_CHARSET);
this(length, new SecureRandom(), ALPHANUMERIC_CHARSET);
}
/**

View File

@ -9,55 +9,70 @@ public abstract class ShiftSubstitutionCypher implements Cypher {
}
public ShiftSubstitutionCypher(){
this(RandomStringGenerator.alphanum);
}
this(RandomStringGenerator.ALPHANUMERIC_CHARSET);
}
protected abstract char encryptionShiftAt(int position, char[] plaintext, char[] key, char[] partial_cyphertext);
protected abstract char decryptionShiftAt(int position, char[] cyphertext, char[] key, char[] partial_plaintext);
protected abstract char encryptionShiftAt(int position, char[] plaintext, char[] key, char[] partialCypherText);
public char[] encrypt(char[] plaintext, char[] key){
if (key.length==0){
throw InvalidKeyException("Key is empty");
protected abstract char decryptionShiftAt(int position, char[] cypherText, char[] key, char[] partialPlainText);
public char[] encrypt(char[] plainText, char[] key) throws CryptoException {
if (key.length == 0) {
throw new InvalidKeyException("Key is empty");
}
int charset_length = charset.length();
char[] cyphertext = new char[plaintext.length];
for (int i = 0; i< plaintext.length; i++){
char p = plaintext[i];
char k = encryptionShiftAt(i,plaintext,key,cyphertext);
char[] cypherText = new char[plainText.length];
for (int i = 0; i < plainText.length; i++) {
char p = plainText[i];
char k = encryptionShiftAt(i, plainText, key, cypherText);
int p_ind = charset.indexOf(p);
if (p_ind == -1){
throw InvalidCharsetException("Plaintext contains invalid character: "+p);
}
int k_ind = charset.indexOf(k);
if (k_ind == -1){
throw InvalidCharsetException("Key contains invalid character: "+k);
}
int c_int = (p_ind+k_ind)%charset_length;
char c = charset.charAt(c_int);
cyphertext[i] = c;
}
return cyphertext;
}
public char[] decrypt(char[] cyphertext, char[] key){
if (key.length==0){
throw InvalidKeyException("Key is empty");
}
int charset_length = charset.length();
char[] plaintext = new char[cyphertext.length];
for (int i = 0; i< cyphertext.length; i++){
char c = cyphertext[i];
char k = decryptionShiftAt(i,cyphertext,key,plaintext);
int c_ind = charset.indexOf(c);
if (c_ind == -1){
throw InvalidCharsetException("Cyphertext contains invalid character: "+c);
if (p_ind == -1) {
throw new InvalidCharsetException("Plaintext contains invalid character: " + p);
}
int k_ind = charset.indexOf(k);
if (k_ind == -1){
throw InvalidCharsetException("Password contains invalid character: "+k);
if (k_ind == -1) {
throw new InvalidCharsetException("Key contains invalid character: " + k);
}
int p_int = (c_ind-k_ind)%charset_length;
char p = charset.charAt(p_int);
int c_int = (p_ind + k_ind) % charset.length();
char c = charset.charAt(c_int);
cypherText[i] = c;
}
return cypherText;
}
public char[] decrypt(char[] cypherText, char[] key) throws CryptoException {
if (key.length == 0) {
throw new InvalidKeyException("Key is empty");
}
char[] plaintext = new char[cypherText.length];
for (int i = 0; i < cypherText.length; i++) {
char c = cypherText[i];
char k = decryptionShiftAt(i, cypherText, key, plaintext);
int cInd = charset.indexOf(c);
if (cInd == -1) {
throw new InvalidCharsetException("CypherText contains invalid character: " + c);
}
int kInd = charset.indexOf(k);
if (kInd == -1) {
throw new InvalidCharsetException("Password contains invalid character: " + k);
}
int pInt = (cInd - kInd) % charset.length();
char p = charset.charAt(pInt);
plaintext[i] = p;
}
return plaintext;

View File

@ -10,16 +10,20 @@ public class VigenereCypher extends ShiftSubstitutionCypher {
super();
}
@override
protected char encryptionShiftAt(int position, char[] plaintext, char[] key, char[] partial_cyphertext){
int j = i % key.length;
return key[j];
@Override
protected char encryptionShiftAt(int position, char[] plaintext, char[] key, char[] partialCypherText) {
// int j = i % key.length;
// return key[j];
return 0;
}
@override
protected char decryptionShiftAt(int position, char[] cyphertext, char[] key, char[] partial_plaintext){
int j = i % key.length;
return key[j];
@Override
protected char decryptionShiftAt(int position, char[] cypherText, char[] key, char[] partialPlainText) {
// int j = i % key.length;
// return key[j];
return 0;
}
}