JavaScript implements Virginia (Vigenere) cryptographic algorithm Instance _javascript skill

Source: Internet
Author: User
Tags key string

Traditional cryptography does not play a big role in today's network security, but every book that tells cryptography is the first to introduce them, because they are the basis of cryptography, the history of cryptography. Almost every cipher book in the chapter on Vigenere password will have such a "vigenere substitution table" users explain the vigenere password mechanism:

The encryption process is simple, given the key letter x and the plaintext letter Y, which is the letter in line x and column Y. This determines that encrypting a message requires a key string as long as the message, usually the key string is a duplicate of the key word.
Take the example of cryptography and network security-principle and practice as examples in this paper. For example, the key word is deceptive, the message is "We are discovered save yourself", then the encryption process is as follows:

Copy Code code as follows:

Deceptivedeceptivedeceptive (Key string)
Wearediscoveredsaveyourself (News)
ZICVTWQNGRZGVTWAVZHCQYGLMGJ (ciphertext)

How did you get the first letter "Z" in the ciphertext? From the vigenere substitution table, the "D" in the key string is the line, and the letter "W" in the message is the "Z" of the column.

Using the Look-up table method to encrypt several times can easily sum up the rules: The a~z is numbered with 0~25, so the encryption process is to find the letter of the message in the first row of the substitution table, such as "W", and then move the D (ie 3) times backwards, and the resulting letter is the cipher text. If you count to the bottom, the next shift will continue from the beginning (i.e. a). In other words, you can see a~z 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 by the number. This is actually a modulo 26 process.
Extended, the above encryption can only encrypt 26 letters, and can not be case-sensitive. But in fact, in addition to the letter in English, there are punctuation, there are spaces. If you take into account most English characters, then the vigenere substitution table will be larger, and a bit of a waste of space suspicion. If you assume that the characters that can be encrypted have n, and if you build these n characters into a loop, then the encryption process is the process of modulo n, that is, C (i) = (K (i) +p (i)) Modn, where K, C and P represent the key space, the ciphertext space, the message (clear text) space respectively.
Some people on the network implemented this encryption algorithm with C, almost all of them are using the method of searching the substitution table. Although the substitution table can be generated by the program, the resulting substitution table is too regular. I've used JavaScript to do this once, using a modular approach that feels more flexible and takes up less space (time efficiency has not been estimated).

Copy Code code as follows:

var vigenere = {
_STRCPR: ' abcdefghijklmnopqrstuvwxyz_12345 67890.ABCDEFGHIJKLMNOPQRSTUVWXYZ ',//You can shuffle the order of this string, or add more characters
_strkey:function (STRK,STR) {//Generate key string, STRK as key, str as plaintext or ciphertext
var lenstrk = strk.length;
var lenstr = str.length;
if (lenstrk!= lenstr) {//If the key length is different from Str, the key string needs to be generated
if (LENSTRK < LENSTR) {//If the key length is shorter than STR, the key string is generated in the form of a constant duplicate key
while (LENSTRK < LENSTR) {
STRK = strk + strk;
LENSTRK = 2 * LENSTRK;
}
}//at this point, the key string is longer than or equal to the str length
STRK = strk.substring (0,LENSTR);//The key string is truncated to a string that is as long as Str
}
return STRK;
}
}

VIGENERE.LENCPR = Vigenere._strcpr.length;

Vigenere.encrypt = function (k,p) {//cryptographic algorithm, K is key, P is 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 ' This algorithm cannot temporarily encrypt the character: ' + P.charat (loop) + ';
var IK = Vigenere._strcpr.indexof (K.charat (loop));
The IF (ik==-1) return ' 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));
The IF (ik==-1) return ' 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 Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.