Results 1 to 5 of 5
  1. #1
    fhamumtaz is offline Member
    Join Date
    Feb 2017
    Posts
    3
    Rep Power
    0

    Default Can't decrypt ciphertext (AES)

    I made project email client and used encrypt decrypt with AES256, but still there is problem only in decrypt. let me know the solution..
    Part of my code :

    Class enkripdekrip :

    package AES256;

    import java.security.AlgorithmParameters;
    import java.security.SecureRandom;
    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.PBEKeySpec;
    import javax.crypto.spec.SecretKeySpec;
    import javax.swing.JTextArea;
    import org.apache.commons.codec.binary.Base64;

    public class EnkripDekrip {

    //private static final String password = "test";
    private static String salt;
    private static int pswdIterations = 65536 ;
    private static int keySize = 256;
    private byte[] ivBytes;


    public String encrypt(String plainText, String password) throws Exception {

    //get salt
    salt = generateSalt();
    byte[] saltBytes = salt.getBytes("UTF-8");

    // Derive the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1") ;
    PBEKeySpec spec = new PBEKeySpec(
    password.toCharArray(),
    saltBytes,
    pswdIterations,
    keySize
    );

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

    //encrypt the message
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    AlgorithmParameters params = cipher.getParameters();
    ivBytes = params.getParameterSpec(IvParameterSpec.class).get IV();
    byte[] encryptedTextBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
    return new Base64().encodeAsString(encryptedTextBytes);
    }

    @SuppressWarnings("static-access")
    public String decrypt(String encryptedText, String password) throws Exception {

    byte[] saltBytes = salt.getBytes("UTF-8");
    byte[] encryptedTextBytes = new Base64().decodeBase64(encryptedText);

    // Derive the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1") ;
    PBEKeySpec spec = new PBEKeySpec(
    password.toCharArray(),
    saltBytes,
    pswdIterations,
    keySize
    );

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

    // Decrypt the message
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes));


    byte[] decryptedTextBytes = null;
    try {
    decryptedTextBytes = cipher.doFinal(encryptedTextBytes);
    } catch (IllegalBlockSizeException e) {
    e.printStackTrace();
    } catch (BadPaddingException e) {
    e.printStackTrace();
    }

    return new String(decryptedTextBytes);
    }

    public String generateSalt() {
    SecureRandom random = new SecureRandom();
    byte bytes[] = new byte[20];
    random.nextBytes(bytes);
    String s = new String(bytes);
    return s;
    }
    }




    And for actionDekrip, part of my code :

    private void actionDekrip() {
    String chipper = null;
    String kunci = "";
    kunci = JOptionPane.showInputDialog("Enter password for decryption");

    if (kunci.isEmpty()|| kunci == null) {
    JOptionPane.showMessageDialog(rootPane,
    "Enter the password!",
    "Warning!!",
    JOptionPane.ERROR_MESSAGE);

    } else {
    try {
    chipper = enkripDekrip.decrypt(messageTextArea.getText(), kunci);

    } catch (Exception ex) {
    Logger.getLogger(MessageDialog.class.getName()).lo g(Level.SEVERE, null, ex);
    }

    messageTextArea.setText(chipper);
    }
    }



    and the error :

    Mar 08, 2017 9:48:58 AM emailclientaes.EmailClientAES actionDekrip
    SEVERE: null
    java.lang.NullPointerException
    at AES256.EnkripDekrip.decrypt(EnkripDekrip.java:64)
    at emailclientaes.EmailClientAES.actionDekrip(EmailCl ientAES.java:215)
    at emailclientaes.EmailClientAES.access$600(EmailClie ntAES.java:20)
    at emailclientaes.EmailClientAES$9.actionPerformed(Em ailClientAES.java:152)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6505)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:627 0)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719 )
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 693)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Can't decrypt ciphertext (AES)

    There is a NullPointerException at line 64. (In other words: there is a reference that is null and you are trying to invoke a method on it).
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Can't decrypt ciphertext (AES)

    You ignored the responses on the two threads you started earlier. Why should we expect anything different here?

    Use code tags to post codes -- [code]CODE[/code] will display as
    Java Code:
    CODE
    .

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Can't decrypt ciphertext (AES)

    Which is exactly what the stack trace said. Don't folks ever read the error messages??

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Can't decrypt ciphertext (AES)

    Quote Originally Posted by jim829 View Post
    Which is exactly what the stack trace said. Don't folks ever read the error messages??

    Regards,
    Jim

    No. It's easier to register an account on a random Java forum, find the correct section to post your half-baked question, then sit back and enjoy all the solutions that are handed to you on a silver platter.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. How to decrypt SSL?
    By Sergio in forum Advanced Java
    Replies: 3
    Last Post: 02-18-2016, 05:42 PM
  2. Ciphertext
    By newby in forum New To Java
    Replies: 2
    Last Post: 04-05-2012, 06:40 AM
  3. Help with creating ciphertext
    By bdl1127 in forum New To Java
    Replies: 1
    Last Post: 02-24-2012, 05:09 PM
  4. need to decrypt...HELP PLEASE!!!thanks!
    By dmerunong in forum New To Java
    Replies: 3
    Last Post: 09-07-2011, 02:56 PM
  5. Replies: 2
    Last Post: 04-19-2010, 06:25 PM

Tags for this Thread

Posting Permissions

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