JavaScript implementation of Virginia of Vigenere cryptographic algorithm example

  • 2020-03-29 23:58:53
  • OfStack

Traditional encryption technologies do not play a large role in today's network security, but they are introduced at the beginning of every book on cryptography, because they are the foundation of cryptography, the history of cryptography. Almost every cryptography book in the chapter on Vigenere's password has a Vigenere substitution table user explaining Vigenere's password mechanism:

< img SRC = "border = 0 / / files.jb51.net/file_images/article/201311/20131122103155.jpg? 2013102210340 ">

The encryption process is simple: given the key letter x and the plaintext letter y, the ciphertext letter is the letter in row x and column y. This determines that encrypting a message requires a key string of the same length as the message; typically, the key string is a repeat of the key word.
Take "cryptography and network security - principles and practices" as an example of this article. For example, the key word is appearance and the message is "we are discovered save yourself". The encryption process is as follows:


deceptivedeceptivedeceptive (key string) 
wearediscoveredsaveyourself (message) 
ZICVTWQNGRZGVTWAVZHCQYGLMGJ (ciphertext) 

Where did the first letter "Z" in the ciphertext come from? From the Vigenere substitution table, row "d" in the key string, and the letter "w" in the message column is "Z".

It is easy to summarize the rule by encrypting A~Z several times by looking up A table: number A~Z as 0~25, and the encryption process is to find the message letter in the first row of the substitution table, such as "w", and then move it back d (that is, 3) times, the resulting letter is the ciphertext. If the last digit is counted, the next shift continues from scratch (A). That is to say, A~Z can be regarded as A ring, the encryption process is to find the message letter, the pointer to A specific direction of the ring, the number of times is the key letter represented. This is actually a module 26 process.
By extension, the above encryption only encrypts 26 letters and is case-insensitive. But in English, there are punctuation marks and Spaces in addition to letters. If most English characters are considered, the Vigenere substitution table is large and a bit of a waste of space. If it is assumed that there are N characters that can be encrypted, if the N characters are built into a ring, then the encryption process is the process of modular N, that is, C(I)=(K(I)+P(I))modN, where K, C, P respectively represent the key space, ciphertext space, message (plaintext) space.
Someone on the network implemented this encryption algorithm with C, almost always using the method of lookup and substitution table. Although substitution tables can be generated programmatically, the generated substitution tables are too regular. I implemented this once in Javascript, using a modular approach that feels more flexible and definitely takes up less space (time efficiency not yet estimated)


var Vigenere = {
    _strCpr: 'abcdefghijklmnopqrstuvwxyz_12345 67890.ABCDEFGHIJKLMNOPQRSTUVWXYZ',//You can rearrange the order of the points in this string, or add more characters
    _strKey: function(strK,str){//Generates the key string,strK for the key, and STR for the plaintext or ciphertext
        var lenStrK = strK.length;
        var lenStr = str.length;
        if(lenStrK != lenStr){//If the key length is different from STR, you need to generate the key string
            if(lenStrK < lenStr){//If the key length is shorter than STR, the key string is generated by repeating the key repeatedly
                while(lenStrK < lenStr){
                    strK = strK + strK;
                    lenStrK = 2 * lenStrK;
                }
            }//At this point, the length of the key string is greater than or equal to the STR length
            strK = strK.substring(0,lenStr);//Intercepts the key string to the length of STR
        }
        return strK;
    }
}
Vigenere.lenCpr = Vigenere._strCpr.length;
Vigenere.Encrypt = function(K,P){//Encryption algorithm, K for the key, P for the plaintext
    K = Vigenere._strKey(K,P);
    var lenK = K.length;
    var rlt = '';
    var loop = 0;
    for(loop=0; loop<lenK; loop++){
        var iP = Vigenere._strCpr.indexOf(P.charAt(loop));
        if(iP==-1) return ' The algorithm is temporarily unable to pair characters: ' + P.charAt(loop) + ' encrypted ';
        var iK = Vigenere._strCpr.indexOf(K.charAt(loop));
        if(iK==-1) return ' The key contains illegal characters: ' + K.charAt(loop);
        var i = (iP + iK) % Vigenere.lenCpr;
        rlt = rlt + Vigenere._strCpr.charAt(i);
    }
    return rlt;    
};
Vigenere.DisEncrypt = function(K,C){
    K = Vigenere._strKey(K,C);
    var lenK = K.length;
    var rlt = '';
    var loop = 0;
    for(loop=0; loop<lenK; loop++){
        var iK = Vigenere._strCpr.indexOf(K.charAt(loop));
        if(iK==-1) return ' The key contains illegal characters: ' + K.charAt(loop);        
        var iC = Vigenere._strCpr.indexOf(C.charAt(loop));
        if(iK > iC){
            rlt += Vigenere._strCpr.charAt(iC + Vigenere.lenCpr - iK);
        }
        else{
            rlt += Vigenere._strCpr.charAt(iC - iK);
        }
    }
    return rlt;
};


Related articles: