Among the simplest (and among the oldest) methods for encryption is the Caesar cipher:
if a letter in the plaintext is the Nth letter in the alphabet,replace it by the (N + K)th letter in the alphabet, where K is some fixed integer (Caesar used K = 3). For example, the table below shows how a message is encrypted using this method with K = 1:
Plaintext: ATTACK AT DAWN
Ciphertext:BUUBDLABUAEB X 0
( based on a 27 letter alphabet 1st char = space )
I am trying to code what they call the "Vigenere cipher", and I am sure it has been done a hundred times before in programming, but I want to do it myself.
It works similar to Caesar's method, but like this:
a small repeated key is used to determine the value of K for each letter. At each step, the key letter index is added to the plaintext letter index to determine the ciphertext letter index. Our sample plaintext,with the key ABC, is encrypted as follows:
Key:ABCABCABCABCAB
Plaintext:ATTACK AT DAWN
Ciphertext:BVWBENACWAFDX P
For example, the last letter of the ciphertext is P, the 16th letter of the alphabet, because the corresponding plaintext letter is N (the 14th letter),and the corresponding key letter is B (the 2nd letter).
Simple enough for our human brains, but JAVASCRIPT... yikes.
Here is what I have come up with, and for some reason it is not working - and I have no clue as to why.
Can anyone help??
<script type="text/javascript">
function convert(to, theVal){
if(to.upperCase() == 'char'){
return alphabet.charAt(theVal);
} else {
return alphabet.indexOf(theVal);
}
}
var str = "JOHN";
var key = "ABC";
var encryptedChars = new Array();
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var currKey = 0;
for(var i=0; i<key.length; i++){
keyChar = convert('Number', key.charAt(i));
}
for (var x=0; x<str.length; x++){
encryptedChars[x] = convert('Number', str.charAt(x) + keyChar[currentKey]);
if(currentKey<=keyChar.length){
currKey++;
} else {
currKey = 0;
}
}
alert(str + " encrypted with key: " + key + " is: " + encryptedChars.toString() );
</script>
if a letter in the plaintext is the Nth letter in the alphabet,replace it by the (N + K)th letter in the alphabet, where K is some fixed integer (Caesar used K = 3). For example, the table below shows how a message is encrypted using this method with K = 1:
Plaintext: ATTACK AT DAWN
Ciphertext:BUUBDLABUAEB X 0
( based on a 27 letter alphabet 1st char = space )
I am trying to code what they call the "Vigenere cipher", and I am sure it has been done a hundred times before in programming, but I want to do it myself.
It works similar to Caesar's method, but like this:
a small repeated key is used to determine the value of K for each letter. At each step, the key letter index is added to the plaintext letter index to determine the ciphertext letter index. Our sample plaintext,with the key ABC, is encrypted as follows:
Key:ABCABCABCABCAB
Plaintext:ATTACK AT DAWN
Ciphertext:BVWBENACWAFDX P
For example, the last letter of the ciphertext is P, the 16th letter of the alphabet, because the corresponding plaintext letter is N (the 14th letter),and the corresponding key letter is B (the 2nd letter).
Simple enough for our human brains, but JAVASCRIPT... yikes.
Here is what I have come up with, and for some reason it is not working - and I have no clue as to why.
Can anyone help??
<script type="text/javascript">
function convert(to, theVal){
if(to.upperCase() == 'char'){
return alphabet.charAt(theVal);
} else {
return alphabet.indexOf(theVal);
}
}
var str = "JOHN";
var key = "ABC";
var encryptedChars = new Array();
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var currKey = 0;
for(var i=0; i<key.length; i++){
keyChar = convert('Number', key.charAt(i));
}
for (var x=0; x<str.length; x++){
encryptedChars[x] = convert('Number', str.charAt(x) + keyChar[currentKey]);
if(currentKey<=keyChar.length){
currKey++;
} else {
currKey = 0;
}
}
alert(str + " encrypted with key: " + key + " is: " + encryptedChars.toString() );
</script>