11 Replies - 9169 Views - Last Post: 13 April 2009 - 03:12 AM Rate Topic: -----

#1 jbskater2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-February 09

DES encryption program URGENT!

Posted 12 April 2009 - 08:32 PM

I am working on a java program to encrypt and decrypt a 8 charater word With DES.
Here is what I have. Here is the site I have been using as a resource. The program compiles but Im getting random errors hen trying to encrypt and decrypt! need a fast reply thank you.

import java.io.*;
import java.util.*;
import java.text.*;
import java.awt.*;
import javax.swing.*;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.security.*;
import java.security.spec.*;
import java.security.interfaces.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
import com.sun.crypto.provider.SunJCE;

public class DES


{
 	public static void main(String [] args) throws Exception



		{

			Cipher desCipher;
			int deskey;
			int choice;
			String input;
			String inputString;
			Boolean x = true;


			KeyGenerator keygen = KeyGenerator.getInstance("DES");
										SecretKey desKey = keygen.generateKey();

		while	(x= true)
		{
			inputString = JOptionPane.showInputDialog("Choose 1. for Encrypt, 2. for Decrypt, 3. to quit.");
			choice = Integer.parseInt(inputString);




			if(choice == 1)

				{



				inputString = JOptionPane.showInputDialog("Enter Text to be encrypted");


					// Create the cipher
					desCipher = Cipher.getInstance("DES");



				   		 // Initialize the cipher for encryption
						desCipher.init(Cipher.ENCRYPT_MODE, desKey);

						// Our cleartext
						byte[] cleartext = inputString.getBytes();

						// Encrypt the cleartext
						byte[] ciphertext = desCipher.doFinal(cleartext);

						JOptionPane.showMessageDialog(null, " Encrypted text is " + ciphertext );

						// Initialize the same cipher for decryption
						desCipher.init(Cipher.DECRYPT_MODE, desKey);

						// Decrypt the ciphertext
						byte[] cleartext1 = desCipher.doFinal(ciphertext);


					  



					}

				else if(choice == 2)
				{
					inputString = JOptionPane.showInputDialog("Enter Text to be decrypted");


										// Create the cipher
										desCipher = Cipher.getInstance("DES");


					 // Initialize the same cipher for decryption
						desCipher.init(Cipher.DECRYPT_MODE, desKey);

						byte[] ciphertext2 = inputString.getBytes();

											// Decrypt the ciphertext
						byte[] cleartext1 = desCipher.doFinal(ciphertext2);
						JOptionPane.showMessageDialog(null, " Decrypted text is " + ciphertext2 );


				}

				 else if (choice >= 3)
				{
					JOptionPane.showMessageDialog(null, " bye!" );
					System.exit(0);
				}
	}

	}



}

This post has been edited by jbskater2004: 12 April 2009 - 08:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: DES encryption program URGENT!

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: DES encryption program URGENT!

Posted 12 April 2009 - 08:33 PM

What type of random errors ?
Was This Post Helpful? 0
  • +
  • -

#3 jbskater2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-February 09

Re: DES encryption program URGENT!

Posted 12 April 2009 - 08:38 PM

View Postpbl, on 12 Apr, 2009 - 07:33 PM, said:

What type of random errors ?



