2018-05-09 21:13:09 +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" ;
import OperationError from "../errors/OperationError.mjs" ;
2018-05-09 21:13:09 +01:00
/ * *
2024-10-01 09:52:35 +05:30
* Vigenère Encode operation is done
2018-05-09 21:13:09 +01:00
* /
class VigenèreEncode extends Operation {
/ * *
* VigenèreEncode constructor
* /
constructor ( ) {
super ( ) ;
this . name = "Vigenère Encode" ;
this . module = "Ciphers" ;
this . description = "The Vigenere cipher is a method of encrypting alphabetic text by using a series of different Caesar ciphers based on the letters of a keyword. It is a simple form of polyalphabetic substitution." ;
2018-08-21 19:07:13 +01:00
this . infoURL = "https://wikipedia.org/wiki/Vigenère_cipher" ;
2018-05-09 21:13:09 +01:00
this . inputType = "string" ;
this . outputType = "string" ;
this . args = [
{
"name" : "Key" ,
"type" : "string" ,
"value" : ""
}
] ;
}
/ * *
* @ param { string } input
* @ param { Object [ ] } args
* @ returns { string }
* /
run ( input , args ) {
2024-10-01 10:30:33 +05:30
const alphabet = "abcdefghijklmnopqrstuvwxyz0123456789" ,
2018-05-09 21:13:09 +01:00
key = args [ 0 ] . toLowerCase ( ) ;
2024-10-01 10:30:33 +05:30
let output = "" ;
let fail = 0 ;
let keyIndex , msgIndex , chr ;
2018-05-09 21:13:09 +01:00
2018-05-11 16:32:19 +01:00
if ( ! key ) throw new OperationError ( "No key entered" ) ;
2024-10-01 10:30:33 +05:30
if ( ! /^[a-zA-Z0-9]+$/ . test ( key ) ) throw new OperationError ( "The key must consist only of letters and numbers" ) ;
2018-05-09 21:13:09 +01:00
for ( let i = 0 ; i < input . length ; i ++ ) {
2024-10-01 10:30:33 +05:30
const currentChar = input [ i ] ;
const lowerChar = currentChar . toLowerCase ( ) ;
//Implementing logic
if ( alphabet . indexOf ( lowerChar ) >= 0 ) {
// Get the corresponding character of key for the current letter
2018-05-09 21:13:09 +01:00
chr = key [ ( i - fail ) % key . length ] ;
keyIndex = alphabet . indexOf ( chr ) ;
2024-10-01 10:30:33 +05:30
msgIndex = alphabet . indexOf ( lowerChar ) ;
const encodedChar = alphabet [ ( keyIndex + msgIndex ) % alphabet . length ] ;
// Preserve case for letters
output += currentChar === lowerChar ? encodedChar : encodedChar . toUpperCase ( ) ;
2018-05-09 21:13:09 +01:00
} else {
2024-10-01 10:30:33 +05:30
output += currentChar ; // Keep non-alphabetic characters as is
2018-05-09 21:13:09 +01:00
fail ++ ;
}
}
return output ;
}
/ * *
* Highlight Vigenère Encode
*
* @ param { Object [ ] } pos
* @ param { number } pos [ ] . start
* @ param { number } pos [ ] . end
* @ param { Object [ ] } args
* @ returns { Object [ ] } pos
* /
highlight ( pos , args ) {
return pos ;
}
/ * *
* Highlight Vigenère 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 ;
}
}
export default VigenèreEncode ;