|
Well, I got a stupid letter from a university, anyways they explained a cipher on it. So... I decided to make a script. Here it is: String.prototype.Vigenere = function(key, side) { str = this.toLowerCase(); key = key.toLowerCase(); var alph = 'abcdefghijklmnopqrstuvwxyz'; var enc = ''; var k = 0; var temp = 0; for(var s = 0; s < str.length; s++) { if(alph.indexOf(str.charAt(s)) != -1) { if(side) { temp = alph.indexOf(str.charAt(s)) - alph.indexOf(key.charAt(k)); if(temp < 0) temp += 26; } else { temp = alph.indexOf(str.charAt(s)) + alph.indexOf(key.charAt(k)); if(temp > 25) temp -= 26; } enc += alph.charAt(temp); if(k < key.length - 1) k++; else k = 0; } else { enc += str.charAt(s); } } return enc; }Ex: var testStr = 'test message'; document.write(testStr.viginere("foo", 0)); // Encrypt key: What you use to encrypt, must be the same to encrypt and decrypt side: As in which side of the encryption process (enc or dec). Anyways, use a true or false/1 or 0 value. 0 is encrypt, 1 is decrypt. Have fun!
Last Edit: Nov 15, 2005 3:10:43 GMT by Eric
|
|
|
|
Heh, fun. Makes my head hurt, but its fun. What happens if you have other characters?
|
|
|
|
|
Summary of a part: if(alph.indexOf(str.charAt(s)) != -1) { // ... } else { enc += str.charAt(s); }
Last Edit: Nov 15, 2005 2:29:18 GMT by Eric
|
|
|
|
Ah right, missed that.
|
|
|
|
|
What's a Viginere Cipher?
|
|
|
|
What's a Viginere Cipher? Bleh, I mispelled it, its Vigenere. Anyways, its where you shift letters based upon their value. Sort of like XOR, but not using binary. Ex: Z = 25 Z = 25 25 + 25 = 50 50 - 26 = 24 24 = Y Then to decrypt: Y = 24 Z = 25 24 - 25 = -1 -1 + 26 = 25 25 = Z
Last Edit: Nov 15, 2005 3:12:14 GMT by Eric
|
|
|