When I encrypt a word i get an output that always starts with [[email protected] Which may be normal? i duno it generating a random key. But when i try to decrypt it it errors saying that it is the wrong length. Also i noticed its using the same function both ways duno if that is normal or not.

This post has been edited by jbskater2004: 12 April 2009 - 08:39 PM

Was This Post Helpful? 0
  • +
  • -

#4 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: DES encryption program URGENT!

Posted 12 April 2009 - 08:52 PM

I think i know what it is, just not how to fix it


I think that [[email protected] is where the string is in the ram.

if you do a to string to a undeclared array, it will only tell you where it is in the ram, i think that this is the same problem

This post has been edited by virgul: 12 April 2009 - 08:53 PM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: DES encryption program URGENT!

Posted 12 April 2009 - 09:17 PM

View Postjbskater2004, on 12 Apr, 2009 - 07:38 PM, said:

View Postpbl, on 12 Apr, 2009 - 07:33 PM, said:

What type of random errors ?



When I encrypt a word i get an output that always starts with [[email protected] Which may be normal? i duno it generating a random key. But when i try to decrypt it it errors saying that it is the wrong length. Also i noticed its using the same function both ways duno if that is normal or not.

At which line in your code do you output that word ?
As Virgul says looks like writting an object that does not have a toString() method or an array of objects instead of a single one
Was This Post Helpful? 0
  • +
  • -

#6 jbskater2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-February 09

Re: DES encryption program URGENT!

Posted 12 April 2009 - 09:21 PM

possibly. Im not sure how to fix that either but it would make sense with the weird output i am getting. SO would I need to take the part where i print out the var and convert it first?any idea on how i could do that?

This post has been edited by jbskater2004: 12 April 2009 - 09:24 PM

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: DES encryption program URGENT!

Posted 12 April 2009 - 09:22 PM

View Postjbskater2004, on 12 Apr, 2009 - 08:21 PM, said:

possibly. Im not sure how to fix that either but it would make sense with the weird output i am getting.

Which line ?
Was This Post Helpful? 0
  • +
  • -

#8 jbskater2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-February 09

Re: DES encryption program URGENT!

Posted 12 April 2009 - 09:34 PM

View Postpbl, on 12 Apr, 2009 - 08:22 PM, said:

View Postjbskater2004, on 12 Apr, 2009 - 08:21 PM, said:

possibly. Im not sure how to fix that either but it would make sense with the weird output i am getting.

Which line ?




"JOptionPane.showMessageDialog(null, " Decrypted text is " + ciphertext2 );"

65 and 101

View Postjbskater2004, on 12 Apr, 2009 - 08:29 PM, said:

View Postpbl, on 12 Apr, 2009 - 08:22 PM, said:

View Postjbskater2004, on 12 Apr, 2009 - 08:21 PM, said:

possibly. Im not sure how to fix that either but it would make sense with the weird output i am getting.

Which line ?




"JOptionPane.showMessageDialog(null, " Decrypted text is " + ciphertext2 );"

65 and 101


so i need some whay to print the data in the var and not the mem location of ciphertext. i guess it is and array so thats the problem im printing an array and is giving me the memory location so i need it to print each var array.
doh im so dumb
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: DES encryption program URGENT!

Posted 12 April 2009 - 10:11 PM

byte[] ciphertext2

Yes it is an array
so that is where your "garbadge" is coming from
Was This Post Helpful? 0
  • +
  • -

#10 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: DES encryption program URGENT!

Posted 12 April 2009 - 10:22 PM

wow, does that mean that i was correct, sweet, if you get a working version of this can i see it? i want to see it in action!
Was This Post Helpful? 1
  • +
  • -

#11 jbskater2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-February 09

Re: DES encryption program URGENT!

Posted 12 April 2009 - 11:17 PM

View Postvirgul, on 12 Apr, 2009 - 09:22 PM, said:

wow, does that mean that i was correct, sweet, if you get a working version of this can i see it? i want to see it in action!



yea, garabageis gone now but Im not sure how to display the output right to the user I just wrote out each array element to a joptionpane but that could be limiting it. Also sorry about all the horrid typos. This was a homework assignment my dad had to do and i was trying to help him out. It was due at midnight. Im going to continue to finish it but if anyone know how I can display the out puts in a neat fashion it would be awesome. Im also stil getting some out of bounds exceptions when i try to decrypt the encrypted word. Its using a random key generator also which makes it harder for me to compare the input and outputs. Most programs that are similar hardcode a lot of the actual DES cycles which is a lot of code and a lot of time. Also when I merged what I had into his programs it messed up the exception handling and i cant seem to figureout how to handle the exceptions. So currently my portion kills his over all program. The over all program is a GUI based program that you select from a list of encryption schemes and can decrypt and encrypt data. Tough stuff for us cuz we are real java savvy.

This post has been edited by jbskater2004: 12 April 2009 - 11:26 PM

Was This Post Helpful? 0
  • +
  • -

#12 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: DES encryption program URGENT!

Posted 13 April 2009 - 03:12 AM

And here I was thinking that you were implementing your own DES. :(


Anyways, this line,
while	(x= true)



is always going to be true as assigning values to a variable is always true.
Use == to compare two values of primitive types.

However when you check the value of a boolean, you may just write,

while	(x)



as it would be interpreted as while (true), give it a try.

Oh and in case you were thinking of using this application for encryption/decryption, be aware that DES is no longer considered to be secure so don't use it for anything important.

This post has been edited by Gloin: 13 April 2009 - 03:17 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1