Ad
Strings
Ciphers
Fundamentals
Cryptography
Algorithms
Code
Diff
  • import java.util.List;
    import java.util.stream.Collectors;
    
    public class Kata {
    	
    	// <<-CONSTANTS->>
    	private static final int UpperCase_LOWER_LIMIT = 65;
    	private static final int UpperCase_UPPER_LIMIT = 90;
    	private static final int LowerCase_LOWER_LIMIT = 97;
    	private static final int LowerCase_UPPER_LIMIT = 122;
    	private static final int INTERMEDIATE_OFFSET   = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1;
    
    	// <<-METHODS->>
    	private static int getLastDigit(final int digit) {
        String str = String.valueOf(digit);
        str = str.substring(str.length() - 1, str.length());
    		return Integer.valueOf(str);
    	}
    	
    	private static List<Integer> getEncryptationOffsets(String key) {
    		return key.chars()
    				.map(n -> getLastDigit(n) + key.length())
    				.boxed()
    				.toList();
    	}
    	
    	private static List<Integer> getPhraseChars(String phrase) {
    		return phrase.chars()
    				.boxed()
    				.collect(Collectors.toList());
    	}
    	
    	private static String parseChars(List<Integer> translation) {
    		return translation.stream()
    				.map(n -> (char) n.intValue())
    				.map(c -> Character.toString(c))
    				.collect(Collectors.joining());
    	}
    
    	public static String encode(String phrase, String key) {
    		if (phrase == null || phrase.isEmpty() || key == null) return "";
    		if (key.isEmpty()) return phrase;
        
    		List<Integer> offsets     = getEncryptationOffsets(key);
    		List<Integer> phraseChars = getPhraseChars(phrase);
    		List<Integer> translation  = new java.util.ArrayList<>();
    
    		int keyIndex = 0;
    		for (int i = 0; i < phrase.length(); i++) {
    			keyIndex = i % offsets.size();
    			
    			int current = phraseChars.get(i);
    			int result  = current + offsets.get(keyIndex);
    			if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) {
    				int offset = result - UpperCase_UPPER_LIMIT;
    				result = LowerCase_LOWER_LIMIT + offset - 1;
    			}
    			
    			if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) {
    				result += INTERMEDIATE_OFFSET;
    			} else if (result > LowerCase_UPPER_LIMIT) {
    				result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1;
    			}
    			translation.add(result);
    			keyIndex++;
    		}
    
    		return parseChars(translation);
    	}
    
    	public static String decode(String phrase, String key) {
    		if (phrase == null || phrase.isEmpty() || key == null) return "";
    		if (key.isEmpty()) return phrase;
        
    		List<Integer> offsets     = getEncryptationOffsets(key);
    		List<Integer> phraseChars = getPhraseChars(phrase);
    		List<Integer> translation  = new java.util.ArrayList<>();
    
    		int keyIndex = 0;
    		for (int i = 0; i < phrase.length(); i++) {
    			keyIndex = i % offsets.size();
    			
    			int current = phraseChars.get(i);
    			int result  = current - offsets.get(keyIndex);
    			if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) {
    				int offset = LowerCase_LOWER_LIMIT - result;
    				result = UpperCase_UPPER_LIMIT - offset + 1;
    			}
    			if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) {
    				result -= INTERMEDIATE_OFFSET;
    			} else if (result < UpperCase_LOWER_LIMIT) {
    				int offset = UpperCase_LOWER_LIMIT - result;
    				result = LowerCase_UPPER_LIMIT - offset + 1;
    			}
    			translation.add(result);
    			keyIndex++;
    		}
    
    		return parseChars(translation);
    	}
      
    }
    • import java.util.List;
    • import java.util.stream.Collectors;
    • public class Kata {
    • // <<-CONSTANTS->>
    • private static final int UpperCase_LOWER_LIMIT = 65;
    • private static final int UpperCase_UPPER_LIMIT = 90;
    • private static final int LowerCase_LOWER_LIMIT = 97;
    • private static final int LowerCase_UPPER_LIMIT = 122;
    • private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1;
    • // <<-METHODS->>
    • private static int getLastDigit(final int digit) {
    • String str = String.valueOf(digit);
    • str = str.substring(str.length() - 1, str.length());
    • return Integer.valueOf(str);
    • }
    • private static List<Integer> getEncryptationOffsets(String key) {
    • return key.chars()
    • .map(n -> getLastDigit(n) + key.length())
    • .boxed()
    • .toList();
    • }
    • private static List<Integer> getPhraseChars(String phrase) {
    • return phrase.chars()
    • .boxed()
    • .collect(Collectors.toList());
    • }
    • private static String parseChars(List<Integer> traduction) {
    • return traduction.stream()
    • private static String parseChars(List<Integer> translation) {
    • return translation.stream()
    • .map(n -> (char) n.intValue())
    • .map(c -> Character.toString(c))
    • .collect(Collectors.joining());
    • }
    • public static String encode(String phrase, String key) {
    • if (phrase == null || phrase.isEmpty() || key == null) return "";
    • if (key.isEmpty()) return phrase;
    • List<Integer> offsets = getEncryptationOffsets(key);
    • List<Integer> phraseChars = getPhraseChars(phrase);
    • List<Integer> traduction = new java.util.ArrayList<>();
    • List<Integer> translation = new java.util.ArrayList<>();
    • int keyIndex = 0;
    • for (int i = 0; i < phrase.length(); i++) {
    • keyIndex = i % offsets.size();
    • int current = phraseChars.get(i);
    • int result = current + offsets.get(keyIndex);
    • if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) {
    • int offset = result - UpperCase_UPPER_LIMIT;
    • result = LowerCase_LOWER_LIMIT + offset - 1;
    • }
    • if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) {
    • result += INTERMEDIATE_OFFSET;
    • } else if (result > LowerCase_UPPER_LIMIT) {
    • result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1;
    • }
    • traduction.add(result);
    • translation.add(result);
    • keyIndex++;
    • }
    • return parseChars(traduction);
    • return parseChars(translation);
    • }
    • public static String decode(String phrase, String key) {
    • if (phrase == null || phrase.isEmpty() || key == null) return "";
    • if (key.isEmpty()) return phrase;
    • List<Integer> offsets = getEncryptationOffsets(key);
    • List<Integer> phraseChars = getPhraseChars(phrase);
    • List<Integer> traduction = new java.util.ArrayList<>();
    • List<Integer> translation = new java.util.ArrayList<>();
    • int keyIndex = 0;
    • for (int i = 0; i < phrase.length(); i++) {
    • keyIndex = i % offsets.size();
    • int current = phraseChars.get(i);
    • int result = current - offsets.get(keyIndex);
    • if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) {
    • int offset = LowerCase_LOWER_LIMIT - result;
    • result = UpperCase_UPPER_LIMIT - offset + 1;
    • }
    • if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) {
    • result -= INTERMEDIATE_OFFSET;
    • } else if (result < UpperCase_LOWER_LIMIT) {
    • int offset = UpperCase_LOWER_LIMIT - result;
    • result = LowerCase_UPPER_LIMIT - offset + 1;
    • }
    • traduction.add(result);
    • translation.add(result);
    • keyIndex++;
    • }
    • return parseChars(traduction);
    • return parseChars(translation);
    • }
    • }
Strings
Ciphers
Fundamentals
Cryptography
Algorithms