mirror of
https://github.com/gchq/CyberChef.git
synced 2025-05-11 16:51:31 -04:00
Implemented offset and added tests
This commit is contained in:
parent
e7515ffa63
commit
317215ab9d
3 changed files with 123 additions and 86 deletions
|
@ -7,19 +7,19 @@
|
|||
import Operation from "../Operation.mjs";
|
||||
|
||||
/**
|
||||
* Rail Fence Cypher Decode operation
|
||||
* Rail Fence Cipher Decode operation
|
||||
*/
|
||||
class RailFenceCypherDecode extends Operation {
|
||||
class RailFenceCipherDecode extends Operation {
|
||||
|
||||
/**
|
||||
* RailFenceCypherDecode constructor
|
||||
* RailFenceCipherDecode constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Rail Fence Cypher Decode";
|
||||
this.name = "Rail Fence Cipher Decode";
|
||||
this.module = "Ciphers";
|
||||
this.description = "Decodes Strings that were created using the Rail fence Cypher provided a key and an offset";
|
||||
this.description = "Decodes Strings that were created using the Rail fence Cipher provided a key and an offset";
|
||||
this.infoURL = "https://en.wikipedia.org/wiki/Rail_fence_cipher";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
|
@ -43,8 +43,7 @@ class RailFenceCypherDecode extends Operation {
|
|||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const [key] = args;
|
||||
// const [key, offset] = args;
|
||||
const [key, offset] = args;
|
||||
|
||||
let cipher = input;
|
||||
|
||||
|
@ -54,51 +53,35 @@ class RailFenceCypherDecode extends Operation {
|
|||
return "Key should be smaller than the cipher's length";
|
||||
}
|
||||
|
||||
if (offset < 0) {
|
||||
return "Offset has to be a positive integer";
|
||||
}
|
||||
|
||||
const cycle = (key - 1) * 2;
|
||||
|
||||
const rest = cipher.length % key;
|
||||
|
||||
if (rest !== 0) {
|
||||
cipher = cipher + (" ".repeat(key-rest));
|
||||
cipher = cipher + (" ".repeat(key - rest));
|
||||
}
|
||||
|
||||
const blockLen = cipher.length / key;
|
||||
const plaintext = new Array(cipher.length);
|
||||
|
||||
let plaintext = "";
|
||||
let j = 0;
|
||||
let x, y;
|
||||
|
||||
for (let i = 0; i < blockLen; i++) {
|
||||
for (let j = 0; j < key; j++) {
|
||||
plaintext += cipher[i + (j * blockLen)];
|
||||
for (y = 0; y < key; y++) {
|
||||
for (x = 0; x < cipher.length; x++) {
|
||||
if ((y + x + offset) % cycle === 0 || (y - x - offset) % cycle === 0) {
|
||||
plaintext[x] = cipher[j++];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return plaintext;
|
||||
return plaintext.join("").trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Highlight Rail Fence Cypher Decode
|
||||
*
|
||||
* @param {Object[]} pos
|
||||
* @param {number} pos[].start
|
||||
* @param {number} pos[].end
|
||||
* @param {Object[]} args
|
||||
* @returns {Object[]} pos
|
||||
*/
|
||||
highlight(pos, args) {
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Highlight Rail Fence Cypher Decode in reverse
|
||||
*
|
||||
* @param {Object[]} pos
|
||||
* @param {number} pos[].start
|
||||
* @param {number} pos[].end
|
||||
* @param {Object[]} args
|
||||
* @returns {Object[]} pos
|
||||
*/
|
||||
highlightReverse(pos, args) {
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export default RailFenceCypherDecode;
|
||||
export default RailFenceCipherDecode;
|
|
@ -7,19 +7,19 @@
|
|||
import Operation from "../Operation.mjs";
|
||||
|
||||
/**
|
||||
* Rail Fence Cypher Encode operation
|
||||
* Rail Fence Cipher Encode operation
|
||||
*/
|
||||
class RailFenceCypherEncode extends Operation {
|
||||
class RailFenceCipherEncode extends Operation {
|
||||
|
||||
/**
|
||||
* RailFenceCypherEncode constructor
|
||||
* RailFenceCipherEncode constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Rail Fence Cypher Encode";
|
||||
this.name = "Rail Fence Cipher Encode";
|
||||
this.module = "Ciphers";
|
||||
this.description = "Encodes Strings using the Rail fence Cypher provided a key and an offset";
|
||||
this.description = "Encodes Strings using the Rail fence Cipher provided a key and an offset";
|
||||
this.infoURL = "https://en.wikipedia.org/wiki/Rail_fence_cipher";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
|
@ -43,54 +43,31 @@ class RailFenceCypherEncode extends Operation {
|
|||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const [key] = args;
|
||||
// const [key, offset] = args;
|
||||
const [key, offset] = args;
|
||||
|
||||
const plaintext = input;
|
||||
|
||||
if (key < 2) {
|
||||
return "Key has to be bigger than 2";
|
||||
} else if (key > plaintext.length) {
|
||||
return "Key should be smaller than the plain text's length";
|
||||
}
|
||||
|
||||
let cipher = "";
|
||||
|
||||
for (let block = 0; block < key; block++) {
|
||||
for (let pos = block; pos < plaintext.length; pos += key) {
|
||||
cipher += plaintext[pos];
|
||||
}
|
||||
if (offset < 0) {
|
||||
return "Offset has to be a positive integer";
|
||||
}
|
||||
|
||||
return cipher;
|
||||
}
|
||||
const cycle = (key - 1) * 2;
|
||||
const rows = new Array(key).fill("");
|
||||
|
||||
/**
|
||||
* Highlight Rail Fence Cypher Encode
|
||||
*
|
||||
* @param {Object[]} pos
|
||||
* @param {number} pos[].start
|
||||
* @param {number} pos[].end
|
||||
* @param {Object[]} args
|
||||
* @returns {Object[]} pos
|
||||
*/
|
||||
highlight(pos, args) {
|
||||
return pos;
|
||||
}
|
||||
for (let pos = 0; pos < plaintext.length; pos++) {
|
||||
const rowIdx = key - 1 - Math.abs(cycle / 2 - (pos + offset) % cycle);
|
||||
|
||||
/**
|
||||
* Highlight Rail Fence Cypher Encode in reverse
|
||||
*
|
||||
* @param {Object[]} pos
|
||||
* @param {number} pos[].start
|
||||
* @param {number} pos[].end
|
||||
* @param {Object[]} args
|
||||
* @returns {Object[]} pos
|
||||
*/
|
||||
highlightReverse(pos, args) {
|
||||
return pos;
|
||||
rows[rowIdx] += plaintext[pos];
|
||||
}
|
||||
|
||||
return rows.join("").trim();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default RailFenceCypherEncode;
|
||||
export default RailFenceCipherEncode;
|
|
@ -420,36 +420,113 @@ TestRegister.addTests([
|
|||
],
|
||||
},
|
||||
{
|
||||
name: "Rail Fence Cypher Decode: normal",
|
||||
name: "Rail Fence Cipher Decode: normal",
|
||||
input: "Cytgah sTEAto rtn rsligcdsrporpyi H r fWiigo ovn oe",
|
||||
expectedOutput: "Cryptography is THE Art of Writing or solving codes",
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Rail Fence Cypher Decode",
|
||||
"op": "Rail Fence Cipher Decode",
|
||||
"args": [2, 0]
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Rail Fence Cypher Decode: key has to be bigger than 2",
|
||||
name: "Rail Fence Cipher Decode: key has to be bigger than 2",
|
||||
input: "Cytgah sTEAto rtn rsligcdsrporpyi H r fWiigo ovn oe",
|
||||
expectedOutput: "Key has to be bigger than 2",
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Rail Fence Cypher Decode",
|
||||
"op": "Rail Fence Cipher Decode",
|
||||
"args": [1, 0]
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Rail Fence Cypher Decode: key has to be smaller than input's length",
|
||||
name: "Rail Fence Cipher Decode: key has to be smaller than input's length",
|
||||
input: "shortinput",
|
||||
expectedOutput: "Key should be smaller than the cipher's length",
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Rail Fence Cypher Decode",
|
||||
"op": "Rail Fence Cipher Decode",
|
||||
"args": [22, 0]
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Rail Fence Cipher Decode: offset should be positive",
|
||||
input: "shortinput",
|
||||
expectedOutput: "Offset has to be a positive integer",
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Rail Fence Cipher Decode",
|
||||
"args": [2, -1]
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Rail Fence Cipher Decode: Normal with Offset non-null",
|
||||
input: "51746026813793592840",
|
||||
expectedOutput: "12345678901234567890",
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Rail Fence Cipher Decode",
|
||||
"args": [4, 2]
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Rail Fence Cipher Encode: normal",
|
||||
input: "Cryptography is THE Art of Writing or solving codes",
|
||||
expectedOutput: "Cytgah sTEAto rtn rsligcdsrporpyi H r fWiigo ovn oe",
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Rail Fence Cipher Encode",
|
||||
"args": [2, 0]
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Rail Fence Cipher Encode: key has to be bigger than 2",
|
||||
input: "Cryptography is THE Art of Writing or solving codes",
|
||||
expectedOutput: "Key has to be bigger than 2",
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Rail Fence Cipher Encode",
|
||||
"args": [1, 0]
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Rail Fence Cipher Encode: key has to be smaller than input's length",
|
||||
input: "shortinput",
|
||||
expectedOutput: "Key should be smaller than the plain text's length",
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Rail Fence Cipher Encode",
|
||||
"args": [22, 0]
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Rail Fence Cipher Encode: offset should be positive",
|
||||
input: "shortinput",
|
||||
expectedOutput: "Offset has to be a positive integer",
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Rail Fence Cipher Encode",
|
||||
"args": [2, -1]
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Rail Fence Cipher Encode: Normal with Offset non-null",
|
||||
input: "12345678901234567890",
|
||||
expectedOutput: "51746026813793592840",
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Rail Fence Cipher Encode",
|
||||
"args": [4, 2]
|
||||
}
|
||||
],
|
||||
},
|
||||
]);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue