2018-05-09 21:18:33 +02:00
|
|
|
/**
|
|
|
|
* Cipher functions.
|
|
|
|
*
|
|
|
|
* @author Matt C [matt@artemisbot.uk]
|
2018-05-14 19:30:52 +02:00
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
*
|
2018-05-09 21:18:33 +02:00
|
|
|
* @copyright Crown Copyright 2018
|
|
|
|
* @license Apache-2.0
|
|
|
|
*
|
|
|
|
*/
|
2018-05-17 17:11:34 +02:00
|
|
|
|
2019-07-09 13:23:59 +02:00
|
|
|
import OperationError from "../errors/OperationError.mjs";
|
2018-05-14 19:30:52 +02:00
|
|
|
import CryptoJS from "crypto-js";
|
2018-05-09 21:18:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Affine Cipher Encode operation.
|
|
|
|
*
|
|
|
|
* @author Matt C [matt@artemisbot.uk]
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
export function affineEncode(input, args) {
|
|
|
|
const alphabet = "abcdefghijklmnopqrstuvwxyz",
|
|
|
|
a = args[0],
|
|
|
|
b = args[1];
|
|
|
|
let output = "";
|
|
|
|
|
|
|
|
if (!/^\+?(0|[1-9]\d*)$/.test(a) || !/^\+?(0|[1-9]\d*)$/.test(b)) {
|
2018-05-11 17:32:19 +02:00
|
|
|
throw new OperationError("The values of a and b can only be integers.");
|
2018-05-09 21:18:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < input.length; i++) {
|
|
|
|
if (alphabet.indexOf(input[i]) >= 0) {
|
|
|
|
// Uses the affine function ax+b % m = y (where m is length of the alphabet)
|
|
|
|
output += alphabet[((a * alphabet.indexOf(input[i])) + b) % 26];
|
|
|
|
} else if (alphabet.indexOf(input[i].toLowerCase()) >= 0) {
|
|
|
|
// Same as above, accounting for uppercase
|
|
|
|
output += alphabet[((a * alphabet.indexOf(input[i].toLowerCase())) + b) % 26].toUpperCase();
|
|
|
|
} else {
|
|
|
|
// Non-alphabetic characters
|
|
|
|
output += input[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
2018-05-09 21:28:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a polybius square for the given keyword
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @author Matt C [matt@artemisbot.uk]
|
|
|
|
* @param {string} keyword - Must be upper case
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
export function genPolybiusSquare (keyword) {
|
|
|
|
const alpha = "ABCDEFGHIKLMNOPQRSTUVWXYZ",
|
|
|
|
polArray = `${keyword}${alpha}`.split("").unique(),
|
|
|
|
polybius = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
|
polybius[i] = polArray.slice(i*5, i*5 + 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
return polybius;
|
|
|
|
}
|
2018-05-14 19:30:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A mapping of string formats to their classes in the CryptoJS library.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @constant
|
|
|
|
*/
|
|
|
|
export const format = {
|
|
|
|
"Hex": CryptoJS.enc.Hex,
|
|
|
|
"Base64": CryptoJS.enc.Base64,
|
|
|
|
"UTF8": CryptoJS.enc.Utf8,
|
|
|
|
"UTF16": CryptoJS.enc.Utf16,
|
|
|
|
"UTF16LE": CryptoJS.enc.Utf16LE,
|
|
|
|
"UTF16BE": CryptoJS.enc.Utf16BE,
|
|
|
|
"Latin1": CryptoJS.enc.Latin1,
|
|
|
|
};
|