Results 1 to 3 of 3
  1. #1
    smart_mody is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Smile problem about AES Decryption(help me please)

    hello guys


    I want you to help in this code,


    PHP Code:
       import java.security.*;
       import javax.crypto.*;
       import javax.crypto.spec.*;
       import java.io.*;
       
       import java.awt.event.*;
       import javax.swing.*;
       import java.awt.*;
    
    
       public class AES extends JFrame {
         
          JTextArea jtfEncrypt,jtfDecrypt;
       JButton Encrypt,Decrypt,clear,exit;
       
       
       private class ClickListener implements ActionListener
          {
             public void actionPerformed(ActionEvent e) 
           {
             if (e.getSource() == Encrypt)               
             {
             try {
                Enc();
             }
             catch (Exception ex) {
                ex.printStackTrace();
             }
                /////////////
              }
              else if (e.getSource() == Decrypt)               
             {     
             try {
                Dec();
             }
             catch (Exception ex) {
                ex.printStackTrace();
             }
              /////////////
              }
              else if (e.getSource() == clear)               
             {               
              	Clr();
              }
              else if (e.getSource() == exit)               
             {               
              System.exit(0);
              }
             }
         }
           
           
           
       private class Closer implements WindowListener
        {
           public void windowClosing(WindowEvent e)
           {
              /////////
           }
           public void windowActivated(WindowEvent e) {}
           public void windowClosed(WindowEvent e) {}
           public void windowDeactivated(WindowEvent e) {}
           public void windowDeiconified(WindowEvent e) {}
           public void windowIconified(WindowEvent e) {}
           public void windowOpened(WindowEvent e) {}
       
       }
       
         
       public AES()
       {
          jtfEncrypt=new JTextArea();
          jtfDecrypt=new JTextArea();
          Encrypt=new JButton("Encrypt");
          Decrypt=new JButton("Decrypt");
          clear=new JButton("clear");
          exit=new JButton("exit");
         
         
          JPanel panel =new JPanel();
          panel= (JPanel) getContentPane();
         
          panel.setLayout(new GridLayout(3,1));
         
          panel.add(jtfEncrypt);
          panel.add(Encrypt);
          panel.add(jtfDecrypt);
          panel.add(Decrypt);
          panel.add(clear);
          panel.add(exit);
         
         
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          this.setTitle("AES ...");
          this.setResizable(false);
          this.setSize(500,500);
          this.setLocation(350,350);
          this.show();
         
         
                ClickListener cl = new ClickListener();
                     
             Encrypt.addActionListener(cl);
             Decrypt.addActionListener(cl);
             clear.addActionListener(cl);
             exit.addActionListener(cl);
    
             this.addWindowListener(new Closer());//register Win(Frame)
             
       }
         
         public static String asHex (byte buf[]) {
          StringBuffer strbuf = new StringBuffer(buf.length * 2);
          int i;
    
          for (i = 0; i < buf.length; i++) {
           if (((int) buf[i] & 0xff) < 0x10)
           strbuf.append("0");
    
           strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
          }
    
          return strbuf.toString();
         }
    
         public static void main(String[] args) throws Exception {
          skeySpec = generateKey();
          new AES();
         }
       
       public static SecretKeySpec generateKey ( ) throws Exception {
           // Get the KeyGenerator
           KeyGenerator kgen = KeyGenerator.getInstance("AES");
           kgen.init(128); // 192 and 256 bits may not be available
    
           // Generate the secret key specs.
           SecretKey skey = kgen.generateKey();
           byte[] raw = skey.getEncoded();
    
           SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
           
            return skeySpec ;
       }
       
       public void Enc () throws Exception {
          // Instantiate the cipher
           Cipher cipher = Cipher.getInstance("AES");
           cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
           byte[] encrypted =cipher.doFinal(jtfEncrypt.getText().trim().getBytes());
           jtfDecrypt.setText(asHex(encrypted));
       }
       
       public void Dec () throws Exception {
           Cipher cipher = Cipher.getInstance("AES");
           cipher.init(Cipher.DECRYPT_MODE, skeySpec);
           byte[] decrypted =cipher.doFinal(jtfDecrypt.getText().trim().getBytes());
           jtfEncrypt.setText(asHex(decrypted));
       }
       
       public void Clr () {
       	
       	jtfDecrypt.setText("");
       	jtfEncrypt.setText("");
       }
       
       private static SecretKeySpec skeySpec ;
    }
    i have an Exception when press Decryption button


    look at this :

    javax.crypto.BadPaddingException: Given final block not properly padded
    at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
    at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
    at com.sun.crypto.provider.AESCipher.engineDoFinal(Da shoA13*..)
    at javax.crypto.Cipher.doFinal(DashoA13*..)
    at AES.Dec(AES.java:156)
    at AES$ClickListener.actionPerformed(AES.java:34)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6041)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3265)
    at java.awt.Component.processEvent(Component.java:580 6)
    at java.awt.Container.processEvent(Container.java:205 8)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4413)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2116)
    at java.awt.Component.dispatchEvent(Component.java:42 43)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4322)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3986)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3916)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2102)
    at java.awt.Window.dispatchEventImpl(Window.java:2440 )
    at java.awt.Component.dispatchEvent(Component.java:42 43)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)




    Can the Java programmers to help me in this forum

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,814
    Rep Power
    19

    Default

    Have you tried it without the trim() call?

  3. #3
    smart_mody is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Re: Exception in PGP Decryption
    By Deepa in forum New To Java
    Replies: 27
    Last Post: 07-18-2012, 05:16 AM
  2. PGP Encryption&Decryption
    By Deepa in forum New To Java
    Replies: 2
    Last Post: 07-07-2009, 06:22 AM
  3. Encryption/Decryption
    By Echilon in forum New To Java
    Replies: 2
    Last Post: 03-24-2009, 11:58 AM
  4. Encryption-Decryption Problem
    By dinesh.1.sharma in forum Advanced Java
    Replies: 1
    Last Post: 07-17-2008, 10:03 PM
  5. Encryption/Decryption Through AOP
    By SirRawlins in forum Advanced Java
    Replies: 0
    Last Post: 12-19-2007, 03:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •