C
chrysaetos
Gast
Hallo zusammen,
ich habe versucht, ein kleines Programm zu schreiben, welches das kryptographische Verfahren der Fleissner-Schablone simuliert.
Hier mein Code:
Das Problem ist, dass das Programm in einer Art Endlos-Schleife hängenbleibt, obwohl doch die Zählvariablen erhöht werden, oder?
Über Hilfe jeglicher Art würde ich mich freuen.
Viele Grüße
chrysaetos
ich habe versucht, ein kleines Programm zu schreiben, welches das kryptographische Verfahren der Fleissner-Schablone simuliert.
Hier mein Code:
Java:
import java.util.Scanner;
public class fleissner {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Scanner scanner2 = new Scanner(System.in);
System.out.println("Encrypt (1) or decrypt (2)?");
int d = scanner.nextInt();
System.out.println("Enter text without spaces:");
System.out.println("In case it is encrypted enter it from left to right and then from up to down:");
String text = scanner2.nextLine();
if(text.length() != 36){
System.exit(0);
}
if (d == 1) {
char[][] out = encrypt(text);
for (int zeile = 0; zeile < out.length; zeile++) {
for (int spalte = 0; spalte < out[zeile].length; spalte++) {
System.out.print(out[zeile][spalte] + " ");
}
System.out.println();
}
}
if (d == 2) {
char[][] out = decrypt(text);
for (int zeile = 0; zeile < out.length; zeile++) {
for (int spalte = 0; spalte < out[zeile].length; spalte++) {
System.out.print(out[zeile][spalte] + " ");
}
System.out.println();
}
}
}
//Dient dazu die Schablone um 45 Grad nach rechts zu drehen.
public static char[][] rotate(char[][] a1) {
char[][] rotated =
{{a1[5][0],a1[4][0],a1[3][0],a1[2][0],a1[1][0],a1[0][0]},
{a1[5][1],a1[4][1],a1[3][1],a1[2][1],a1[1][1],a1[0][1]},
{a1[5][2],a1[4][2],a1[3][2],a1[2][2],a1[1][2],a1[0][2]},
{a1[5][3],a1[4][3],a1[3][3],a1[2][3],a1[1][3],a1[0][3]},
{a1[5][4],a1[4][4],a1[3][4],a1[2][4],a1[1][4],a1[0][4]},
{a1[5][5],a1[4][5],a1[3][5],a1[2][5],a1[1][5],a1[0][5]}
};
return rotated;
}
public static char[][] encrypt(String text) {
//Die Schablone
char[][] a1 = {{'A', 'X', 'A', 'X', 'A', 'X'},
{'A', 'A', 'A', 'A', 'X', 'A'},
{'A', 'A', 'X', 'A', 'A', 'A'},
{'A', 'X', 'A', 'A', 'X', 'A'},
{'A', 'A', 'A', 'A', 'A', 'X'},
{'A', 'A', 'A', 'X', 'A', 'A'}};
String text1 = substring(text, 0, 9);
int[] ar = {0,9,18,27};
int i = 0;
while(i<4) {
int a = 0;
int b = 0;
int x = 0;
while (x < text1.length()) {
if(a1[a][b]=='X'){
a1[a][b] = text1.charAt(x);
x=x+1;
}
else if (b<5){
b=b+1;
}
else if(a<5){
a=a+1;
b=0;
}
}
int m = ar[i];
int n = m+9;
text1 = substring(text, m, n);
a1 = rotate(a1);
i = i+1;
}
return a1;
}
public static char[][] decrypt(String text) {
char[][] out = {
{text.charAt(1), text.charAt(3), text.charAt(5), text.charAt(10), text.charAt(14), text.charAt(19)},
{text.charAt(22), text.charAt(29), text.charAt(33), text.charAt(8), text.charAt(11), text.charAt(15)},
{text.charAt(18), text.charAt(23), text.charAt(26), text.charAt(28), text.charAt(31), text.charAt(35)},
{text.charAt(2), text.charAt(6), text.charAt(13), text.charAt(16), text.charAt(21), text.charAt(25)},
{text.charAt(30), text.charAt(32), text.charAt(34), text.charAt(0), text.charAt(4), text.charAt(7)},
{text.charAt(9), text.charAt(12), text.charAt(17), text.charAt(20), text.charAt(24), text.charAt(27)}
};
return out;
}
public static String substring(String str, int start, int end) {
String out = "";
if (start > end) {
return out;
}
if (start < 0) {
start = 0;
}
if (end > str.length() - 1) {
end = str.length();
}
while (start < end) {
out = out + str.charAt(start);
start = start + 1;
}
return out;
}
}
Das Problem ist, dass das Programm in einer Art Endlos-Schleife hängenbleibt, obwohl doch die Zählvariablen erhöht werden, oder?
Über Hilfe jeglicher Art würde ich mich freuen.
Viele Grüße
chrysaetos