hi , I have a problem in the following program (at the first link ) , which is : I send a simple encrypted message in AES , the encrypting and sending operations is done ok with no any problems , but when receiving and decrypting , the message decryption operation gives an exceptions , Although the decryption operation is done absolutely very well ( the second link improves that ) , some body tells me what is the problem Exactly please , thanks .
the first link : RapidShare: Easy Filehosting
the second link : RapidShare: Easy Filehosting 12 2554
What exception do you get at what line number? Post the line that is reported for the exception here.
yes , these are the exceptions :
java.lang.NullPointerException
at trying.methodsForEncAndDec.AES_decrypt(methodsForE ncAndDec.java:83)
at trying.UDPServer.main(UDPServer.java:72)
Client send:
Exception in thread "main" java.lang.NullPointerException
at trying.methodsForEncAndDec.byte_deconcat(methodsFo rEncAndDec.java:123
)
at trying.UDPServer.main(UDPServer.java:80)
Press any key to continue...
@techani
Read that stack trace: at line 83 of method AES_decrypt something was null while it shouldn't be (hence the thrown Exception). That method was called from line 72 in your main method (the next line in the stack trace).
Those lines should give you a clue about what was null. Check your own source.
kind regards,
Jos
ya, I have checked , but i really wasn't able to know what is the wrong in the function AES_decrypt at the source code, because i don't see or even I can't see any runtime error there in that function , have you ever tried to execute the program in the second link ?! , then you will find that it does the encryption and the decryption operation very very well
Sooooo the problem surly absolutely is not at the (en-de)cryption operation
But although of that, the runtime error which the compiler Alleges in line 83 in the
AES_decrypt function, shows in some way that there is a problem in the key using operations(maybe ???), but i say again, the key using operation at the program in the second link(you should execute it to get more imagination about the problem) is the same using operations here in the main program, i don't know whats up ! . I really got so disappointed !!!!!!!!!!!!!!!!!!!!!!!!
@techani
No I haven't; not many people like to be lured away in order to download something they don't know. Better post a short (compiling and running) example here in this forum that shows the unwanted behaviour. You'd get more and better responses then.
NullPointerExceptions can easily be pinned down with a couple of System.out.println() statements at crucial locations. Your stack trace helps here.
kind regards,
Jos
ok , here take , this which was at the first link , my main program , three files :
methodsForEncAndDec.java :
UDPServer.java : -
package trying;
-
-
-
import java.io.*;
-
import java.net.*;
-
import javax.swing.*;
-
import java.util.*;
-
import java.security.*;
-
import java.security.spec.*;
-
import javax.crypto.*;
-
import javax.crypto.spec.*;
-
import java.lang.System.*;
-
import java.lang.*;
-
-
-
public class UDPServer{
-
-
-
-
public static void main(String args[]){
-
-
//Create receiver Socket
-
DatagramSocket aSocket = null;
-
-
-
try{
-
//1.create socket at agreed port
-
aSocket = new DatagramSocket(1234);
-
-
-
//2. Define Receeiver Packet Size Size
-
byte[] buffer = new byte[methodsForEncAndDec.msglength];
-
-
-
-
Provider sunjce = new com.sun.crypto.provider.SunJCE();
-
Security.addProvider(sunjce);
-
-
-
-
// 3. Listening....
-
while(true){
-
DatagramPacket request = new DatagramPacket(buffer, buffer.length);
-
aSocket.receive(request);
-
-
-
-
-
//------------------------------- Decryption operation with AES--------------------------------------------------------------------
-
-
-
-
String recieved=new String(request.getData()).trim();
-
-
char[] AES_encrypted_C=recieved.toCharArray();
-
-
byte[] AES_encrypted_B=new byte[AES_encrypted_C.length];
-
-
for (int i=0 ; i<AES_encrypted_C.length ; i++)
-
AES_encrypted_B[i]=(byte)AES_encrypted_C[i];
-
-
-
-
// unhide the following(lines:66-68) if you want to see that the received message is as same as the sent one
-
/*
-
System.out.println("---> After Recieving: <---");
-
for (int i=0 ; i<AES_encrypted_B.length ; i++)
-
System.out.println((char)AES_encrypted_B[i]);
-
*/
-
-
-
byte [] AES_decrypted = methodsForEncAndDec.AES_decrypt(AES_encrypted_B,methodsForEncAndDec.AESinput_key);
-
-
-
-
//---------------------------------------------------------------------------------------------------------------------------------------------
-
-
System.out.println("Client send: ");
-
-
byte [] AES_without_hash =methodsForEncAndDec.byte_deconcat(AES_decrypted);
-
-
-
}
-
}catch (SocketException e){System.out.println("Socket: " + e.getMessage());
-
}catch (IOException e) {System.out.println("IO: " + e.getMessage());
-
}finally {if(aSocket != null) aSocket.close();}
-
}
-
}
-
UDPClient.java : -
package trying;
-
-
import java.io.*;
-
import java.net.*;
-
import javax.swing.*;
-
import java.util.*;
-
import java.security.*;
-
import java.security.spec.*;
-
import javax.crypto.*;
-
import javax.crypto.spec.*;
-
import java.lang.System.*;
-
import java.lang.*;
-
-
-
-
public class UDPClient{
-
-
-
-
-
public static void main(String args[]){
-
-
DatagramSocket aSocket = null;
-
-
-
try {
-
-
// 1. Create Client Socket
-
aSocket = new DatagramSocket();
-
-
-
-
//------------------------------- Encryption operation with AES--------------------------------------------------------------------
-
-
//Declare SunJCE provider.
-
Provider sunjce = new com.sun.crypto.provider.SunJCE();
-
Security.addProvider(sunjce);
-
-
//Get Plaintext
-
Scanner my_object = new Scanner(System.in);
-
System.out.println("---> Enter the plainText: <---");
-
String plain = my_object.nextLine();
-
-
//Hashing then Concatenating
-
byte[] hash = methodsForEncAndDec.SHA_hash(plain);
-
byte[] msg_with_hash = methodsForEncAndDec.byte_concat(plain.getBytes() , hash);
-
-
-
-
//Get AES key
-
System.out.println("---> Enter 16 characters key: <---");
-
methodsForEncAndDec.AESinput_key/*String kyy */= my_object.nextLine();
-
-
//AES encrypt
-
byte [] AES_encrypted = methodsForEncAndDec.AES_encrypt(msg_with_hash,methodsForEncAndDec.AESinput_key/*kyy*/);
-
-
//convert into character array
-
char[] AES_encrypted_C=new char[AES_encrypted.length];
-
for (int i=0 ; i<AES_encrypted.length ; i++)
-
AES_encrypted_C[i]=(char)AES_encrypted[i];
-
-
//convert into string
-
String AES_encrypted_S=new String(AES_encrypted_C);
-
-
//then convert the string into array of bytes to be sent
-
byte[] AES_finalencrypted=AES_encrypted_S.getBytes();
-
-
-
//---------------------------------------------------------------------------------------------------------------------------------------------
-
-
-
-
-
//3.Define the server IP..
-
InetAddress aHost = InetAddress.getByName("localhost");//master
-
-
-
//4. Attatch the socket to the Server Port..
-
int serverPort =1234;
-
-
-
-
//5. define the packet to be sent to the server ...
-
DatagramPacket request =
-
new DatagramPacket(AES_finalencrypted,AES_encrypted_S.length(), aHost, serverPort);
-
-
-
//6. Send the Packet to the server...
-
aSocket.send(request);
-
-
-
methodsForEncAndDec.msglength=AES_finalencrypted.length;
-
-
-
}catch (SocketException e){System.out.println("Socket: " + e.getMessage());
-
}catch (IOException e){System.out.println("IO: " + e.getMessage());
-
}finally {if(aSocket != null) aSocket.close();}
-
}
-
}
-
and this which was at the second link :
AES_SHA_RSA.java : -
import java.util.*;
-
import java.io.*;
-
import java.security.*;
-
import java.security.spec.*;
-
import javax.crypto.*;
-
import javax.crypto.spec.*;
-
import java.lang.System.*;
-
import java.lang.*;
-
-
/*Note 1 : I use 'Base64' to display cipher texts in terms
-
//of hex digits only , If you don't want to use it , you can
-
//do this : Scanner.out.println(new String(cipher_Text));
-
-
//Note 2 : input msg cannot be very long , because i don't
-
//use update() yet, if you want to input a msg with any
-
//length then use update() in conjunction with doFinal()
-
-
//Note 3 : it may be better to build a new function to initia-
-
//-lize the ciphers , i.e. to put the .getInstance() constru-
-
//-ctors there , so there will be only one instance of every
-
//cipher...
-
-
//Note 4 : the program firstly computes the hash of the msg , then //concatenates the hash to the msg , now we encrypt (using RSA or //AES ) the new msg. After decryption , it deconcatenates the hash to //obtain the original msg.
-
*/
-
-
public class AES_SHA_RSA
-
{
-
public static void main(String args[])
-
{
-
//Declare SunJCE provider.
-
Provider sunjce = new com.sun.crypto.provider.SunJCE();
-
Security.addProvider(sunjce);
-
-
//Get Plaintext
-
Scanner my_object = new Scanner(System.in);
-
System.out.println("---> Enter the plainText: <---");
-
String plain = my_object.nextLine();
-
-
//Hashing then Concatenating
-
byte[] hash = SHA_hash(plain);
-
byte[] msg_with_hash = byte_concat(plain.getBytes() , hash);
-
-
//Get AES key
-
System.out.println("---> Enter the 128-bit key: <---");
-
String input_key = my_object.nextLine();
-
-
//AES en(de)crypt
-
byte [] AES_encrypted = AES_encrypt(msg_with_hash,input_key);
-
byte [] AES_decrypted = AES_decrypt(AES_encrypted,input_key);
-
byte [] AES_without_hash = byte_deconcat(AES_decrypted);
-
-
}//end
-
-
-
-
-
//#############################################################
-
public static byte[] SHA_hash(String text)
-
{
-
try{
-
//hash
-
MessageDigest md = MessageDigest.getInstance("SHA");
-
byte[] hash_text = md.digest(text.getBytes());
-
//print
-
String hs = new sun.misc.BASE64Encoder().encode(hash_text);
-
System.out.println("---> hash: <---");
-
System.out.println(hs);
-
return hash_text;
-
}
-
catch( Exception e )
-
{
-
e.printStackTrace();
-
}
-
return null;
-
}
-
//#############################################################
-
public static byte[] AES_encrypt(byte[] plain_Text,String key)
-
{
-
try{
-
// Key
-
byte[] byte_key = key.getBytes();
-
SecretKeySpec final_key = new SecretKeySpec(byte_key,"AES");
-
-
// ENCRYPTION
-
Cipher m_encrypter = Cipher.getInstance("AES/ECB/PKCS5Padding");
-
m_encrypter.init(Cipher.ENCRYPT_MODE , final_key);
-
byte[] encrypted_Text = m_encrypter.doFinal(plain_Text);
-
-
// Print
-
String ct = new sun.misc.BASE64Encoder().encode(encrypted_Text);
-
System.out.println("---> After Encryption with AES: <---");
-
System.out.println(ct);
-
-
return encrypted_Text;
-
}
-
//catch exceptions
-
catch( Exception e )
-
{
-
e.printStackTrace();
-
}
-
return null;
-
}
-
//#############################################################
-
public static byte[] AES_decrypt(byte[] encrypted_Text,String key)
-
{
-
try{
-
// Key
-
byte[] byte_key = key.getBytes();
-
SecretKeySpec final_key = new SecretKeySpec(byte_key,"AES");
-
-
// DECRYPTION
-
Cipher m_decrypter = Cipher.getInstance("AES/ECB/PKCS5Padding");
-
m_decrypter.init(Cipher.DECRYPT_MODE , final_key);
-
byte[] decrypted_Text = m_decrypter.doFinal(encrypted_Text);
-
-
// Print
-
String dt = new sun.misc.BASE64Encoder().encode(decrypted_Text);
-
System.out.println("---> After Decryption with AES: <---");
-
System.out.println(dt);
-
-
return decrypted_Text;
-
}
-
//catch exceptions
-
catch( Exception e )
-
{
-
e.printStackTrace();
-
}
-
return null;
-
}
-
//#############################################################
-
public static byte[] byte_concat(byte[] a , byte[] b)
-
{
-
byte[] x = new byte[a.length + b.length];
-
for(int i=0 ; i<a.length ; i++)
-
{
-
x[i]=a[i];
-
}
-
for(int j=a.length ; j<x.length ; j++)
-
{
-
x[j]=b[j-a.length];
-
}
-
return x;
-
}
-
//#############################################################
-
public static byte[] byte_deconcat(byte[] a)
-
{
-
//20 byte = 160bit = length of SHA hash to be deconcat
-
byte[] x = new byte[a.length - 20];
-
for(int i=0 ; i<x.length ; i++)
-
{
-
x[i]=a[i];
-
}
-
//print
-
System.out.println("---> After de-concat : <---");
-
for (int i=0 ; i<x.length ; i++)
-
{
-
System.out.print((char)x[i]);
-
}
-
System.out.println("");
-
return x;
-
}
-
-
}
-
and what do you mean by : can easily be pinned down with a couple of System.out.println() statements at crucial locations??? sorry i didn't understand , could you explain well please ??? thanks .
@techani
That is not required. You were supposed to post only the lines at and just before the line reported for the exception by your stacktrace. @techani
You have got to be kidding me.
I wrote: "Better post a short (compiling and running) example here in this forum that shows the unwanted behaviour. You'd get more and better responses then."
kind regards,
Jos
I'm missing line numbers, what line is exactly 83?
I'm very sorry for late , here is the line :
SecretKeySpec final_key = new SecretKeySpec(byte_key,"AES");
thanks.
???????????????????????????????????????
Try adding the following lines before that line: -
if ( byte_key == null){
-
System.out.println("My program will die here");
-
}
-
Also please use code tags. eg [ code ] without the spaces [ / code]
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Bruce Davis |
last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded
tkinter gui application. The problem appears to be a deadlock condition
when a child thread pops up a
Pmw dialog window in...
|
by: jrlen balane |
last post by:
basically what the code does is transmit data to a hardware and then
receive data that the hardware will transmit.
import serial
import string
import time
from struct import *
ser =...
|
by: fripper |
last post by:
I posted this problem a couple of days ago but felt I might have better luck
re-stating the problem.
Apparently I messed up IIS (v. 5) somehow because I am suddenly unable to
load web forms! A...
|
by: Peter Olcott |
last post by:
www.halting-problem.com
|
by: Sarah |
last post by:
I need to access some data on a server. I can access it directly using UNC
(i.e. \\ComputerName\ShareName\Path\FileName) or using a mapped network
drive resource (S:\Path\FileName).
Here is my...
|
by: Harold Howe |
last post by:
I am having a problem deserializing objects from a library when the
following conditions exist:
1- The library is strongly named
2- The serialized file was created with version 1.0 of the...
|
by: dav3 |
last post by:
I am by no means an ultra slick programmer and my problem solving skills.. well they leave much to be desired. That being said I have been working on the following problem for the past few days and...
|
by: HC |
last post by:
Hello, all, I started out thinking my problems were elsewhere but as I
have worked through this I have isolated my problem, currently, as a
difference between MSDE and SQL Express 2005 (I'll just...
|
by: Rene |
last post by:
Hello to all!
For a long time I have been "fighting" a problem compiling an OpenGL
program which uses GLUT. First I have put a question in a Watcom group
(I want to use this compiler) to which I...
|
by: =?Utf-8?B?amVmZmVyeQ==?= |
last post by:
i need help with a combo box and this same code works on my first tab with a
combo box. The error or problem i have is this code causes an index out of
range error when i run it on my second combo...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |