all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
33
Task/Vigen-re-cipher/Java/vigen-re-cipher.java
Normal file
33
Task/Vigen-re-cipher/Java/vigen-re-cipher.java
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
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, 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) + 26) % 26 + 'A');
|
||||
j = ++j % key.length();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue