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;
};