Results 1 to 7 of 7
- 07-18-2011, 10:44 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
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. :)
- 07-18-2011, 11:19 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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("...."); <---
- 07-18-2011, 11:24 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
ah! right!
Am a beginner in java programming.
Can you tell me how do i correct this?
- 07-18-2011, 11:28 AM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
I already have?
MessageDigest md = MessageDigest.getInstance(algorithm HERE, such as "SHA");
Take a look at:
MessageDigest (Java Platform SE 6)
- 07-18-2011, 11:35 AM #5
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
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)
- 07-19-2011, 06:25 PM #6
Banned
- Join Date
- Feb 2011
- Posts
- 65
- Rep Power
- 0
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.
- 07-19-2011, 06:44 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Exception in thread "main" java.lang.NullPointerException
By Borysator in forum New To JavaReplies: 2Last Post: 01-10-2010, 12:49 PM -
Exception in thread "main" java.lang.NullPointerException
By new_coder in forum New To JavaReplies: 2Last Post: 08-15-2009, 08:05 AM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 6Last Post: 07-16-2009, 03:30 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 1Last Post: 07-16-2009, 10:35 AM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 06:32 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks