加密char 13次JavaFX

时间:2019-02-15 00:50:11

标签: java javafx char

我无法在13个字符后对代码进行加密。该代码应该更改在加密的每个字符13个字符的文本字段中键入的内容。例如,abc被加密为nop。这是问题。 Part one of the question and second part of the question 我认为方法是问题所在。现在,我意识到我认为它不能包含13个字符。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

    public class Encrypt13 extends Application {
        @Override
        public void start(Stage primaryStage) {



                // add a gridpane
                GridPane grid = new GridPane();
                grid.setPadding(new Insets(10, 10, 10, 10));



            // add a label for original message top left side
            Label label = new Label("Original Message: ");
            GridPane.setConstraints(label, 0, 0);

            // add label2 for message encrypted
            Label label2 = 

        new Label("Encrpyted Message: ");
        GridPane.setConstraints(label2, 0, 2);

        // add input of text to be encrypted right side of label
        TextField textfield = new TextField();
        GridPane.setConstraints(textfield, 2, 0);

        // add text that will be event handled below textfield
        Label text = new Label();
        GridPane.setConstraints(text, 2, 2);

        // add it to the parent
        grid.getChildren().addAll(label, label2, textfield, text);

        // add an event
        textfield.setOnKeyPressed(e -> {
            // add event with Enter pressed to add arrays compared and added to get 13
            // characters
            char[] newArray = new char[textfield.getLength()];
            if (e.getCode() == KeyCode.ENTER) {
                // this is for the array to get the number checked and added to i char
                for (int p = 0; p < textfield.getLength(); p++) {
                    // this is to get the char i start by the typed from the char until the length
                    // incremented by the method getAllAlpha
                    // which has all alphabets in parameters of the typed with the p number at the
                    // charAt
                    for (char i = textfield.getText().charAt(p); i < textfield.getText().charAt(p) + 1; i += getAllAlpha(textfield.getText(), p)) {
                        newArray[p] += i;
                        text.setText(new String (newArray));
                    }
                }
            }
        });

        // set a scene and place show it
        Scene scene = new Scene(grid, 400, 400);
        primaryStage.setTitle("Encrypt13");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static char getAllAlpha(String q, int x) {
        String alpha = "";
        int i;
        for(i = 0; i <= 255; i++)
        {
            alpha+=(char)i;
        }

        return alpha.charAt(Character.getNumericValue(q.charAt(x)));
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

1 个答案:

答案 0 :(得分:2)

使用char支持与其他数字类型相同的运算的事实,以及a-z和A-Z分别是连续值的事实。

您可以使用%运算符将字符保持在所需范围内,并包装太大的值。

final int rotationDistance = 13;
final int alphabetCharCount = ('Z' - 'A' + 1);

// add an event
textfield.setOnAction(e -> {
    char[] chars = textfield.getText().toCharArray();

    // rotate chars in [a-zA-Z]
    for (int i = 0; i < chars.length; i++) {
        char c = chars[i];
        if (c >= 'A' && c <= 'Z') {
            chars[i] = (char) ((c - 'A' + rotationDistance) % alphabetCharCount + 'A');
        } else if (c >= 'a' && c <= 'z') {
            chars[i] = (char) ((c - 'a' + rotationDistance) % alphabetCharCount + 'a');
        }
    }
    text.setText(new String(chars));
});

您当然还可以使用条件来修复超出字母范围的字符。

if (c >= 'A' && c <= 'Z') {
    c += rotationDistance;
    chars[i] = (c > 'Z') ? (char) (c - alphabetCharCount) : c;
} ...