Results 1 to 3 of 3
- 09-28-2009, 10:28 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 2
- Rep Power
- 0
problem about AES Decryption(help me please)
hello guys
I want you to help in this code,
i have an Exception when press Decryption buttonPHP 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 ; }
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
- 09-29-2009, 09:56 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Have you tried it without the trim() call?
- 09-29-2009, 11:32 PM #3
Member
- Join Date
- Jul 2009
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Re: Exception in PGP Decryption
By Deepa in forum New To JavaReplies: 27Last Post: 07-18-2012, 05:16 AM -
PGP Encryption&Decryption
By Deepa in forum New To JavaReplies: 2Last Post: 07-07-2009, 06:22 AM -
Encryption/Decryption
By Echilon in forum New To JavaReplies: 2Last Post: 03-24-2009, 11:58 AM -
Encryption-Decryption Problem
By dinesh.1.sharma in forum Advanced JavaReplies: 1Last Post: 07-17-2008, 10:03 PM -
Encryption/Decryption Through AOP
By SirRawlins in forum Advanced JavaReplies: 0Last Post: 12-19-2007, 03:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks