Skip to content

Commit

Permalink
Fix parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
philsmart committed Oct 5, 2022
1 parent 20e73fa commit 1ed1883
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class UserEncryptionContext {
public UserEncryptionContext() {
encMessage = "Text";
encKey = "0";
chosenEncFunction = "caesar-cipher";
chosenEncFunction = "(a) caesar-cipher";
}

public boolean isImageInitialised() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.stream.Collectors;

import javax.script.ScriptException;
Expand All @@ -30,7 +32,7 @@
import uk.ac.cardiff.nsa.hashenc.engine.ScriptHelper;

/**
* Basic Encryption controller. Is not thread-safe and will leak settings between users.
* Basic Encryption controller.
*
* <p>
* Note, 'message' is used here in place of 'plaintext'.
Expand All @@ -44,12 +46,12 @@ public class EncryptionController {
private final Logger log = LoggerFactory.getLogger(EncryptionController.class);

/** A fixed list of encryption script resources. */
private final Map<String, Resource> encryptionScriptResources = Map.of("caesar-cipher",
new ClassPathResource("scripts/encryption/caesar-cipher.js"), "one-time-pad",
new ClassPathResource("scripts/encryption/one-time-pad.js"), "basic(none)",
new ClassPathResource("scripts/encryption/basic.js"), "block-cipher",
new ClassPathResource("scripts/encryption/block-cipher.js"), "block-cipher-chaining (Experimental)",
new ClassPathResource("scripts/encryption/block-cipher-chaining.js"), "block-cipher-counter (Experimental)",
private final Map<String, Resource> encryptionScriptResources = Map.of("(a) caesar-cipher",
new ClassPathResource("scripts/encryption/caesar-cipher.js"), "(c) one-time-pad",
new ClassPathResource("scripts/encryption/one-time-pad.js"), "(z) basic (none)",
new ClassPathResource("scripts/encryption/basic.js"), "(b) block-cipher",
new ClassPathResource("scripts/encryption/block-cipher.js"), "(z) block-cipher-chaining (Experimental)",
new ClassPathResource("scripts/encryption/block-cipher-chaining.js"), "(z) block-cipher-counter (Experimental)",
new ClassPathResource("scripts/encryption/block-cipher-counter.js"));

/**
Expand All @@ -65,11 +67,12 @@ public class EncryptionController {
public EncryptionController() {

// Load the scripts
encryptionScripts = new HashMap<>(encryptionScriptResources.size());
encryptionScripts = new TreeMap<>();
for (final Map.Entry<String, Resource> resource : encryptionScriptResources.entrySet()) {
final String resourceAsScript = ScriptHelper.loadScriptResourceToString(resource.getValue());
encryptionScripts.put(resource.getKey(), resourceAsScript);
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ var cipherTextAsBytes = function encrypt(messageAsBytes, keyAsBytes) {
}
// 8 bit keys, so fix the block to 8 bits or 1 byte, so byte for byte
var blockSize = 1;
// IV is a byte between 0 and 255 (00000000 and 11111111). The IV MUST normally
// Change for each enc operation, so this is flawed.
// Here, the IV is a byte between 0 and 255 (00000000 and 11111111). The IV MUST normally
// be random, and change for each enc operation, so this is flawed.
var iv = 100;
for (var i = 0; i < messageAsBytes.length; i = i + blockSize) {
if (i == 0){
Expand Down
6 changes: 2 additions & 4 deletions src/main/resources/scripts/encryption/block-cipher.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ var cipherTextAsBytes = function encrypt(messageAsBytes, keyAsBytes){
for (var i=0; i<messageAsBytes.length; i=i+keyAsBytes.length) {
// Usually much more happens during each 'block' enc. step e.g. see SP networks
for (var j = 0; j < keyAsBytes.length; j++){
var b = messageAsBytes[i+j] ^ keyAsBytes[j];
messageAsBytes[i+j] = b;
messageAsBytes[i+j] = messageAsBytes[i+j] ^ keyAsBytes[j];
}
}
return messageAsBytes;
Expand All @@ -20,8 +19,7 @@ var decodedMessageAsBytes = function decrypt(encryptedMessageAsBytes, keyAsBytes
}
for (var i=0; i<encryptedMessageAsBytes.length; i=i+keyAsBytes.length) {
for (var j = 0; j < keyAsBytes.length; j++){
var decryptedByte = encryptedMessageAsBytes[i+j] ^ keyAsBytes[j];
decryptedArray[i+j] = decryptedByte;
decryptedArray[i+j] = encryptedMessageAsBytes[i+j] ^ keyAsBytes[j];
}
}
return decryptedArray;
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/scripts/encryption/caesar-cipher.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Note, a Caesar cipher would not normally operate over a generic set of bytes like
// it is here. So this is not a true Caesar Cipher. It simply shifts (increments) the
// number corresponding to the byte by the shift, and does not wrap around if that byte no
// longer represents an alphanumeric e.g. Z plus a shift of 1 will give the UTF-8 (and ASCII)
// character of '{' and not 'a' as it should.

var cipherTextAsBytes = function encrypt(messageAsBytes, keyAsBytes) {
if (keyAsBytes.length > 1) {
print("Caesar cipher key should be 1 byte");
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/templates/enc.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ <h1>Shall we play more games?</h1>

<p style="font-weight: bold;">1</p>
<div id="game-container">

<p><b>Note</b>, these algorithms are for demonstration only! they are not usable encryption
schemes.</p>
<p>
<b>JavaScript Encryption function. Must return a byte array.</b>

<form th:action="@{/set-enc-template}" th:object="${templateScript}"
method="post" style="display: inline">
<select id="templateScript" name="templateScript">
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/templates/hashing-usage.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ <h1>Shall we play a game?</h1>
</tr>

<tr>
<td><span>Digest:sha256: 40d2c42d9c170b0699fd898acecf01df0ccd8efc70b294be17816956b265b968</span></td>
<td><span th:text="${docTwoHash}"></span></td>
<td><span>Digest: 40d2c42d9c170b0699fd898acecf01df0ccd8efc70b294be17816956b265b968</span></td>
<td><span>Digest:</span><span th:text="${docTwoHash}"></span></td>
</tr>
<tr>
<td th:if="${match!=null}"><p>
Has the messages integrity been preserved? <span
Has the message integrity been preserved? <span
th:if="${!match}">no sorry</span> <span th:if="${match}">yes!</span>
</p></td>

Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/templates/hashing.html
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ <h1>Shall we play a game?</h1>

</th:block>
</ul>
If there is more than one output, we have likely not succeeded.
<p>
You will never really know if this is a preimage, or a second preimage!
</p>
</div>

Expand Down

0 comments on commit 1ed1883

Please sign in to comment.