Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modify code so it doesn't use a random key

Status
Not open for further replies.

jamescpp

IS-IT--Management
Aug 29, 2001
70
US
I posted to java.sun.com a few hours ago and haven't gotten any responses. Can anyone help me modify this program so it doesn't use a random key? I want to decrypt the password in another program, so I don't want to have to read a key file.

I've seen some forums posts doing something similar, but so far I haven't been able to successfully change mine. I don't think it would take much.

The goal is so I'm not storing a test password in an ini file that anyone can open up and see. I store the encrypted one, and then the program can decrypt it.

Anyway, any help is appreciated.
James

Here's the Encrypt code:
import java.io.*;
import java.security.*;
import javax.crypto.*;
import sun.misc.*;

public class EncryptPassword
{

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

if(args.length<1)
{
System.out.println(&quot;Usage : EncryptPassword text&quot;);
return;
}

Security.addProvider(new com.sun.crypto.provider.SunJCE());

Key key;

try
{
ObjectInputStream in=new ObjectInputStream(new FileInputStream(&quot;des.key&quot;));
key=(Key)in.readObject();
in.close();
} catch(FileNotFoundException fnfe)
{
KeyGenerator generator= KeyGenerator.getInstance(&quot;DES&quot;);
generator.init(new SecureRandom() );
key=generator.generateKey();
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(&quot;des.key&quot;));
out.writeObject(key);
out.close();
}


KeyGenerator generator= KeyGenerator.getInstance(&quot;DES&quot;);
generator.init(new SecureRandom() );
key=generator.generateKey();
Cipher cipher=Cipher.getInstance(&quot;DES/ECB/PKCS5Padding&quot;);

System.out.println(key);
cipher.init(Cipher.ENCRYPT_MODE,key);
String amalgam=args[0];
for(int i=2;i<args.length;i++)
amalgam+=&quot; &quot;+args;

byte[] stringBytes=amalgam.getBytes(&quot;UTF8&quot;);
byte[] raw=cipher.doFinal(stringBytes);
BASE64Encoder encoder = new BASE64Encoder();
String base64 = encoder.encode(raw);
System.out.println(base64);

}
}

Here's the decrypt code:
import java.io.*;
import java.security.*;
import javax.crypto.*;
import sun.misc.*;

public class DecryptPassword
{

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

if(args.length<1)
{
System.out.println(&quot;Usage : DecryptPassword text&quot;);
return;
}

Security.addProvider(new com.sun.crypto.provider.SunJCE());

Key key;

try
{
ObjectInputStream in=new ObjectInputStream(new FileInputStream(&quot;des.key&quot;));
key=(Key)in.readObject();
in.close();
} catch(FileNotFoundException fnfe)
{
KeyGenerator generator= KeyGenerator.getInstance(&quot;DES&quot;);
generator.init(new SecureRandom() );
key=generator.generateKey();
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(&quot;des.key&quot;));
out.writeObject(key);
out.close();
}


KeyGenerator generator= KeyGenerator.getInstance(&quot;DES&quot;);
generator.init(new SecureRandom() );
key=generator.generateKey();
Cipher cipher=Cipher.getInstance(&quot;DES/ECB/PKCS5Padding&quot;);


cipher.init(Cipher.DECRYPT_MODE,key);
BASE64Decoder decoder = new BASE64Decoder();
byte[] raw = decoder.decodeBuffer(args[0]);
byte[] stringBytes = cipher.doFinal(raw);
String result = new String(stringBytes,&quot;UTF8&quot;);
System.out.println(result);

}
}
 
I'm not really sure about this kind of thing... but is it serializable? Might you be able to serialize the key and pass it on? <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
Don't know. First time I've tried to do this crypto stuff. Never worked with serializable before either. So I guess I REALLY don't know... :)

James
 
I have had the best luck using some of the free des libs
laying around online. The one I've been using is in c so
you use them through jni but in the long run I've found
that this is a much better solution since encryption can
be slow if you are crunching a lot of data. Not really
a problem for you. Try this link
Try the &quot;DES-Barr&quot; package. It's easy to use and has a
utility to crunch a char array to a 128 bit key for you.
You just feed in a string and it hashes out a key from it.
If you need code let me know. I don't think the des should
be tough. It's very straight forward.
 
Thanks for the help here. I was able to get the code modified appropriately using DESKeySpec.

James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top