mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-24 08:46:19 -04:00
Implemented the Rail Fence Cipher with both encoding and decoding
This commit is contained in:
parent
f7be8d720b
commit
1509b2b96c
4 changed files with 272 additions and 0 deletions
|
@ -95,6 +95,8 @@
|
|||
"Affine Cipher Decode",
|
||||
"A1Z26 Cipher Encode",
|
||||
"A1Z26 Cipher Decode",
|
||||
"Rail Fence Cipher Encode",
|
||||
"Rail Fence Cipher Decode",
|
||||
"Atbash Cipher",
|
||||
"Substitute",
|
||||
"Derive PBKDF2 key",
|
||||
|
|
87
src/core/operations/RailFenceCipherDecode.mjs
Normal file
87
src/core/operations/RailFenceCipherDecode.mjs
Normal file
|
@ -0,0 +1,87 @@
|
|||
/**
|
||||
* @author Flavio Diez [flaviofdiez+cyberchef@gmail.com]
|
||||
* @copyright Crown Copyright 2020
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
|
||||
/**
|
||||
* Rail Fence Cipher Decode operation
|
||||
*/
|
||||
class RailFenceCipherDecode extends Operation {
|
||||
|
||||
/**
|
||||
* RailFenceCipherDecode constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Rail Fence Cipher Decode";
|
||||
this.module = "Ciphers";
|
||||
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";
|
||||
this.args = [
|
||||
{
|
||||
name: "Key",
|
||||
type: "number",
|
||||
value: 2
|
||||
},
|
||||
{
|
||||
name: "Offset",
|
||||
type: "number",
|
||||
value: 0
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const [key, offset] = args;
|
||||
|
||||
let cipher = input;
|
||||
|
||||
if (key < 2) {
|
||||
return "Key has to be bigger than 2";
|
||||
} else if (key > cipher.length) {
|
||||
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));
|
||||
}
|
||||
|
||||
const plaintext = new Array(cipher.length);
|
||||
|
||||
let j = 0;
|
||||
let x, y;
|
||||
|
||||
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.join("").trim();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
export default RailFenceCipherDecode;
|
73
src/core/operations/RailFenceCipherEncode.mjs
Normal file
73
src/core/operations/RailFenceCipherEncode.mjs
Normal file
|
@ -0,0 +1,73 @@
|
|||
/**
|
||||
* @author Flavio Diez [flaviofdiez+cyberchef@gmail.com]
|
||||
* @copyright Crown Copyright 2020
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
|
||||
/**
|
||||
* Rail Fence Cipher Encode operation
|
||||
*/
|
||||
class RailFenceCipherEncode extends Operation {
|
||||
|
||||
/**
|
||||
* RailFenceCipherEncode constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Rail Fence Cipher Encode";
|
||||
this.module = "Ciphers";
|
||||
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";
|
||||
this.args = [
|
||||
{
|
||||
name: "Key",
|
||||
type: "number",
|
||||
value: 2
|
||||
},
|
||||
{
|
||||
name: "Offset",
|
||||
type: "number",
|
||||
value: 0
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, 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";
|
||||
}
|
||||
|
||||
if (offset < 0) {
|
||||
return "Offset has to be a positive integer";
|
||||
}
|
||||
|
||||
const cycle = (key - 1) * 2;
|
||||
const rows = new Array(key).fill("");
|
||||
|
||||
for (let pos = 0; pos < plaintext.length; pos++) {
|
||||
const rowIdx = key - 1 - Math.abs(cycle / 2 - (pos + offset) % cycle);
|
||||
|
||||
rows[rowIdx] += plaintext[pos];
|
||||
}
|
||||
|
||||
return rows.join("").trim();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default RailFenceCipherEncode;
|
Loading…
Add table
Add a link
Reference in a new issue