Answers for "javascript caesar cipher"

0

caesar cipher javascript

function rot13(str) {
var charcode = [];
var words = [];

for(let i = 0; i < str.length; i++){
  
  var asc = str.charCodeAt(i)
  
  charcode.push(asc)
}
var converted = charcode.map(function(a){
  return a-13
})
console.log(converted)
converted.forEach((letter)=>{
 if(letter >= 65){
  var final =String.fromCharCode(letter)
  words.push(final)
 }
 else if(letter>=52){
   final = String.fromCharCode(letter+26)
   words.push(final)
 }
 else {
   final = String.fromCharCode(letter+13)
   words.push(final)
 }
 
})
return words.join("")
}

console.log(rot13("SERR YBIR?"));
Posted by: Guest on July-01-2021
0

Caesars Cipher javascript

var myCipher = [];
      var myArray = []; 
      
      for (i=0; i < str.length; i++) {
        // convert character - or don't if it's a punctuation mark.  Ignore spaces.
        if (str.charCodeAt(i) > 64 && str.charCodeAt(i) < 78) {
             myArray.push(String.fromCharCode(str.charCodeAt(i) + 13));
         } else if (str.charCodeAt(i) > 77 && str.charCodeAt(i) < 91) { 
             myArray.push(String.fromCharCode(77 - (90 - str.charCodeAt(i))));
         } else if (str.charCodeAt(i) > 32 && str.charCodeAt(i) < 65) {
             myArray.push(str.charAt(i));
         }
       
        // push word onto array when encountering a space or reaching the end of the string
        
        if (str.charCodeAt(i) == 32) {
          myCipher.push(myArray.join(''));
          myArray.length = 0;      
        }
        
        if (i == (str.length - 1)) {
           myCipher.push(myArray.join(''));
        }

      }

      return myCipher.join(" ");
      
    }
Posted by: Guest on July-12-2020
0

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))
Posted by: Guest on October-25-2021
0

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))
Posted by: Guest on October-25-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language