473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem in a simple program

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
Jan 15 '09 #1
12 2554
r035198x
13,262 8TB
What exception do you get at what line number? Post the line that is reported for the exception here.
Jan 16 '09 #2
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...
Jan 17 '09 #3
JosAH
11,448 Expert 8TB
@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
Jan 17 '09 #4
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 !!!!!!!!!!!!!!!!!!!!!!!!
Jan 18 '09 #5
JosAH
11,448 Expert 8TB
@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
Jan 18 '09 #6
ok , here take , this which was at the first link , my main program , three files :

methodsForEncAndDec.java :
Expand|Select|Wrap|Line Numbers
  1. package trying;
  2.  
  3. import java.util.*;
  4. import java.io.*;
  5. import java.security.*;
  6. import java.security.spec.*;
  7. import javax.crypto.*;
  8. import javax.crypto.spec.*;
  9. import java.lang.System.*;
  10. import java.lang.*;
  11.  
  12.  
  13.  
  14. public class methodsForEncAndDec{
  15.  
  16.     public static int msglength;
  17.     public static KeyPair key ;
  18.     public static String AESinput_key;
  19.  
  20.  
  21.     public methodsForEncAndDec(){}
  22.  
  23.  
  24.  
  25. public static byte[] SHA_hash(String text)
  26. {
  27. try{
  28.     //hash
  29.     MessageDigest md = MessageDigest.getInstance("SHA");
  30.     byte[] hash_text = md.digest(text.getBytes());
  31.     //print
  32. /*    String hs = new sun.misc.BASE64Encoder().encode(hash_text);
  33.     System.out.println("---> hash: <---");
  34.     System.out.println(hs);*/
  35.     return hash_text;
  36.     }
  37. catch( Exception e )
  38.     {
  39.       e.printStackTrace();
  40.     }
  41.     return null;
  42. }
  43. //#############################################################
  44. public static byte[] AES_encrypt(byte[] plain_Text,String key)
  45. {
  46. try{
  47.     // Key
  48.     byte[] byte_key  = key.getBytes();
  49.     SecretKeySpec final_key = new SecretKeySpec(byte_key,"AES");
  50.  
  51.     // ENCRYPTION
  52.     Cipher m_encrypter = Cipher.getInstance("AES/ECB/PKCS5Padding");
  53.     m_encrypter.init(Cipher.ENCRYPT_MODE , final_key);
  54.     byte[] encrypted_Text = m_encrypter.doFinal(plain_Text);
  55.  
  56.     // unhide this if you want to see the sent message      
  57.         /*System.out.println("---> After Encryption with AES: <---");
  58.     for (int i=0 ; i<encrypted_Text.length ; i++)
  59.     System.out.println((char)encrypted_Text[i]);
  60.     */
  61.  
  62.     return encrypted_Text;
  63.     }
  64.     //catch exceptions  
  65. catch( Exception e )
  66.     {
  67.       e.printStackTrace();
  68.     }
  69.     return null;
  70. }    
  71. //#############################################################
  72. public static byte[] AES_decrypt(byte[] encrypted_Text,String key)
  73. {
  74. try{
  75.     // Key
  76.     byte[] byte_key  = key.getBytes();
  77.     SecretKeySpec final_key = new SecretKeySpec(byte_key,"AES");
  78.  
  79.     // DECRYPTION
  80.     Cipher m_decrypter = Cipher.getInstance("AES/ECB/PKCS5Padding");
  81.     m_decrypter.init(Cipher.DECRYPT_MODE , final_key);
  82.     byte[] decrypted_Text = m_decrypter.doFinal(encrypted_Text);
  83.  
  84.     // Print
  85. /*    String dt = new sun.misc.BASE64Encoder().encode(decrypted_Text);
  86.     System.out.println("---> After Decryption with AES: <---");
  87.     System.out.println(dt);*/
  88.  
  89.     return decrypted_Text;
  90.     }
  91.     //catch exceptions  
  92. catch( Exception e )
  93.     {
  94.       e.printStackTrace();
  95.     }
  96.     return null;
  97. }    
  98. //#############################################################
  99. public static byte[] byte_concat(byte[] a , byte[] b)
  100. {
  101.     byte[] x = new byte[a.length + b.length];    
  102.     for(int i=0 ; i<a.length ; i++)
  103.     {
  104.         x[i]=a[i];
  105.     }
  106.     for(int j=a.length ; j<x.length ; j++)
  107.     {
  108.         x[j]=b[j-a.length];
  109.     }
  110.     return x;
  111. }
  112. //#############################################################
  113. public static byte[] byte_deconcat(byte[] a)
  114. {
  115.     //20 byte = 160bit = length of SHA hash to be deconcat
  116.     byte[] x = new byte[a.length - 20];    
  117.     for(int i=0 ; i<x.length ; i++)
  118.     {
  119.         x[i]=a[i];
  120.     }
  121.     //print
  122.     System.out.println("---> After de-concat : <---");
  123.     for (int i=0 ; i<x.length ; i++)
  124.     {
  125.         System.out.print((char)x[i]);
  126.     }
  127.     System.out.println("");
  128.     return x;
  129. }
  130.  
  131. }
  132.  
UDPServer.java :
Expand|Select|Wrap|Line Numbers
  1. package trying;
  2.  
  3.  
  4. import java.io.*; 
  5. import java.net.*; 
  6. import javax.swing.*;
  7. import java.util.*;
  8. import java.security.*;
  9. import java.security.spec.*;
  10. import javax.crypto.*;
  11. import javax.crypto.spec.*;
  12. import java.lang.System.*;
  13. import java.lang.*;
  14.  
  15.  
  16. public class UDPServer{
  17.  
  18.  
  19.  
  20.     public static void main(String args[]){ 
  21.  
  22.         //Create receiver Socket
  23.         DatagramSocket aSocket = null;
  24.  
  25.  
  26.         try{
  27.             //1.create socket at agreed port
  28.             aSocket = new DatagramSocket(1234); 
  29.  
  30.  
  31.             //2. Define Receeiver Packet Size Size 
  32.             byte[] buffer = new byte[methodsForEncAndDec.msglength];
  33.  
  34.  
  35.  
  36. Provider sunjce = new com.sun.crypto.provider.SunJCE();
  37. Security.addProvider(sunjce);    
  38.  
  39.  
  40.  
  41.          // 3. Listening....
  42.              while(true){
  43.                  DatagramPacket request = new DatagramPacket(buffer, buffer.length);
  44.                   aSocket.receive(request); 
  45.  
  46.  
  47.  
  48.  
  49. //-------------------------------  Decryption operation with AES--------------------------------------------------------------------
  50.  
  51.  
  52.  
  53.     String recieved=new String(request.getData()).trim();
  54.  
  55.     char[] AES_encrypted_C=recieved.toCharArray();
  56.  
  57.     byte[] AES_encrypted_B=new byte[AES_encrypted_C.length];
  58.  
  59. for (int i=0 ; i<AES_encrypted_C.length ; i++)
  60.     AES_encrypted_B[i]=(byte)AES_encrypted_C[i];
  61.  
  62.  
  63.  
  64.     // unhide the following(lines:66-68) if you want to see that the received message is as same as the sent one     
  65.     /*
  66.     System.out.println("---> After Recieving: <---");
  67.     for (int i=0 ; i<AES_encrypted_B.length ; i++)
  68.     System.out.println((char)AES_encrypted_B[i]);
  69.     */
  70.  
  71.  
  72.     byte [] AES_decrypted = methodsForEncAndDec.AES_decrypt(AES_encrypted_B,methodsForEncAndDec.AESinput_key);
  73.  
  74.  
  75.  
  76. //---------------------------------------------------------------------------------------------------------------------------------------------               
  77.  
  78.            System.out.println("Client send: ");
  79.  
  80.     byte [] AES_without_hash =methodsForEncAndDec.byte_deconcat(AES_decrypted);
  81.  
  82.  
  83.             }
  84.         }catch (SocketException e){System.out.println("Socket: " + e.getMessage());
  85.         }catch (IOException e) {System.out.println("IO: " + e.getMessage());
  86.         }finally {if(aSocket != null) aSocket.close();}
  87.     }
  88. }
  89.  
UDPClient.java :
Expand|Select|Wrap|Line Numbers
  1. package trying;
  2.  
  3. import java.io.*; 
  4. import java.net.*; 
  5. import javax.swing.*;
  6. import java.util.*;
  7. import java.security.*;
  8. import java.security.spec.*;
  9. import javax.crypto.*;
  10. import javax.crypto.spec.*;
  11. import java.lang.System.*;
  12. import java.lang.*;
  13.  
  14.  
  15.  
  16. public class UDPClient{
  17.  
  18.  
  19.  
  20.  
  21.     public static void main(String args[]){ 
  22.  
  23.         DatagramSocket aSocket = null;
  24.  
  25.  
  26.         try {
  27.  
  28.             // 1. Create Client Socket
  29.             aSocket = new DatagramSocket(); 
  30.  
  31.  
  32.  
  33. //-------------------------------  Encryption operation with AES--------------------------------------------------------------------
  34.  
  35. //Declare SunJCE provider.
  36.     Provider sunjce = new com.sun.crypto.provider.SunJCE();
  37.       Security.addProvider(sunjce);
  38.  
  39.     //Get Plaintext
  40.     Scanner my_object = new Scanner(System.in);
  41.     System.out.println("---> Enter the plainText: <---");
  42.     String plain = my_object.nextLine();
  43.  
  44.     //Hashing then Concatenating
  45.     byte[] hash = methodsForEncAndDec.SHA_hash(plain);
  46.     byte[] msg_with_hash = methodsForEncAndDec.byte_concat(plain.getBytes() , hash);
  47.  
  48.  
  49.  
  50.     //Get AES key
  51.     System.out.println("---> Enter 16 characters key: <---");
  52.     methodsForEncAndDec.AESinput_key/*String kyy */= my_object.nextLine();
  53.  
  54.     //AES encrypt
  55.     byte [] AES_encrypted = methodsForEncAndDec.AES_encrypt(msg_with_hash,methodsForEncAndDec.AESinput_key/*kyy*/);
  56.  
  57. //convert into character array        
  58. char[] AES_encrypted_C=new char[AES_encrypted.length];
  59. for (int i=0 ; i<AES_encrypted.length ; i++)
  60.         AES_encrypted_C[i]=(char)AES_encrypted[i];
  61.  
  62. //convert into string
  63. String AES_encrypted_S=new String(AES_encrypted_C);
  64.  
  65. //then convert the string into array of bytes to be sent
  66. byte[] AES_finalencrypted=AES_encrypted_S.getBytes();
  67.  
  68.  
  69. //---------------------------------------------------------------------------------------------------------------------------------------------        
  70.  
  71.  
  72.  
  73.  
  74.             //3.Define the server IP..    
  75.                InetAddress aHost = InetAddress.getByName("localhost");//master
  76.  
  77.  
  78.                //4. Attatch the socket to the Server Port..
  79.             int serverPort =1234;    
  80.  
  81.  
  82.  
  83.             //5. define the packet to be sent to the server ...                                                     
  84.             DatagramPacket request =
  85.                  new DatagramPacket(AES_finalencrypted,AES_encrypted_S.length(), aHost, serverPort);
  86.  
  87.  
  88.             //6. Send the Packet to the server...     
  89.             aSocket.send(request);
  90.  
  91.  
  92.         methodsForEncAndDec.msglength=AES_finalencrypted.length;
  93.  
  94.  
  95.         }catch (SocketException e){System.out.println("Socket: " + e.getMessage());
  96.         }catch (IOException e){System.out.println("IO: " + e.getMessage());
  97.         }finally {if(aSocket != null) aSocket.close();}
  98.     }                  
  99. }
  100.  
and this which was at the second link :

