From e48a9ad4f223e83bba24d3ad92ee9a25b459f92d Mon Sep 17 00:00:00 2001 From: straiffix Date: Sat, 14 Apr 2018 01:39:08 +0200 Subject: [PATCH] Add files via upload --- ciphers/Vigenere.java | 63 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 ciphers/Vigenere.java diff --git a/ciphers/Vigenere.java b/ciphers/Vigenere.java new file mode 100644 index 00000000..c155187d --- /dev/null +++ b/ciphers/Vigenere.java @@ -0,0 +1,63 @@ +package ciphers; + +/** + * A Java implementation of Vigenere Cipher. + * @author straiffix + */ + + +public class Vigenere { + + public static String encrypt(final String message, final String key) + { + + String result = ""; + + for (int i = 0, j = 0; i < message.length(); i++) + { + char c = message.charAt(i); + if (c >= 'A' && c <= 'Z') { + result += (char) ((c + key.toUpperCase().charAt(j) - 2 * 'A') % 26 + 'A'); + j = ++j % key.length(); + } else if (c >= 'a' && c <= 'z') { + result += (char) ((c + key.toLowerCase().charAt(j) - 2 * 'a') % 26 + 'a'); + j = ++j % key.length(); + } else { + result+=c; + continue; + } + } + return result; + } + + public static String decrypt( final String message, final String key) + { + String result =""; + + for(int i = 0, j = 0; i < message.length(); i++){ + + char c = message.charAt(i); + if((c >= 'A' && c <= 'Z')){ + result += ((char)('Z'-(25-(c-key.toUpperCase().charAt(j)))%26)); + } + else if(c >= 'a' && c <= 'z'){ + result += ((char)('z'-(25-(c-key.toLowerCase().charAt(j)))%26)); + } + else{ + result+=c; + continue; + } + j = ++j % key.length(); + } + return result; + } + public static void main (String [] args){ + String text="Hello World!"; + String key="itsakey"; + System.out.println(text); + String ciphertext=encrypt(text, key); + System.out.println(ciphertext); + System.out.println(decrypt(ciphertext, key)); + + } +} \ No newline at end of file