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("Usage : EncryptPassword text"
return;
}
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key;
try
{
ObjectInputStream in=new ObjectInputStream(new FileInputStream("des.key");
key=(Key)in.readObject();
in.close();
} catch(FileNotFoundException fnfe)
{
KeyGenerator generator= KeyGenerator.getInstance("DES"
generator.init(new SecureRandom() );
key=generator.generateKey();
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("des.key");
out.writeObject(key);
out.close();
}
KeyGenerator generator= KeyGenerator.getInstance("DES"
generator.init(new SecureRandom() );
key=generator.generateKey();
Cipher cipher=Cipher.getInstance("DES/ECB/PKCS5Padding"
System.out.println(key);
cipher.init(Cipher.ENCRYPT_MODE,key);
String amalgam=args[0];
for(int i=2;i<args.length;i++)
amalgam+=" "+args;
byte[] stringBytes=amalgam.getBytes("UTF8"
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("Usage : DecryptPassword text"
return;
}
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key;
try
{
ObjectInputStream in=new ObjectInputStream(new FileInputStream("des.key");
key=(Key)in.readObject();
in.close();
} catch(FileNotFoundException fnfe)
{
KeyGenerator generator= KeyGenerator.getInstance("DES"
generator.init(new SecureRandom() );
key=generator.generateKey();
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("des.key");
out.writeObject(key);
out.close();
}
KeyGenerator generator= KeyGenerator.getInstance("DES"
generator.init(new SecureRandom() );
key=generator.generateKey();
Cipher cipher=Cipher.getInstance("DES/ECB/PKCS5Padding"
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,"UTF8"
System.out.println(result);
}
}
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("Usage : EncryptPassword text"
return;
}
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key;
try
{
ObjectInputStream in=new ObjectInputStream(new FileInputStream("des.key");
key=(Key)in.readObject();
in.close();
} catch(FileNotFoundException fnfe)
{
KeyGenerator generator= KeyGenerator.getInstance("DES"
generator.init(new SecureRandom() );
key=generator.generateKey();
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("des.key");
out.writeObject(key);
out.close();
}
KeyGenerator generator= KeyGenerator.getInstance("DES"
generator.init(new SecureRandom() );
key=generator.generateKey();
Cipher cipher=Cipher.getInstance("DES/ECB/PKCS5Padding"
System.out.println(key);
cipher.init(Cipher.ENCRYPT_MODE,key);
String amalgam=args[0];
for(int i=2;i<args.length;i++)
amalgam+=" "+args;
byte[] stringBytes=amalgam.getBytes("UTF8"
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("Usage : DecryptPassword text"
return;
}
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key;
try
{
ObjectInputStream in=new ObjectInputStream(new FileInputStream("des.key");
key=(Key)in.readObject();
in.close();
} catch(FileNotFoundException fnfe)
{
KeyGenerator generator= KeyGenerator.getInstance("DES"
generator.init(new SecureRandom() );
key=generator.generateKey();
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("des.key");
out.writeObject(key);
out.close();
}
KeyGenerator generator= KeyGenerator.getInstance("DES"
generator.init(new SecureRandom() );
key=generator.generateKey();
Cipher cipher=Cipher.getInstance("DES/ECB/PKCS5Padding"
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,"UTF8"
System.out.println(result);
}
}