mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-23 00:06:17 -04:00
Checkbox for mod inverse; calculations implemented
This commit is contained in:
parent
7eb887ca51
commit
de57002c6f
1 changed files with 21 additions and 5 deletions
|
@ -35,6 +35,11 @@ class AffineCipherDecode extends Operation {
|
||||||
"name": "b",
|
"name": "b",
|
||||||
"type": "number",
|
"type": "number",
|
||||||
"value": 0
|
"value": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Use modular inverse values",
|
||||||
|
"type": "boolean",
|
||||||
|
"value": false
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -48,7 +53,7 @@ class AffineCipherDecode extends Operation {
|
||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
const alphabet = "abcdefghijklmnopqrstuvwxyz",
|
const alphabet = "abcdefghijklmnopqrstuvwxyz",
|
||||||
[a, b] = args,
|
[a, b, useInv] = args,
|
||||||
aModInv = Utils.modInv(a, 26); // Calculates modular inverse of a
|
aModInv = Utils.modInv(a, 26); // Calculates modular inverse of a
|
||||||
let output = "";
|
let output = "";
|
||||||
|
|
||||||
|
@ -62,11 +67,22 @@ class AffineCipherDecode extends Operation {
|
||||||
|
|
||||||
for (let i = 0; i < input.length; i++) {
|
for (let i = 0; i < input.length; i++) {
|
||||||
if (alphabet.indexOf(input[i]) >= 0) {
|
if (alphabet.indexOf(input[i]) >= 0) {
|
||||||
// Uses the affine decode function (y-b * A') % m = x (where m is length of the alphabet and A' is modular inverse)
|
if (useInv) {
|
||||||
output += alphabet[Utils.mod((alphabet.indexOf(input[i]) - b) * aModInv, 26)];
|
// Uses the affine decode function (y+b)*a % m, where m is the length
|
||||||
|
// of the alphabet and a,b are modular inverses of the original a,b
|
||||||
|
output += alphabet[Utils.mod((alphabet.indexOf(input[i]) + b) * a, 26)];
|
||||||
|
} else {
|
||||||
|
// Uses the affine decode function (y-b * A') % m = x (where m is length of the alphabet and A' is modular inverse)
|
||||||
|
output += alphabet[Utils.mod((alphabet.indexOf(input[i]) - b) * aModInv, 26)];
|
||||||
|
}
|
||||||
} else if (alphabet.indexOf(input[i].toLowerCase()) >= 0) {
|
} else if (alphabet.indexOf(input[i].toLowerCase()) >= 0) {
|
||||||
// Same as above, accounting for uppercase
|
if (useInv) {
|
||||||
output += alphabet[Utils.mod((alphabet.indexOf(input[i].toLowerCase()) - b) * aModInv, 26)].toUpperCase();
|
// Same as above, accounting for uppercase
|
||||||
|
output += alphabet[Utils.mod((alphabet.indexOf(input[i].toLowerCase()) + b) * a, 26)].toUpperCase();
|
||||||
|
} else {
|
||||||
|
// Same as above, accounting for uppercase
|
||||||
|
output += alphabet[Utils.mod((alphabet.indexOf(input[i].toLowerCase()) - b) * aModInv, 26)].toUpperCase();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Non-alphabetic characters
|
// Non-alphabetic characters
|
||||||
output += input[i];
|
output += input[i];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue