Copy codeCode: 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;
};