Fleissner-Schablone

C

chrysaetos

Gast
Hallo zusammen,

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
 

LimDul

Top Contributor
So aus dem Bauch heraus -

Java:
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;
                }

            }
Ist den garantiert, dass diese Schleife terminiert? Irgendwann ist doch a=b=5 und an der Stelle im Array steht kein 'X' also, terminiert die Schleife nicht. Soweit ich das durch reines ansehen sehe
 

fhoffmann

Top Contributor
Deine while-Schleife while(i<4)enthält eine weitere while-Schleifewhile(x < text1.length()).
Beim ersten Durchlauf terminiert die innere Schleife, da in a1 genau 9 mal 'X' vorkommt. Beim zweiten Durchlauf hast du diese 9 'X' aber überschrieben und die innere Schleife terminiert nicht mehr.
 
C

chrysaetos

Gast
Vielen Dank erst Mal für eure Antworten!
Ich habe jetzt das Problem mit dem Überschreiben (Beitrag 4) behoben, indem ich das Ergebnis einfach in ein neues Array schreibe:
Java:
   public static char[][] encrypt(String text) {

        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,36};
        char[][] out = new char[6][6];

        int i = 0;
        while(i<4) {
            int a = 0;
            int b = 0;
            int x = 0;

            while (x < text1.length()) {
                if(a1[a][b]=='X'){
                    out[a][b] = text1.charAt(x);
                    x=x+1;

                }
                else if (b<5){
                    b=b+1;
                }
                else if(a<5){
                    a=a+1;
                    b=0;
                }

            }


            i = i+1;
            int m = ar[i];
            int n = m+9;
            text1 = substring(text, m, n);
            a1 = rotate(a1);


        }
        return out;
    }

Nun wird jedoch nicht das Gewünschte ausgegeben, sondern vielmehr nur einige der insgesamt 36 Zeichen.
Habt jemand eine Idee, wo das Problem liegt?

Viele Grüße
chrysaetos
 
Zuletzt bearbeitet von einem Moderator:
C

chrysaetos

Gast
Hallo nochmal,

das Problem konnte ich nun recht simpel lösen.
Falls es jemanden interessieren sollte :):
Java:
public static char[][] encrypt(String text) {

        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,36};
        char[][] out = new char[6][6];

        int i = 0;
        while(i<4) {
            int a = 0;
            int b = 0;
            int x = 0;

            while (x < text1.length()) {
                if(a1[a][b]=='X'){
                    out[a][b] = text1.charAt(x);
                    x=x+1;
                    if (b<5){
                        b=b+1;
                    }
                    else if(a<5){
                        a=a+1;
                        b=0;
                    }
                }
                else if (b<5){
                    b=b+1;
                }
                else if(a<5){
                    a=a+1;
                    b=0;
                }

            }
            i = i+1;
            int m = ar[i];
            int n = m+9;
            text1 = substring(text, m, n);
            a1 = rotate(a1);


        }
        return out;
    }

Vielen Dank für eure Hilfe :)!
 

Neue Themen


Oben