Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript caesar cipher

const caesarEncoded = (s, n) => {
	let alphabet = 'abcdefghijklmnopqrstuvwxyz'
	let lc = alphabet.replace(/s/g, '').toLowerCase().split('')
	let uc = alphabet.replace(/s/g, '').toUpperCase().split('')

	return Array.from(s)
		.map((v) => {
			if (lc.indexOf(v.toLowerCase()) === -1 || uc.indexOf(v.toUpperCase()) === -1) {
				return v
			}

			const lcEncryptIndex = (lc.indexOf(v.toLowerCase()) + n) % alphabet.length
			const lcEncryptedChar = lc[lcEncryptIndex]

			const ucEncryptIndex = (uc.indexOf(v.toUpperCase()) + n) % alphabet.length
			const ucEncryptedChar = uc[ucEncryptIndex]

			return lc.indexOf(v) !== -1 ? lcEncryptedChar : ucEncryptedChar
		})
		.join('')
}

const caesarDecoded = (s, n) => {
	let alphabet = 'abcdefghijklmnopqrstuvwxyz'
	let lc = alphabet.replace(/s/g, '').toLowerCase().split('')
	let uc = alphabet.replace(/s/g, '').toUpperCase().split('')

	return Array.from(s)
		.map((v) => {
			if (lc.indexOf(v.toLowerCase()) === -1 || uc.indexOf(v.toUpperCase()) === -1) {
				return v
			}

			let lcEncryptIndex = (lc.indexOf(v.toLowerCase()) - n) % alphabet.length
			lcEncryptIndex = lcEncryptIndex < 0 ? lcEncryptIndex + alphabet.length : lcEncryptIndex
			const lcEncryptedChar = lc[lcEncryptIndex]

			let ucEncryptIndex = (uc.indexOf(v.toUpperCase()) - n) % alphabet.length
			ucEncryptIndex = ucEncryptIndex < 0 ? ucEncryptIndex + alphabet.length : ucEncryptIndex
			const ucEncryptedChar = uc[ucEncryptIndex]

			return lc.indexOf(v) !== -1 ? lcEncryptedChar : ucEncryptedChar
		})
		.join('')
}

console.log(caesarEncoded('XVCJ9', 5))
console.log(caesarDecoded('CAHO9', 5))
Comment

javascript caeser cipher

function cipherRot13(str) {
  str = str.toUpperCase();
  return str.replace(/[A-Z]/g, rot13);

  function rot13(correspondance) {
    const charCode = correspondance.charCodeAt();
    //A = 65, Z = 90
    return String.fromCharCode(
            ((charCode + 13) <= 90) ? charCode + 13
                                    : (charCode + 13) % 90 + 64
           );
    
  }
}
Comment

javascript caesar cipher

const alphabet = [
	'A',
	'B',
	'C',
	'D',
	'E',
	'F',
	'G',
	'H',
	'I',
	'J',
	'K',
	'L',
	'M',
	'N',
	'O',
	'P',
	'Q',
	'R',
	'S',
	'T',
	'U',
	'V',
	'W',
	'X',
	'Y',
	'Z'
]
const MAX_ROT = alphabet.length

/**
 * Encrypt text  using spanish Cesar encryption
 * @memberof crypt/cesar
 * @param {string} text - A string to be encrypted
 * @param {number} rot - Scroll numer, can be from 0 to 26
 * @returns {string} - Encrypted text
 */
const encrypt = (text, rot = 3) => {
	if (rot > MAX_ROT || rot < 0) {
		throw Error('rot can be only beetween 0 to 26')
	}

	text = Array.from(text)

	const encryptedText = text.map((char) => {
		const isLower = char.toLowerCase()
		const idx = alphabet.indexOf(char.toUpperCase())

		if (idx === -1) {
			return char
		}

		const encryptedIdx = (idx + rot) % MAX_ROT
		const encryptedChar = alphabet[encryptedIdx]

		return isLower ? encryptedChar.toLowerCase() : encryptedChar
	})

	return encryptedText.join('')
}

/**
 * Decrypt text using spanish Cesar encryption
 * @memberof crypt/cesar
 * @param {string} text - A string to be decrypted
 * @param {number} rot - Scroll numer, can be from 0 to 26
 * @returns {string} - Decrypted text
 */
const decrypt = (text, rot = 3) => {
	if (rot > MAX_ROT || rot < 0) {
		throw Error('rot can be only beetween 0 to 26')
	}

	text = Array.from(text)

	const decryptedText = text.map((char) => {
		const isLower = char.toLowerCase()
		const idx = alphabet.indexOf(char.toUpperCase())

		if (idx === -1) {
			return char
		}

		let decryptedIdx = (idx - rot) % MAX_ROT

		decryptedIdx = decryptedIdx < 0 ? decryptedIdx + MAX_ROT : decryptedIdx

		const decryptedChar = alphabet[decryptedIdx]

		return isLower ? decryptedChar.toLowerCase() : decryptedChar
	})

	return decryptedText.join('')
}

console.log(encrypt('abcxyz', 5))
console.log(decrypt('fghcde', 5))
Comment

PREVIOUS NEXT
Code Example
Javascript :: check if is function javascript 
Javascript :: convert json to dataframe python 
Javascript :: jquery change input value if greater than 
Javascript :: prime number js 
Javascript :: remove event listener jquery 
Javascript :: js toggle value 
Javascript :: queryselectorall data attribute 
Javascript :: iseet jquery 
Javascript :: button in vanilla js 
Javascript :: how to convert kilomer to meter in javascript 
Javascript :: how to get element of an array in javascript 
Javascript :: scrollbar automatically scroll down as new divs are added reactjs 
Javascript :: play audio with js 
Javascript :: remove class element 
Javascript :: javascript password generator 
Javascript :: gulp synchronous tasks 
Javascript :: reload page in react router dom v6 
Javascript :: node mssql 
Javascript :: nodejs date difference 
Javascript :: js get parameters 
Javascript :: discord.js verify 
Javascript :: how to handle all error of all router in express 
Javascript :: create module in js 
Javascript :: jquery get selected checkbox value array 
Javascript :: react useeffect async javascript 
Javascript :: javascript strip 
Javascript :: remove element from array in js 
Javascript :: how to create a package.json file in npm 
Javascript :: javascript conver time into 24 hour format 
Javascript :: jquery if .val is blank 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =