Rewrites encryption
All checks were successful
EpicKnarvik97/Books-Without-Borders/pipeline/head This commit looks good
All checks were successful
EpicKnarvik97/Books-Without-Borders/pipeline/head This commit looks good
Adds an optional real encryption mode, which encrypts pages using AES, without saving plaintext. Re-implements the old magic encryption in non-real encryption mode. Fixes incorrect key generation for use in the substitution cipher and the gene cipher. Removes the option for saving books as txt. Adds tests for all encryption methods. Saves all necessary decryption data when storing encrypted books. Removes the old book updating code.
This commit is contained in:
@@ -3,22 +3,31 @@ package net.knarcraft.bookswithoutborders.encryption;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
|
||||
public class AESTest {
|
||||
|
||||
@Test
|
||||
public void encryptDecryptTest() {
|
||||
String plainText = "A lot of text";
|
||||
String password = "abc123";
|
||||
String plainText = "Flåklypa";
|
||||
String password = "TqOZdpY9RjjjVE9JjCWVecUYObv5MYidByrpI3cxjoY=";
|
||||
|
||||
AES aes = new AES(AES.generateIV(), AES.generateIV());
|
||||
System.out.println("Plaintext: " + plainText);
|
||||
System.out.println("Encryption password: " + password);
|
||||
|
||||
String encrypted = aes.encryptDecryptText(plainText, password, true);
|
||||
assertNotSame(encrypted, plainText);
|
||||
assertNotNull(encrypted);
|
||||
String decrypted = aes.encryptDecryptText(encrypted, password, false);
|
||||
AESConfiguration configuration = new AESConfiguration(new byte[]{-85, 103, -82, 71, 119, 28, 73, -75, -81, 102, -127, -125, -8, -75, 81, -111},
|
||||
new byte[]{(byte) 104, -42, 63, 31, -120, -2, 14, -119, 35, 122, 109, -64, 122, 117, 33, -85}, password);
|
||||
AES aes = new AES(configuration);
|
||||
|
||||
String cypherText = aes.encryptText(plainText);
|
||||
System.out.println("Cypher text: " + cypherText);
|
||||
|
||||
assertNotNull(cypherText);
|
||||
assertNotEquals(plainText, cypherText);
|
||||
|
||||
String decrypted = aes.decryptText(cypherText);
|
||||
System.out.println("Decrypted: " + decrypted);
|
||||
assertEquals(plainText, decrypted);
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,28 @@
|
||||
package net.knarcraft.bookswithoutborders.encryption;
|
||||
|
||||
import net.knarcraft.bookswithoutborders.utility.EncryptionHelper;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
|
||||
public class GenenCryptTest {
|
||||
|
||||
@Test
|
||||
public void encryptDecryptTest() {
|
||||
String encryptionKey = EncryptionHelper.getNumberKeyFromStringKey("My secret password!");
|
||||
String plaintext = "Very secret &4colored&r message.";
|
||||
GenenCrypt genenCrypt = new GenenCrypt(encryptionKey);
|
||||
|
||||
String cypherText = genenCrypt.encryptText(plaintext);
|
||||
|
||||
assertNotNull(cypherText);
|
||||
assertNotSame(cypherText, plaintext);
|
||||
|
||||
String decrypted = genenCrypt.decryptText(cypherText);
|
||||
|
||||
assertEquals(plaintext.toUpperCase(), decrypted);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
package net.knarcraft.bookswithoutborders.encryption;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
|
||||
public class OneTimePadTest {
|
||||
|
||||
@Test
|
||||
public void oneTimePadTest() {
|
||||
String plaintext = "Very secret text that should be kept secret";
|
||||
String key = "Very secret key!";
|
||||
|
||||
OneTimePad oneTimePad = new OneTimePad(key);
|
||||
String cypherText = oneTimePad.encryptText(plaintext);
|
||||
|
||||
assertNotNull(cypherText);
|
||||
assertNotSame(plaintext, cypherText);
|
||||
|
||||
String decrypted = oneTimePad.decryptText(cypherText);
|
||||
assertEquals(plaintext, decrypted);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
package net.knarcraft.bookswithoutborders.encryption;
|
||||
|
||||
import net.knarcraft.bookswithoutborders.utility.EncryptionHelper;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
|
||||
public class SubstitutionCipherTest {
|
||||
|
||||
@Test
|
||||
public void encryptDecryptTest() {
|
||||
String plaintext = "Very secret text that should be kept secret";
|
||||
String integerKey = EncryptionHelper.getNumberKeyFromStringKey("Very secret key!");
|
||||
SubstitutionCipher substitutionCipher = new SubstitutionCipher(integerKey);
|
||||
String cypherText = substitutionCipher.encryptText(plaintext);
|
||||
|
||||
assertNotNull(cypherText);
|
||||
assertNotSame(plaintext, cypherText);
|
||||
|
||||
String decrypted = substitutionCipher.decryptText(cypherText);
|
||||
assertEquals(plaintext, decrypted);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user