2017-05-31 2 views
1

algorithme impemented AES en utilisant JavaFX « et essaie de mettre en œuvre », mais il me donne pas d'erreur quand je compile le code, mais il fait quand je pressais un bouton de cryptage, il montre un groupe d'erreur dans la ligne de commandeprogramme AES JavaFX montrant erreur lors d'une pression sur le bouton de cryptage

le code sont les suivantes

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(); 
      } 
     } 

    } 
} 

et la sortie de la cmd quand j'appuie sur le bouton de cryptage est

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) 

Répondre

1

Vous La touche r est trop courte. AES fonctionne avec les clés 128 bits, 192 bits et 256 bits.

Vous avez défini keyValue, qui est un tableau de 16 octets (16 * 8 = 128 bits). Utilisez cela à la place.

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

comment puis-je spécifier une clé de 128 bits pouvez-vous me donner un exemple? – Fule

+0

Vous pouvez utiliser chaque chaîne composée de 16 caractères (ASCII) pour obtenir une clé de 128 bits, par exemple. –

+0

« TheBestSecretKey » est un bon exemple –