Last active
August 29, 2015 14:28
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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