Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added vigenere cipher #2325

Merged
merged 1 commit into from
Oct 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions js/VigenereCipher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<script>

function getKey(str,key)
{

key=key.split("");
if(str.length == key.length)
return key.join("");
else
{
let temp=key.length;
for (let i = 0;i<(str.length-temp) ; i++)
{

key.push(key[i % ((key).length)])
}
}
return key.join("");
}


function encrypt(str,key)
{
let cipher_text="";

for (let i = 0; i < str.length; i++)
{
let x = (str[i].charCodeAt(0) + key[i].charCodeAt(0)) %26;

x += 'A'.charCodeAt(0);

cipher_text+=String.fromCharCode(x);
}
return cipher_text;
}

function dcrypt(cipher_text,key)
{
let orig_text="";

for (let i = 0 ; i < cipher_text.length ; i++)
{
let z = (cipher_text[i].charCodeAt(0) -
key[i].charCodeAt(0) + 26) %26;

z += 'A'.charCodeAt(0);
orig_text+=String.fromCharCode(z);
}
return orig_text;
}


function LowToUpCase(s)
{
let str =(s).split("");
for(let i = 0; i < s.length; i++)
{
if(s[i] == s[i].toLowerCase())
{
str[i] = s[i].toUpperCase();
}
}
s = str.toString();
return s;
}

let str = "hello world";
let keyword = "tesla";


let key = getKey(str, keyword);

let cipher_text = encrypt(str, key);

document.write("Encrypted message : "
+ cipher_text);

document.write("Decrypted message : "
+ decrypt(cipher_text, key));


</script>