caesar cipher
public static String incode(String strIn, int delta) {
String strOut = "";
// bring delta to positive value
while (delta < 0) {
delta += 26;
}
for (int i = 0; i < strIn.length(); i++) {
char c = strIn.charAt(i);
if (Character.isAlphabetic(c)) {
char startLetter = Character.isUpperCase(c) ? 'A' : 'a'; // convert [a, z] to [0, 25]
strOut += (char) ((c - startLetter + delta) % 26 + startLetter);
} else {
strOut += c;
}
}
return strOut;
}
public static String decode(String strIn, int delta) {
return incode(strIn, -delta);
}function caesarCipher(str, shift) {
const alphabetArr = "abcdefghijklmnopqrstuvwxyz".split("");
let res = "";
for (let i = 0; i < str.length; i++) {
const char = str[i];
const idx = alphabetArr.indexOf(char);
// if it is not a letter, don`t shift it
if (idx === -1) {
res += char;
continue;
}
// only 26 letters in alphabet, if > 26 it wraps around
const encodedIdx = (idx + shift) % 26;
res += alphabetArr[encodedIdx];
}
return res;
}def caesar_encrypt():
word = input('Enter the plain text: ')
c = ''
for i in word:
if (i == ' '):
c += ' '
else:
c += (chr(ord(i) + 3))
return c
def caesar_decrypt():
word = input('Enter the cipher text: ')
c = ''
for i in word:
if (i == ' '):
c += ' '
else:
c += (chr(ord(i) - 3))
return c
plain = 'hello'
cipher = caesar_encrypt(plain)
decipher = caesar_decrypt(cipher)