2018-05-09 20:18:33 +01:00
/ * *
* @ author Matt C [ matt @ artemisbot . uk ]
* @ copyright Crown Copyright 2018
* @ license Apache - 2.0
* /
2019-07-09 12:23:59 +01:00
import Operation from "../Operation.mjs" ;
2024-06-13 06:44:48 +00:00
import { affineDecrypt , affineDecryptInverse , AFFINE _ALPHABETS } from "../lib/Ciphers.mjs" ;
2018-05-09 20:18:33 +01:00
/ * *
* Affine Cipher Decode operation
* /
class AffineCipherDecode extends Operation {
/ * *
* AffineCipherDecode constructor
* /
constructor ( ) {
super ( ) ;
this . name = "Affine Cipher Decode" ;
this . module = "Ciphers" ;
2024-06-13 17:42:54 +00:00
this . description = "The Affine cipher is a type of monoalphabetic substitution cipher. To decrypt, each letter in an alphabet is mapped to its numeric equivalent, decrypted by a mathematical function (the inverse of ax+b % m), and converted back to a letter." ;
2018-08-21 19:07:13 +01:00
this . infoURL = "https://wikipedia.org/wiki/Affine_cipher" ;
2018-05-09 20:18:33 +01:00
this . inputType = "string" ;
this . outputType = "string" ;
this . args = [
{
"name" : "a" ,
"type" : "number" ,
"value" : 1
} ,
{
"name" : "b" ,
"type" : "number" ,
"value" : 0
2024-06-12 14:57:27 +00:00
} ,
2024-06-13 06:44:48 +00:00
{
"name" : "Alphabet" ,
"type" : "editableOption" ,
"value" : AFFINE _ALPHABETS
} ,
2024-06-12 14:57:27 +00:00
{
"name" : "Use modular inverse values" ,
"type" : "boolean" ,
"value" : false
2018-05-09 20:18:33 +01:00
}
] ;
}
/ * *
* @ param { string } input
* @ param { Object [ ] } args
* @ returns { string }
2018-05-15 18:01:04 +01:00
*
* @ throws { OperationError } if a or b values are invalid
2018-05-09 20:18:33 +01:00
* /
run ( input , args ) {
2024-06-13 06:44:48 +00:00
const a = args [ 0 ] , b = args [ 1 ] , alphabet = args [ 2 ] , useInverse = args [ 3 ] ;
if ( useInverse ) return affineDecryptInverse ( input , a , b , alphabet ) ;
else return affineDecrypt ( input , a , b , alphabet ) ;
2018-05-09 20:18:33 +01:00
}
/ * *
* Highlight Affine Cipher Decode
*
* @ param { Object [ ] } pos
* @ param { number } pos [ ] . start
* @ param { number } pos [ ] . end
* @ param { Object [ ] } args
* @ returns { Object [ ] } pos
* /
highlight ( pos , args ) {
return pos ;
}
/ * *
* Highlight Affine Cipher 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 AffineCipherDecode ;