The code _javascript technique of JS Virginia cipher algorithm

Source: Internet
Author: User
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;
};

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.