运行2个java代码

时间:2015-11-18 10:35:42

标签: java

我有2个java程序(密码),一个是Playfair,第二个是Transposition。 现在我想运行Playfair代码,然后使用我从Playfair代码获得的结果编译Transposition。我该怎么做?(

Playfair代码

import java.awt.Point;
import java.util.Scanner;

public class PlayfairCipher {
    private static char[][] charTable;
    private static Point[] positions;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String key = prompt("Enter an encryption key (min length 6): ", sc, 6);
        String txt = prompt("Enter the message: ", sc, 1);
        String jti = prompt("Replace J with I? y/n: ", sc, 1);

        boolean changeJtoI = jti.equalsIgnoreCase("y");

        createTable(key, changeJtoI);

        String enc = encode(prepareText(txt, changeJtoI));

        System.out.printf("%nEncoded message: %n%s%n", enc);
        System.out.printf("%nDecoded message: %n%s%n", decode(enc));
    }

    private static String prompt(String promptText, Scanner sc, int minLen) {
        String s;
        do {
            System.out.print(promptText);
            s = sc.nextLine().trim();
        } while (s.length() < minLen);
        return s;
    }

    private static String prepareText(String s, boolean changeJtoI) {
        s = s.toUpperCase().replaceAll("[^A-Z]", "");
        return changeJtoI ? s.replace("J", "I") : s.replace("Q", "");
    }

    private static void createTable(String key, boolean changeJtoI) {
        charTable = new char[5][5];
        positions = new Point[26];

        String s = prepareText(key + "ABCDEFGHIJKLMNOPQRSTUVWXYZ", changeJtoI);

        int len = s.length();
        for (int i = 0, k = 0; i < len; i++) {
            char c = s.charAt(i);
            if (positions[c - 'A'] == null) {
                charTable[k / 5][k % 5] = c;
                positions[c - 'A'] = new Point(k % 5, k / 5);
                k++;
            }
        }
    }

    private static String encode(String s) {
        StringBuilder sb = new StringBuilder(s);

        for (int i = 0; i < sb.length(); i += 2) {

            if (i == sb.length() - 1)
                sb.append(sb.length() % 2 == 1 ? 'X' : "");

            else if (sb.charAt(i) == sb.charAt(i + 1))
                sb.insert(i + 1, 'X');
        }
        return codec(sb, 1);
    }

    private static String decode(String s) {
        return codec(new StringBuilder(s), 4);
    }

    private static String codec(StringBuilder text, int direction) {
        int len = text.length();
        for (int i = 0; i < len; i += 2) {
            char a = text.charAt(i);
            char b = text.charAt(i + 1);

            int row1 = positions[a - 'A'].y;
            int row2 = positions[b - 'A'].y;
            int col1 = positions[a - 'A'].x;
            int col2 = positions[b - 'A'].x;

            if (row1 == row2) {
                col1 = (col1 + direction) % 5;
                col2 = (col2 + direction) % 5;

            } else if (col1 == col2) {
                row1 = (row1 + direction) % 5;
                row2 = (row2 + direction) % 5;

            } else {
                int tmp = col1;
                col1 = col2;
                col2 = tmp;
            }

            text.setCharAt(i, charTable[row1][col1]);
            text.setCharAt(i + 1, charTable[row2][col2]);
        }
        return text.toString();
    }
}

和换位

import java.util.*;
import java.util.Scanner;  // needed for Scanner

public class transpositionCipher
{

public static void main(String args[])
{
String key;
String message;
String encryptedMessage; 
// Letters in the x-axis
int x=0; 
// Letters in the y-axis
int y=0; 


// Prompt the user
System.out.print( "Type your Key : " );


// Read a line of text from the user.

Scanner scan = new Scanner(System.in);
key = scan.nextLine();

// Display the input back to the user.
System.out.println( "Your Key is " + key );


//Prompt the user
System.out.print( "Type your Message : " );


//Read a line of text from the user.
message = scan.nextLine();

//Display the input back to the user.
System.out.println( "Your Message is " + message );


int msgchar = message.length();
int keycahr = key.length();

if (!((msgchar % keycahr) == 0)){

do{
message = message + "x";
msgchar = message.length();
}while(!((msgchar % keycahr) == 0));

}


encryptedMessage = "";

// To set the temp as [x][y]
char temp[][]=new char [key.length()][message.length()];
char msg[] = message.toCharArray();
// To populate the array
x=0;
y=0;
// To convert the message into an array of char
for (int i=0; i< msg.length;i++)
{
temp[x][y]=msg[i];
if (x==(key.length()-1)) 
{
x=0;
y=y+1;
} // Close if 
else 
{
x++;
}
} // Close for loop

// To sort the key
char t[]=new char [key.length()];
t=key.toCharArray();
Arrays.sort(t);

for (int j=0;j<y;j++)
{
for (int i=0;i<key.length();i++)
{
System.out.print(temp[i][j]);
}
System.out.println();
}

System.out.println();

// To print out row by row (i.e. y)
for (int j=0;j<y;j++){ 
// To compare the the sorted Key with the key
// For char in the key
for (int i=0;i<key.length();i++){ 
int pos=0;
// To get the position of key.charAt(i) from sorted key
for (pos=0;pos<t.length;pos++){ 
if (key.charAt(i)==t[pos]){ 
// To break the for loop once the key is found
break;
}
}
System.out.print(temp[pos][j]);
encryptedMessage+=temp[pos][j];
}
System.out.println();
}

System.out.println(encryptedMessage);
System.exit(0);
}enter code here
}

1 个答案:

答案 0 :(得分:0)

取一个操作(PlayfairCipher.encode)的输出,这是一个cyphertext并将其输入到第二个操作(你的转置代码),因为它是明文。

Scanner sc = new Scanner(System.in);

String key = prompt("Enter an encryption key (min length 6): ", sc, 6);
String txt = prompt("Enter the message: ", sc, 1);
String jti = prompt("Replace J with I? y/n: ", sc, 1);

boolean changeJtoI = jti.equalsIgnoreCase("y");

PlayfairCipher.createTable(key, changeJtoI);

String enc = PlayfairCipher.encode(prepareText(txt, changeJtoI));

//Now instead of using 'message' for the second encryption, you use the output of the first operation 'enc'

int x=0; 
int y=0; 
int msgchar = enc.length();
int keycahr = key.length();

if (!((msgchar % keycahr) == 0)){

   do{
      enc = enc + "x";
      msgchar = enc.length();
   }while(!((msgchar % keycahr) == 0));
}

...