• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 448
  • Last Modified:

New to JAVA - Encryption program.

Hello,

This program encrypts and decrypts text using the Data Encryption Standard. I am unsure if it is working correctly as I am having some trouble with my Sun software. It should be fine.. Could someone please run the code and let me know. I would really appreciate it.  

Thanks,
Sarah
import java.io.*;
//The Java Security API
import java.security.*;
//The Java Security API
import javax.crypto.*;
import sun.misc.*;
 
 
//The Java Security API is a set of packages that are used for writing
secure programs in Java.
public class InterNetworkSecurityAssignmentOne {
public static void main(String[] args) throws Exception {
// Check command-line arguments. -e or -d, & a string.
if (args.length < 2) {
// -e will encrypt data, -d decrypts it.
System.out.println("Usage: InterNetworkSecurityAssignmentOne -e|-d text");
//
return;
}
// Get or create key.
Key key;
try {
ObjectInputStream in = new ObjectInputStream(
new FileInputStream("AssignmentOneKey.ser"));
key = (Key)in.readObject();
in.close();
}
catch (FileNotFoundException fnfe) {
//Symmetric ciphers use a single key instead of a key pair. The JCE
has a class called
//javax.crypto.KeyGenerator that can randomly generate a single key.
//Here I have created it using a getInstance().
//Next I asked for a key for the DES cipher.
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom());// pseudo-random number generator PRNG?
key = generator.generateKey();
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("AssignmentOneKey.ser"));
out.writeObject(key);
out.close();
}
// Get a cipher object.
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// Encrypt or decrypt the input string.
if (args[0].indexOf("e") != -1) {
cipher.init(Cipher.ENCRYPT_MODE, key);
String amalgam = args[1];
for (int i = 2; i < args.length; i++)
amalgam += " " + args[i];
byte[] stringBytes = amalgam.getBytes("UTF8");
byte[] raw = cipher.doFinal(stringBytes);
BASE64Encoder encoder = new BASE64Encoder();
String base64 = encoder.encode(raw);
System.out.println(base64);
}
else if (args[0].indexOf("d") != -1) {
cipher.init(Cipher.DECRYPT_MODE, key);
BASE64Decoder decoder = new BASE64Decoder();
byte[] raw = decoder.decodeBuffer(args[1]);
byte[] stringBytes = cipher.doFinal(raw);
String result = new String(stringBytes, "UTF8");
System.out.println(result);
}
}
}

Open in new window

0
Sarah_B
Asked:
Sarah_B
  • 4
  • 2
1 Solution
 
contactkarthiCommented:
seems to work fine

as i use and IDE just created string array to test it

//String[] args = {"-e","InterNetworkSecurityAssignmentOne"};
            String[] args = {"-d","gnhP1ey3XP3DnoxpyzXUi5x+4CF2Dx3ejgTTet0BZsMrXzWjPvu1qA=="};

comment one the line to do what you want

import java.io.*;
//The Java Security API
import java.security.*;
//The Java Security API
import javax.crypto.*;
import sun.misc.*;
 
 
//The Java Security API is a set of packages that are used for writing secure programs in Java.
public class InterNetworkSecurityAssignmentOne {
	public static void main(String[] args1) throws Exception {
//		Check command-line arguments. -e or -d, & a string.
		//String[] args = {"-e","InterNetworkSecurityAssignmentOne"};
		String[] args = {"-d","gnhP1ey3XP3DnoxpyzXUi5x+4CF2Dx3ejgTTet0BZsMrXzWjPvu1qA=="};
		if (args.length < 2) {
//			-e will encrypt data, -d decrypts it.
			System.out.println("Usage: InterNetworkSecurityAssignmentOne -e|-d text");
 
			return;
		}
//		Get or create key.
		Key key;
		try {
			ObjectInputStream in = new ObjectInputStream(
					new FileInputStream("AssignmentOneKey.ser"));
			key = (Key)in.readObject();
			in.close();
		}
		catch (FileNotFoundException fnfe) {
//			Symmetric ciphers use a single key instead of a key pair. The JCE has a class called
//			javax.crypto.KeyGenerator that can randomly generate a single key.
//			Here I have created it using a getInstance().
//			Next I asked for a key for the DES cipher.
			KeyGenerator generator = KeyGenerator.getInstance("DES");
			generator.init(new SecureRandom());// pseudo-random number generator PRNG?
			key = generator.generateKey();
			ObjectOutputStream out = new ObjectOutputStream(
					new FileOutputStream("AssignmentOneKey.ser"));
			out.writeObject(key);
			out.close();
		}
//		Get a cipher object.
		Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
//		Encrypt or decrypt the input string.
		if (args[0].indexOf("e") != -1) {
			cipher.init(Cipher.ENCRYPT_MODE, key);
			String amalgam = args[1];
			for (int i = 2; i < args.length; i++)
				amalgam += " " + args[i];
			byte[] stringBytes = amalgam.getBytes("UTF8");
			byte[] raw = cipher.doFinal(stringBytes);
			BASE64Encoder encoder = new BASE64Encoder();
			String base64 = encoder.encode(raw);
			System.out.println(base64);
		}
		else if (args[0].indexOf("d") != -1) {
			cipher.init(Cipher.DECRYPT_MODE, key);
			BASE64Decoder decoder = new BASE64Decoder();
			byte[] raw = decoder.decodeBuffer(args[1]);
			byte[] stringBytes = cipher.doFinal(raw);
			String result = new String(stringBytes, "UTF8");
			System.out.println(result);
		}
	}
}

Open in new window

0
 
Sarah_BAuthor Commented:
contactkarth,

Thanks very much for your swift response. Could you possibly attach the jar file to a comment. I am still having trouble at this end. This would solve the problem for me completely.

Thanks, Sarah.
0
 
contactkarthiCommented:
i didnt use any jars.. i just pasted the code in eclipse and ran it..

what is the problem you are facing. can you post the error.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Sarah_BAuthor Commented:
contactkarthi,

I'm afraid the problem is i'm just very new to Java programming. I've downloaded all sun software but am confused to say the least. I found this code implementing DES and need to just obtain the jar file to complete a report for college but i just don't know how. I know it's simple i guess but not for me! I'd appreciate any further help you could give.

Thanks, Sarah.
0
 
contactkarthiCommented:
you need to have rt.jar in class path

that has the encoder and decoder in it
0
 
contactkarthiCommented:
rt.jar should be in a folder similar to this

C:\Program Files\Java\jre1.6.0_07\lib
0

Featured Post

When ransomware hits your clients, what do you do?

MSPs: Endpoint security isn’t enough to prevent ransomware.
As the impact and severity of crypto ransomware attacks has grown, Webroot fought back, not just by building a next-gen endpoint solution capable of preventing ransomware attacks but also by being a thought leader.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now