AES_SHA_RSA.java :
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.io.*;
  3. import java.security.*;
  4. import java.security.spec.*;
  5. import javax.crypto.*;
  6. import javax.crypto.spec.*;
  7. import java.lang.System.*;
  8. import java.lang.*;
  9.  
  10. /*Note 1 : I use 'Base64' to display cipher texts in terms
  11. //of hex digits only , If you don't want to use it , you can
  12. //do this : Scanner.out.println(new String(cipher_Text));
  13.  
  14. //Note 2 : input msg cannot be very long , because i don't
  15. //use update() yet, if you want to input a msg with any
  16. //length then use update() in conjunction with doFinal()
  17.  
  18. //Note 3 : it may be better to build a new function to initia-
  19. //-lize the ciphers , i.e. to put the .getInstance() constru-
  20. //-ctors there , so there will be only one instance of every
  21. //cipher...
  22.  
  23. //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.
  24. */
  25.  
  26. public class AES_SHA_RSA
  27. {
  28. public static void main(String args[]) 
  29. {
  30.     //Declare SunJCE provider.
  31.     Provider sunjce = new com.sun.crypto.provider.SunJCE();
  32.       Security.addProvider(sunjce);
  33.  
  34.     //Get Plaintext
  35.     Scanner my_object = new Scanner(System.in);
  36.     System.out.println("---> Enter the plainText: <---");
  37.     String plain = my_object.nextLine();
  38.  
  39.     //Hashing then Concatenating
  40.     byte[] hash = SHA_hash(plain);
  41.     byte[] msg_with_hash = byte_concat(plain.getBytes() , hash);
  42.  
  43.     //Get AES key
  44.     System.out.println("---> Enter the 128-bit key: <---");
  45.     String input_key = my_object.nextLine();
  46.  
  47.     //AES en(de)crypt
  48.     byte [] AES_encrypted = AES_encrypt(msg_with_hash,input_key);
  49.     byte [] AES_decrypted = AES_decrypt(AES_encrypted,input_key);
  50.     byte [] AES_without_hash = byte_deconcat(AES_decrypted);
  51.  
  52. }//end 
  53.  
  54.  
  55.  
  56.  
  57. //#############################################################
  58. public static byte[] SHA_hash(String text)
  59. {
  60. try{
  61.     //hash
  62.     MessageDigest md = MessageDigest.getInstance("SHA");
  63.     byte[] hash_text = md.digest(text.getBytes());
  64.     //print
  65.     String hs = new sun.misc.BASE64Encoder().encode(hash_text);
  66.     System.out.println("---> hash: <---");
  67.     System.out.println(hs);
  68.     return hash_text;
  69.     }
  70. catch( Exception e )
  71.     {
  72.       e.printStackTrace();
  73.     }
  74.     return null;
  75. }
  76. //#############################################################
  77. public static byte[] AES_encrypt(byte[] plain_Text,String key)
  78. {
  79. try{
  80.     // Key
  81.     byte[] byte_key  = key.getBytes();
  82.     SecretKeySpec final_key = new SecretKeySpec(byte_key,"AES");
  83.  
  84.     // ENCRYPTION
  85.     Cipher m_encrypter = Cipher.getInstance("AES/ECB/PKCS5Padding");
  86.     m_encrypter.init(Cipher.ENCRYPT_MODE , final_key);
  87.     byte[] encrypted_Text = m_encrypter.doFinal(plain_Text);
  88.  
  89.     // Print
  90.     String ct = new sun.misc.BASE64Encoder().encode(encrypted_Text);
  91.     System.out.println("---> After Encryption with AES: <---");
  92.     System.out.println(ct);
  93.  
  94.     return encrypted_Text;
  95.     }
  96.     //catch exceptions  
  97. catch( Exception e )
  98.     {
  99.       e.printStackTrace();
  100.     }
  101.     return null;
  102. }    
  103. //#############################################################
  104. public static byte[] AES_decrypt(byte[] encrypted_Text,String key)
  105. {
  106. try{
  107.     // Key
  108.     byte[] byte_key  = key.getBytes();
  109.     SecretKeySpec final_key = new SecretKeySpec(byte_key,"AES");
  110.  
  111.     // DECRYPTION
  112.     Cipher m_decrypter = Cipher.getInstance("AES/ECB/PKCS5Padding");
  113.     m_decrypter.init(Cipher.DECRYPT_MODE , final_key);
  114.     byte[] decrypted_Text = m_decrypter.doFinal(encrypted_Text);
  115.  
  116.     // Print
  117.     String dt = new sun.misc.BASE64Encoder().encode(decrypted_Text);
  118.     System.out.println("---> After Decryption with AES: <---");
  119.     System.out.println(dt);
  120.  
  121.     return decrypted_Text;
  122.     }
  123.     //catch exceptions  
  124. catch( Exception e )
  125.     {
  126.       e.printStackTrace();
  127.     }
  128.     return null;
  129. }    
  130. //#############################################################
  131. public static byte[] byte_concat(byte[] a , byte[] b)
  132. {
  133.     byte[] x = new byte[a.length + b.length];    
  134.     for(int i=0 ; i<a.length ; i++)
  135.     {
  136.         x[i]=a[i];
  137.     }
  138.     for(int j=a.length ; j<x.length ; j++)
  139.     {
  140.         x[j]=b[j-a.length];
  141.     }
  142.     return x;
  143. }
  144. //#############################################################
  145. public static byte[] byte_deconcat(byte[] a)
  146. {
  147.     //20 byte = 160bit = length of SHA hash to be deconcat
  148.     byte[] x = new byte[a.length - 20];    
  149.     for(int i=0 ; i<x.length ; i++)
  150.     {
  151.         x[i]=a[i];
  152.     }
  153.     //print
  154.     System.out.println("---> After de-concat : <---");
  155.     for (int i=0 ; i<x.length ; i++)
  156.     {
  157.         System.out.print((char)x[i]);
  158.     }
  159.     System.out.println("");
  160.     return x;
  161. }
  162.  
  163. }
  164.  
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 .
Jan 21 '09 #7
r035198x
13,262 8TB
@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.
Jan 21 '09 #8
JosAH
11,448 Expert 8TB
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
Jan 21 '09 #9
I'm missing line numbers, what line is exactly 83?
Jan 23 '09 #10
I'm very sorry for late , here is the line :


SecretKeySpec final_key = new SecretKeySpec(byte_key,"AES");

thanks.
Jan 26 '09 #11
???????????????????????????????????????
Feb 26 '09 #12
jkmyoung
2,057 Expert 2GB
Try adding the following lines before that line:

Expand|Select|Wrap|Line Numbers
  1. if ( byte_key == null){
  2. System.out.println("My program will die here");
  3. }
  4.  

Also please use code tags. eg [ code ] without the spaces [ / code]
Feb 26 '09 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

0
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...
38
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 =...
5
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...
117
by: Peter Olcott | last post by:
www.halting-problem.com
8
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...
5
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...
5
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...
9
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...
3
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...
6
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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
0
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...
0
marktang
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,...
0
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...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.