Code for implementing the JS Virginia cryptographic algorithm

Source: Internet
Author: User
Tags key string

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

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.