2017-05-31 40 views
1

使用javafx强制执行A​​ES算法“我正在尝试执行”但它在编译代码时没有出错,但是当按下加密按钮时显示一堆在命令行AES javafx程序在加密按钮过程中显示错误

码错误的情况如下

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) 

回答

1

r键太短。 AES适用于128位,192位和256位密钥。

您已定义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(); 
      } 
     } 

    } 
+0

我该如何指定一个128位密钥,你能给我一个例子吗? – Fule

+0

例如,您可以使用由16个字符(ASCII)组成的每个字符串来获取128位密钥。 –

+0

“TheBestSecretKey”就是很好的例子 –