Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Vigenerecipher {
- public static void main(String[] args) {
- String key = "ncl";
- String original = "newcastleuniversity";
- String encripted = encrypt(original, key);
- System.out.println(encripted);
- System.out.println(decrypt(encripted, key));
- }
- static String encrypt(String text, final String key) {
- String res = "";
- text = text.toLowerCase();
- char[] stringArray = text.toCharArray();
- for (int i = 0, j = 0; i < text.length(); i++) {
- if ((int)stringArray[i] < 97 || (int)stringArray[i] > 122) continue;
- res += (char)(((int)stringArray[i] + 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.toLowerCase();
- for (int i = 0, j = 0; i < text.length(); i++) {
- char c = text.charAt(i);
- if (c < 97 || c > 122) continue;
- res += (char)((c - key.charAt(j) + 26) % 26 + 'a');
- j = ++j % key.length();
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement