public class Kata {
public static String encrypt(final String text, int n) {
if (n <= 0 || text == null || text.isEmpty()) {
return text;
}
StringBuilder firstPart = new StringBuilder();
StringBuilder secondPart = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
char aChar = text.charAt(i);
if (i % 2 == 1) {
firstPart.append(aChar);
} else {
secondPart.append(aChar);
}
}
return encrypt(firstPart.append(secondPart).toString(), --n);
}
public static String decrypt(final String encryptedText, int n) {
if (n <= 0 || encryptedText == null || encryptedText.isEmpty()) {
return encryptedText;
}
StringBuilder text = new StringBuilder();
final int half = encryptedText.length() / 2;
for (int i = 0; i < half; i++) {
text.append(encryptedText.charAt(half + i)).append(
encryptedText.charAt(i));
}
if (encryptedText.length() % 2 == 1) {
text.append(encryptedText.charAt(encryptedText.length() - 1));
}
return decrypt(text.toString(), --n);
}