Example of Vigenere cryptographic algorithm implemented by JavaScript

Source: Internet
Author: User
Tags key string

Traditional encryption technology does not play a major role in today's network security, but every book that talks about cryptography first introduces them, because they are the foundation of cryptography and the history of cryptography. Almost every cryptography book describes the Vigenere password. The user explains the Vigenere password mechanism in "Vigenere proxy table:

The encryption process is very simple. Given the key letters x and y, the ciphertext letter is the letter located in row x and column y. This determines that a message needs to be encrypted with a key string of the same length as the message. Generally, the key string is a duplicate of the key word.
This article uses the example in "cryptographic and network security-Principles and Practices. For example, if the key word is deceptive and the message is "we are discovered save yourself", the encryption process is as follows:

Copy codeThe Code is as follows:
Deceptivedeceptivedeceptive (key string)
Wearediscoveredsaveyourself (Message)
ZICVTWQNGRZGVTWAVZHCQYGLMGJ (ciphertext)

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

You can easily sum up the rule by encrypting the table several times ~ Z to 0 ~ 25, the encryption process is to find the message letter in the first line of the replacement table, such as "w", and then move d (that is, 3) backward, the resulting letter is ciphertext. If the last position is reached, the next shift will start from the beginning (that is,. That is to say, ~ Z is regarded as a ring. The encryption process is to shift the pointer to a specific direction of the ring after the message letter is found. The number of times is the number represented by the key letter. This is actually a process of MOD 26.
Extended, the above encryption can only encrypt 26 letters, and cannot be case sensitive. In fact, in addition to letters, there are punctuation marks and spaces in English. If most of the English characters are considered, Vigenere replacement tables will be large and may be a waste of space. Assume that there are N encrypted characters. If the N characters are built into a ring, the encryption process is a process of N, namely, C (I) = (K (I) + P (I) modN. K, C, and P represent the key space, ciphertext space, and message (plaintext) space.
Some people use C to implement this encryption algorithm on the network, almost all of which use the query and replacement methods. Although a program can generate a replacement table, the generated replacement table is too regular. I have implemented it once in Javascript. I am using a modulo method. I feel more flexible and the occupied space is definitely smaller (I have not estimated the time efficiency)

Copy codeThe Code is as follows:
Var Vigenere = {
_ StrCpr: 'abcdefghijklmnopqrstuvwxyz _ 12345 67890. abcdefghijklmnopqrstuvwxyz ', // You can disrupt the order of the string or add more characters
_ StrKey: function (strK, str) {// generate the key string. strK is the key and str is the plaintext or ciphertext.
Var lenStrK = strK. length;
Var lenStr = str. length;
If (lenStrK! = LenStr) {// if the key length is different from that of str, a key string must be generated.
If (lenStrK <lenStr) {// if the key length is shorter than str, the key string is generated by repeatedly repeating the key.
While (lenStrK <lenStr ){
StrK = strK + strK;
LenStrK = 2 * lenStrK;
}
} // At this time, the length of the key string is greater than or equal to the str Length
StrK = strK. substring (0, lenStr); // truncate the key string into a string of the same length as str.
}
Return strK;
}
}

Vigenere. lenCpr = Vigenere. _ strCpr. length;

Vigenere. Encrypt = function (K, P) {// encryption algorithm. K is the key and P is 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 'This algorithm currently cannot encrypt characters:' + P. charAt (loop) + ';
Var iK = Vigenere. _ strCpr. indexOf (K. charAt (loop ));
If (iK =-1) return 'key contains invalid 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 'key contains invalid 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.