Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function substitutionCipher()
- {
- var str = prompt("Enter a string to encrypt","").toUpperCase().replace(/[^A-Z]*/g,'');
- var pwd = prompt("Enter you Little Orphan Annie secret password","").toUpperCase().replace(/[^A-Z]*/g,'');
- var endecrypted = '';
- var alphabet = cypherbet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
- var charBox = '';
- var i = len = 0;
- var encryptFlag = confirm('To encrypt, click OK. To decrypt click CANCEL.');
- /* strip duplicate letters from the password */
- for(i = 0, len = pwd.length; i < len; i++)
- {
- if(!charBox || charBox.indexOf(pwd.charAt(i)) < 0 )
- {
- charBox += pwd.charAt(i);
- cypherbet = cypherbet.replace(pwd.charAt(i),''); /*remove the password character from the cyphered alphabet for now*/
- }
- }
- pwd = charBox; /*replace the password with the result of stripping redundant characters from the password*/
- /*prepend the password to the cyphered alphabet*/
- cypherbet = pwd + cypherbet;
- for(i = 0, len = str.length; i < len; i++)
- {
- if(encryptFlag)
- {
- if(endecrypted && i % 5 == 0)
- {
- endecrypted += ' ';
- }
- endecrypted += cypherbet.charAt(alphabet.indexOf(str.charAt(i)));
- }else{
- endecrypted += alphabet.charAt(cypherbet.indexOf(str.charAt(i)));
- }
- }
- alert(endecrypted);
- }
- substitutionCipher();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement