Where can I find the Java source code for the Vigenere password?
•
Java
In my application, I want to implement some encryption So I need the code for the Vigenere password Who knows where I can find the Java source code?
Solution
This is the Vigenere password class. You can use it by calling the encryption and decryption functions:
public class VigenereCipher {
public static void main(String[] args) {
String key = "VIGENERECIPHER";
String ori = "Beware the Jabberwock,my son! The jaws that bite,the claws that catch!";
String enc = encrypt(ori,key);
System.out.println(enc);
System.out.println(decrypt(enc,key));
}
static String encrypt(String text,final String key) {
String res = "";
text = text.toUpperCase();
for (int i = 0,j = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c < 'A' || c > 'Z') continue;
res += (char)((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
j = ++j % key.length();
}
return res;
}
static String decrypt(String text,j = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c < 'A' || c > 'Z') continue;
res += (char)((c - key.charAt(j) + 26) % 26 + 'A');
j = ++j % key.length();
}
return res;
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
