Makes one-time-pad and substitution cipher real encryption compatible
All checks were successful
EpicKnarvik97/Books-Without-Borders/pipeline/head This commit looks good

This commit is contained in:
2025-08-18 18:02:57 +02:00
parent 6adec89ae1
commit cb70a8298d
10 changed files with 69 additions and 53 deletions

View File

@@ -34,10 +34,10 @@ public class CommandEncrypt implements TabExecutor {
return false;
}
EncryptionStyle encryptionStyle = arguments.length == 2 ? EncryptionStyle.getFromString(arguments[1]) : EncryptionStyle.SUBSTITUTION;
EncryptionStyle encryptionStyle = arguments.length == 2 ? EncryptionStyle.getFromString(arguments[1]) : EncryptionStyle.AES;
// AES is the only reliable method for retaining the plaintext
if (BooksWithoutBorders.getConfiguration().useRealEncryption()) {
if (BooksWithoutBorders.getConfiguration().useRealEncryption() && !encryptionStyle.isRealEncryptionSupported()) {
encryptionStyle = EncryptionStyle.AES;
}
@@ -132,10 +132,13 @@ public class CommandEncrypt implements TabExecutor {
@NotNull
protected List<String> doTabCompletion(@NotNull String[] args, boolean groupEncrypt) {
int argumentsCount = args.length;
boolean useRealEncryption = BooksWithoutBorders.getConfiguration().useRealEncryption();
List<String> encryptionStyles = new ArrayList<>();
for (EncryptionStyle encryptionStyle : EncryptionStyle.values()) {
encryptionStyles.add(encryptionStyle.toString());
if (!useRealEncryption || encryptionStyle.isRealEncryptionSupported()) {
encryptionStyles.add(encryptionStyle.toString());
}
}
if (groupEncrypt) {
@@ -150,9 +153,6 @@ public class CommandEncrypt implements TabExecutor {
if (argumentsCount == 1) {
return List.of("<password>");
} else if (argumentsCount == 2) {
if (BooksWithoutBorders.getConfiguration().useRealEncryption()) {
return List.of();
}
return TabCompletionHelper.filterMatchingStartsWith(encryptionStyles, args[1]);
}
}

View File

@@ -10,38 +10,53 @@ public enum EncryptionStyle {
/**
* Possibly lossy encryption using DNA codons
*/
DNA("dna"),
DNA("dna", false),
/**
* A simple cipher using the key to substitute one character for another
*/
SUBSTITUTION("substitution"),
SUBSTITUTION("substitution", true),
/**
* A military-grade encryption cypher
*/
AES("aes"),
AES("aes", true),
/**
* An unbreakable encryption method assuming the key is completely random and never used more than once, ever
*/
ONE_TIME_PAD("onetimepad"),
ONE_TIME_PAD("onetimepad", true),
/**
* Just a way of using magic text to make text illegible
*/
MAGIC("magic"),
MAGIC("magic", false),
;
private final String name;
private final boolean realEncryptionSupported;
/**
* Instantiates a new encryption style
*
* @param name <p>The human-readable encryption style name</p>
* @param name <p>The human-readable encryption style name</p>
* @param realEncryptionSupported <p>Whether the encryption style can be used for real encryption</p>
*/
EncryptionStyle(@NotNull String name) {
EncryptionStyle(@NotNull String name, boolean realEncryptionSupported) {
this.name = name;
this.realEncryptionSupported = realEncryptionSupported;
}
/**
* Gets whether this encryption style supports real encryption
*
* <p>Real encryption means that only the cypher text is stored on disk, so the server owner cannot simply check the
* contents of encrypted books.</p>
*
* @return <p>True if this encryption style supports real encryption</p>
*/
public boolean isRealEncryptionSupported() {
return this.realEncryptionSupported;
}
/**
@@ -57,7 +72,7 @@ public enum EncryptionStyle {
return style;
}
}
return SUBSTITUTION;
return AES;
}
@Override

View File

@@ -1,5 +1,6 @@
package net.knarcraft.bookswithoutborders.encryption;
import net.knarcraft.bookswithoutborders.utility.EncryptionHelper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -26,12 +27,12 @@ public class OneTimePad implements Encryptor {
@Override
public @Nullable String encryptText(@NotNull String input) {
return oneTimePad(input);
return oneTimePad(input, true);
}
@Override
public @Nullable String decryptText(@NotNull String input) {
return oneTimePad(input);
return oneTimePad(input, false);
}
/**
@@ -40,11 +41,16 @@ public class OneTimePad implements Encryptor {
* <p>The one time pad encryption is very secure, and encryption works just like decryption, but is vulnerable if
* the same key is used more than once.</p>
*
* @param input <p>The input to encrypt/decrypt</p>
* @param input <p>The input to encrypt/decrypt</p>
* @param encrypt <p>Whether to encrypt or decrypt the input</p>
* @return <p>The encrypted/decrypted output</p>
*/
@NotNull
public String oneTimePad(@NotNull String input) {
public String oneTimePad(@NotNull String input, boolean encrypt) {
if (!encrypt) {
input = new String(EncryptionHelper.hexStringToByteArray(input), StandardCharsets.UTF_8);
}
String longKey;
try {
final MessageDigest digest = MessageDigest.getInstance("SHA3-256");
@@ -58,7 +64,12 @@ public class OneTimePad implements Encryptor {
for (int i = 0; i < input.length(); i++) {
output.append((char) (input.charAt(i) ^ longKey.charAt(i % longKey.length())));
}
return output.toString();
if (encrypt) {
return EncryptionHelper.bytesToHex(output.toString().getBytes(StandardCharsets.UTF_8));
} else {
return output.toString();
}
}
}

View File

@@ -1,9 +1,11 @@
package net.knarcraft.bookswithoutborders.encryption;
import net.knarcraft.bookswithoutborders.utility.EncryptionHelper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.StringTokenizer;
/**
@@ -49,6 +51,10 @@ public class SubstitutionCipher implements Encryptor {
return output.toString();
}
if (!encrypt) {
input = new String(EncryptionHelper.hexStringToByteArray(input), StandardCharsets.UTF_8);
}
// converts each number in the key to an integer and adds to an array
int[] offsetArray = getOffsetArray(this.key);
@@ -68,7 +74,12 @@ public class SubstitutionCipher implements Encryptor {
offsetPosition = 0;
}
}
return output.toString();
if (encrypt) {
return EncryptionHelper.bytesToHex(output.toString().getBytes(StandardCharsets.UTF_8));
} else {
return output.toString();
}
}
/**

View File

@@ -12,7 +12,7 @@ en:
SUCCESS_PAGE_DELETED: "Page deleted!"
SUCCESS_BOOK_LOADED: "Book created!"
SUCCESS_MIGRATION_STARTED: "Starting book migration..."
SUCCESS_RELOADED: "BooksWithoutBorders configuration reloaded!"
SUCCESS_RELOADED: "Configuration, books and language strings reloaded!"
SUCCESS_SAVED: "Book Saved as &e\"{fileName}\"&r"
ACTION_COPY: "copy"
ACTION_CLEAR: "clear"