为什么会出现两个报错 一个是null point 一个是aes的length有问题 就是加密(语言-java)

private static final String KEY = "my-secret-key";

private Button uploadBtn;
private Button downloadBtn;
private ProgressBar progressBar;

private Label statusLabel;

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

@Override
public void start(Stage stage) {
    uploadBtn = new Button("Upload File");
    uploadBtn.setOnAction(e -> uploadFile());

    downloadBtn = new Button("Download File");
    downloadBtn.setOnAction(e -> downloadFile());

    progressBar = new ProgressBar(0);
    progressBar.setPrefWidth(200);

    statusLabel = new Label("");

    VBox root = new VBox(20, uploadBtn, downloadBtn, progressBar, statusLabel);
    root.setAlignment(Pos.CENTER);

    Scene scene = new Scene(root, 300, 250);
    stage.setScene(scene);
    stage.setTitle("File Upload/Download Manager");
    stage.show();
}

private void uploadFile() {
    FileChooser fileChooser = new FileChooser();
    File selectedFile = fileChooser.showOpenDialog(null);
    if (selectedFile != null) {
        try {
            upload(selectedFile);
        } catch (Exception e) {
            e.printStackTrace();
            setStatus("Upload failed");
        }
    }
}

private void downloadFile() {
    FileChooser fileChooser = new FileChooser();
    File selectedFile = fileChooser.showSaveDialog(null);
    if (selectedFile != null) {
        try {
            download(selectedFile);
        } catch (Exception e) {
            e.printStackTrace();
            setStatus("Download failed");
        }
    }
}

private void upload(File file) throws IOException, NoSuchAlgorithmException {
    setStatus("Uploading...");
    progressBar.setProgress(0);

    FileInputStream fis = new FileInputStream(file);
    File encryptedFile = encryptFile(fis, file.getName());

    // Here you would upload the encrypted file to a server
    // using HTTP, FTP, or any other protocol

    progressBar.setProgress(1);
    setStatus("Upload complete");

    encryptedFile.delete(); // delete local copy of encrypted file
}

private void download(File file) throws IOException, NoSuchAlgorithmException {
    setStatus("Downloading...");
    progressBar.setProgress(0);

    // Here you would download the encrypted file from a server
    // using HTTP, FTP, or any other protocol
    // For this example, we just simulate the download by
    // copying the local file to the destination file

    FileInputStream fis = new FileInputStream(getLocalFile());
    decryptFile(fis, file);

    progressBar.setProgress(1);
    setStatus("Download complete");
}

private void setStatus(String message) {
    statusLabel.setText(message);
}

private File getLocalFile() {
    // Here you would get the encrypted file from a server
    // using HTTP, FTP, or any other protocol
    // For this example, we just simulate the download by
    // copying the local file to a temporary file

    try {
        File localFile = File.createTempFile("encrypted", null);
        FileOutputStream fos = new FileOutputStream(localFile);
        fos.write(encrypt("Hello, world!".getBytes()));
        fos.close();

        return localFile;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

private File encryptFile(FileInputStream fis, String filename) throws IOException, NoSuchAlgorithmException {
    byte[] buffer = new byte[1024];
    int bytesRead;
    StringBuilder sb = new StringBuilder();
    while ((bytesRead = fis.read(buffer)) != -1) {
        sb.append(new String(buffer, 0, bytesRead));
    }

    byte[] encryptedData = encrypt(sb.toString().getBytes());
    File encryptedFile = new File(filename + ".enc");
    FileOutputStream fos = new FileOutputStream(encryptedFile);
    fos.write(encryptedData);
    fos.close();

    return encryptedFile;
}

private void decryptFile(FileInputStream fis, File file) throws IOException, NoSuchAlgorithmException {
    byte[] buffer = new byte[1024];
    int bytesRead;
    StringBuilder sb = new StringBuilder();
    while ((bytesRead = fis.read(buffer)) != -1) {
        sb.append(new String(buffer, 0, bytesRead));
    }

    byte[] decryptedData = decrypt(sb.toString().getBytes());
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(decryptedData);
    fos.close();
}

private byte[] encrypt(byte[] data) throws NoSuchAlgorithmException {
    try {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY.getBytes(), "AES"));
        byte[] encryptedData = cipher.doFinal(data);
        return encryptedData;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

private byte[] decrypt(byte[] data) throws NoSuchAlgorithmException {
    try {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY.getBytes(), "AES"));
        byte[] decryptedData = cipher.doFinal(data);
        return decryptedData;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

private byte[] hash(byte[] data) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(data);
    byte[] digest = md.digest();
    return digest;
}

你把报错放一下,否则不知道你的空指针是在哪里触发的。至于illegal key可能是jdk之前的限制导致的,升级的1.8.0_162或以上版本就ok 了