java - AES javafx 程序在按下加密按钮时显示错误

标签 java arrays javafx javafx-2 javafx-8

使用javafx实现了AES算法“我正在尝试实现”,但是当我编译代码时它没有给我错误,但是当我按下加密按钮时它会在命令行中显示一堆错误

代码如下

import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.geometry.*;
import javafx.scene.image.*;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AESGui extends Application{

    TextArea inputArea = new TextArea();
    TextArea outputArea = new TextArea();
    Main object1 = new Main();

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


    @Override
    public void start(Stage stage){

        HBox hbox1 = new HBox(50);
        hbox1.setAlignment(Pos.CENTER);
        hbox1.getChildren().addAll(inputArea, outputArea);

        Button encrypt = new Button("Encrypt");
        encrypt.setOnAction (event -> {
            object1.aesEncryption();
        });

        Button decrypt = new Button("Decrypt");

        HBox hbox2 = new HBox(50);
        hbox2.setAlignment(Pos.CENTER);
        hbox2.getChildren().addAll(encrypt, decrypt);

        VBox vbox = new VBox();
        vbox.setAlignment(Pos.CENTER);
        vbox.getChildren().addAll(hbox1, hbox2);

        Scene scene = new Scene(vbox, 900, 700);

        stage.setScene(scene);
        stage.setTitle("aes gui");
        stage.show();
    }

    public class AESAlgorithm{
        public String algo = "AES";
        public byte[] keyValue;

        public AESAlgorithm(String key){
            keyValue = key.getBytes();
        }

        public Key generateKey() throws Exception{
            Key key = new SecretKeySpec(keyValue, algo);
            return key;
        }

        public String encrypt(String msg) throws Exception{

            Key key = generateKey();
            Cipher c = Cipher.getInstance(algo);
            c.init(Cipher.ENCRYPT_MODE, key);
            byte[] encVal = c.doFinal(msg.getBytes());
            String encryptedValue = new BASE64Encoder().encode(encVal);
            return encryptedValue;

        }

        public String decrypt(String msg) throws Exception{

            Key key = generateKey();
            Cipher c = Cipher.getInstance(algo);
            c.init(Cipher.DECRYPT_MODE, key);
            byte[] decordedValue = new BASE64Decoder().decodeBuffer(msg);
            byte[] decValue = c.doFinal(decordedValue);
            String decryptedValue = new String(decValue);
            return decryptedValue;
        }
    }

    public class Main {
        AESAlgorithm aesAlgo;
        private byte[] keyValue = new byte[]{'t','h','e','b','e','s','t','s','e','c','r','e','t','k','e','y'};
        public Main(){
            //initComponents();
            //here we passed key to constructor
            aesAlgo = new AESAlgorithm("MySecretKey");
        }

        private void aesEncryption(){
            try{

                String plainText = inputArea.getText();
                String encryptedText = aesAlgo.encrypt(plainText);
                outputArea.setText(encryptedText);

            }
            catch(Exception e){
                e.printStackTrace();
            }
        }

    }
}

当我按下加密按钮时,cmd 的输出是

java.security.InvalidKeyException: Invalid AES key length: 11 bytes
at com.sun.crypto.provider.AESCipher.engineGetKeySize(AESCipher.java:509)
at javax.crypto.Cipher.passCryptoPermCheck(Cipher.java:1067)
at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1025)
at javax.crypto.Cipher.implInit(Cipher.java:801)
at javax.crypto.Cipher.chooseProvider(Cipher.java:864)
at javax.crypto.Cipher.init(Cipher.java:1249)
at javax.crypto.Cipher.init(Cipher.java:1186)
at AESGui$AESAlgorithm.encrypt(AESGui.java:79)
at AESGui$Main.aesEncryption(AESGui.java:111)
at AESGui$Main.access$000(AESGui.java:98)
at AESGui.lambda$start$0(AESGui.java:42)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)

最佳答案

您的 key 太短。 AES 适用于 128 位、192 位和 256 位 key 。

您已经定义了keyValue,它是一个 16 字节数组(16*8 = 128 位)。请改用它。

public class Main {
        AESAlgorithm aesAlgo;
        private byte[] keyValue = new byte[]{'t','h','e','b','e','s','t','s','e','c','r','e','t','k','e','y'};
        public Main(){
            //initComponents();
            //here we passed key to constructor
            aesAlgo = new AESAlgorithm(keyValue);
        }

        private void aesEncryption(){
            try{

                String plainText = inputArea.getText();
                String encryptedText = aesAlgo.encrypt(plainText);
                outputArea.setText(encryptedText);

            }
            catch(Exception e){
                e.printStackTrace();
            }
        }

    }

关于java - AES javafx 程序在按下加密按钮时显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44294039/

相关文章:

java用户界面实现: short-term test + progress indicator w/o using separate dialog

java - 从使用Mockito模拟的restTemplate.exchange()中检索httpEntity对象

mysql - 使用 'While' 回显数组结果不显示第 1 行

java - Gradle - 将 JavaFX SDK 添加到类路径

css - JavaFX:获取TextArea的行高

java - 如何定义 ListView 应该使用哪个属性来呈现

java - Foursquare API 场馆搜索返回空场馆

java - 如何衡量来自特定用户的 REST API 请求数量?

arrays - 在 Swift 中访问属于数组的字典中的值

c - 递增 float 组指针