MCPcopy Create free account
hub / github.com/TheAlgorithms/JavaScript / encrypt

Function encrypt

Ciphers/AffineCipher.js:72–82  ·  view source on GitHub ↗

* Encrypt a Affine Cipher * @param {String} str - String to be encrypted * @param {Number} a - A coefficient * @param {Number} b - B coefficient * @return {String} result - Encrypted string

(str, a, b)

Source from the content-addressed store, hash-verified

70 */
71
72function encrypt(str, a, b) {
73 let result = ''
74 if (isCorrectFormat(str, a, b)) {
75 for (let x = 0; x < str.length; x++) {
76 const charIndex = findCharIndex(str[x])
77 if (charIndex < 0) result += '-1' + ' '
78 else result += key.charAt(mod(a * charIndex + b, 26)) + ' '
79 }
80 }
81 return result.trim()
82}
83
84/**
85 * Decrypt a Affine Cipher

Callers 1

Calls 3

isCorrectFormatFunction · 0.85
findCharIndexFunction · 0.85
modFunction · 0.85

Tested by

no test coverage detected