Skip to content

Instantly share code, notes, and snippets.

@0guzhan
Last active August 29, 2015 14:28
Show Gist options
  • Save 0guzhan/1e98b49e919ce323d46f to your computer and use it in GitHub Desktop.
Save 0guzhan/1e98b49e919ce323d46f to your computer and use it in GitHub Desktop.
Basically Seperate CipherText into n character string array. it actually gives no idea about ciphertext but helps me to visualize what all this bunch of characters have.
import java.util.Arrays;
public class CipherTextTools {
public static void main(String[] args) {
String[] seperated = seperateCipher("igxcshavc4eslykamah71j8o0es7myk1", 8);
System.out.println(Arrays.toString(seperated));
}
private static String[] seperateCipher(String cipher, int n) {
int length = cipher.length();
if (length % n == 0) {
String[] seperatedCipher = new String[length / n];
char[] characters = cipher.toCharArray();
char[] cipherPart = new char[n];
for (int i = 0; i < length; i++) {
cipherPart[i % n] = characters[i];
if (i % n == n-1) {
seperatedCipher[i / n] = new String(cipherPart);
}
}
return seperatedCipher;
}
else {
int remainder = length % n;
String[] seperatedCipher = new String[length / n + 1];
char[] characters = cipher.toCharArray();
char[] cipherPart = new char[n];
for (int i = 0; i < length - remainder; i++) {
cipherPart[i % n] = characters[i];
if (i % n == n-1) {
seperatedCipher[i / n] = new String(cipherPart);
}
}
cipherPart = new char[remainder];
for (int i = 0; i + length - remainder < length; i++) {
cipherPart[i] = characters[i + length - remainder];
}
seperatedCipher[length / n] = new String(cipherPart);
return seperatedCipher;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment