Results 1 to 7 of 7
  1. #1
    yvprashant is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default Exception in thread "main" java.lang.NullPointerException

    Am trying a program to encrypt a string using 3DES.

    Source code of the program -

    import java.util.*;
    import javax.crypto.*;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;

    import java.security.*;
    public class TripleDESTest {
    public static void main(String[] args) throws Exception {
    String text = "kyle boon";
    byte[] codedtext = new TripleDESTest().encrypt(text);
    String decodedtext = new TripleDESTest().decrypt(codedtext);
    System.out.println(codedtext);
    System.out.println(decodedtext); // This correctly shows "kyle boon"

    }
    public byte[] encrypt(String message) throws Exception {
    MessageDigest md = null;
    final byte[] digestOfPassword = md.digest("HG58YZ3CR9"
    .getBytes("utf-8"));
    final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8;) {
    keyBytes[k++] = keyBytes[j++];
    }
    final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
    final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
    final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key, iv);
    final byte[] plainTextBytes = message.getBytes("utf-8");
    final byte[] cipherText = cipher.doFinal(plainTextBytes);
    // final String encodedCipherText = new sun.misc.BASE64Encoder()
    // .encode(cipherText);
    return cipherText;
    }
    public String decrypt(byte[] message) throws Exception {
    final MessageDigest md = MessageDigest.getInstance("md5");
    final byte[] digestOfPassword = md.digest("HG58YZ3CR9"
    .getBytes("utf-8"));
    final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8;) {
    keyBytes[k++] = keyBytes[j++];
    }
    final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
    final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
    final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    decipher.init(Cipher.DECRYPT_MODE, key, iv);
    // final byte[] encData = new
    // sun.misc.BASE64Decoder().decodeBuffer(message);
    final byte[] plainText = decipher.doFinal(message);
    return new String(plainText, "UTF-8");
    }
    }

    Error details -
    Exception in thread "main" java.lang.NullPointerException
    at TripleDESTest.encrypt(TripleDESTest.java:18)
    at TripleDESTest.main(TripleDESTest.java:10)

    Any help would be greatly appreciated. Thank you. :)

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    MessageDigest md = null;
    final byte[] digestOfPassword = md.digest("HG58YZ3CR9".getBytes("utf-8"));

    md is null and in the nex tline you are calling a method on it --> NPE!

    MessageDigest md = MessageDigest.getInstance("...."); <---

  3. #3
    yvprashant is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    ah! right!

    Am a beginner in java programming.
    Can you tell me how do i correct this?

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    I already have?
    MessageDigest md = MessageDigest.getInstance(algorithm HERE, such as "SHA");

    Take a look at:
    MessageDigest (Java Platform SE 6)

  5. #5
    yvprashant is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks!
    But now am getting this error -

    Exception in thread "main" 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.DESedeCipher.engineDoFinal (DashoA13*..)
    at javax.crypto.Cipher.doFinal(DashoA13*..)
    at TripleDESTest.decrypt(TripleDESTest.java:47)
    at TripleDESTest.main(TripleDESTest.java:11)

  6. #6
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    java.lang.NullPointerException reason is that you are accessing object that is not exist or your code cant access the object.

    example

    YourObjectClass MyVar = null; //<- null pointers
    then you are invoking "var" //then this will cause "java.lang.NullPointerException"

    the code bellow is not null.
    YourObjectClass MyVar = new YourObjectClass() //<- this is not null.

  7. #7
    yvprashant is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 01-10-2010, 01:49 PM
  2. Replies: 2
    Last Post: 08-15-2009, 09:05 AM
  3. Replies: 6
    Last Post: 07-16-2009, 04:30 PM
  4. Replies: 1
    Last Post: 07-16-2009, 11:35 AM
  5. Replies: 1
    Last Post: 07-16-2007, 07:32 AM

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
  •