โค้ดโปรแกรม
// encryption and decryption
// caesar cipher
const encrypt = (t,s) => {
const ch = t.split("")
.map((_,i)=>{
const c = t.charCodeAt(i)-97
if(c >=0 && c <= t.length){
return ((c+s)%26)+97
}else{
return c+s+97
}
})
return String.fromCharCode(...ch)
}
const decrypt = (cp,s) => {
const ch = cp.split("")
.map((_,i)=>{
const c = cp.charCodeAt(i)-97
if(c >=0 && c <= cp.length){
return ((c-s)%26)+97
}else{
return c-s+97
}
})
return String.fromCharCode(...ch)
}
const text="Hello WorLD"
const shift=-10
const en = encrypt(text,shift)
console.log(en)
const de = decrypt(en,shift)
console.log(de)
Post Views: 957