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 :: react router dom v6 private route 
Javascript :: react-floating-whatsapp 
Javascript :: mongoose connection increase timeout in node js 
Javascript :: setimmediate 
Javascript :: render first index active tabs in reactjs 
Javascript :: sweetalert example 
Javascript :: js string to boolean 
Javascript :: react bootstrap table 
Javascript :: javascript valueOf() Method 
Javascript :: import error in react 
Javascript :: pop array 
Javascript :: regex expression for email 
Javascript :: javascript + sync for loop 
Javascript :: change build directory react 
Javascript :: gatsby tailwind 
Javascript :: react router history not defined 
Javascript :: find if two elements in array sum to given integer js 
Javascript :: nodejs heap usage 
Javascript :: entypo icons react native 
Javascript :: vue js readdir 
Javascript :: upload image to server next js 
Javascript :: cypress wait object to change 
Javascript :: define value in js 
Javascript :: react native lottie 
Javascript :: javascript get object where 
Javascript :: Activelink.js 
Javascript :: aws secret manager nodejs 
Javascript :: js array pop 
Javascript :: how to get the text of a clicked elemet by javascript 
Javascript :: string object js 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =