Pages

Monday, December 27, 2010

Simple Substitution Cipher

/**
 * Function that provide substitution encryption and decryption
 * algorithm.
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 28, 2010
 * @version : 1.0
 */


/**
 * @param string text either plaintext or encrypted text
 * /
function SubstitutionCipher(text){
    this.text = text;
}


/**
 * @return encrypted string
 */
SubstitutionCipher.prototype.encrypt = function(){

    var encrypted = this.text.replace(/a/g,'1');
    encrypted = encrypted.replace(/e/g,'2');
    encrypted = encrypted.replace(/i/g,'3');
    encrypted = encrypted.replace(/o/g,'4');
    encrypted = encrypted.replace(/u/g,'5');
    encrypted = encrypted.replace(/ /g,'6');

    return encrypted;
};


/**
 * @return plaintext 
 */
SubstitutionCipher.prototype.decrypt = function(){

    var decrypted = this.text.replace(/1/g,'a');
    decrypted = decrypted.replace(/2/g,'e');
    decrypted = decrypted.replace(/3/g,'i');
    decrypted = decrypted.replace(/4/g,'o');
    decrypted = decrypted.replace(/5/g,'u');
    decrypted = decrypted.replace(/6/g,' ');

    return decrypted;
};
      

No comments:

Post a Comment