Hi java gurus, I am a actually new to the GnuPG package and i have been able to deal with the problems i have encountered so far but this one has been very difficult for me to accomplish, any help or ideas on how to fix it will be very appreciated.
i am trying to create a new key in the GnuPG database which is external to my application and i have been able to create the key using the command line (which is not what i want to do) but be able to create this key using my application but instead when i run my code i keep getting a no response and no key was created when i checked this via the GnuPG, however if i create a key or i already registered a user via the command utility by windows, i can use the encryption and decryption command via my application and and my code can be found below. many thanks in advance.
//method to create the key
public boolean createkey(){
boolean success=false;
success = runGnuPG ("--gen-key \n1\n2048\n365\ny\ntolulope\ntolu@tolu.com\ndulling\no\npassword123\npassword123",null);
if (success && this.gpg_exitCode != 0)
success = false;
return success;
}
public boolean createkey2(){
boolean success=false;
success = runGnuPG ("--gen-key ","1 2048 365 y tolulope tolu@tolu.com dulling o password123 password123"/*null*/);
if (success && this.gpg_exitCode != 0)
success = false;
return success;
}
//method to encrypt and decrypt a file
public boolean encrypt (String inStr, String User)
{
boolean success=false;
File temp= createTempFile (inStr);
String outFile =progtempdir+"/"+User+System.currentTimeMillis()+".gpg";
if (temp!=null)
{
success = runGnuPG ("-r " + User+" -o \""+outFile+"\" --encrypt \""+temp+"\"", null);
temp.delete ();
if (success && this.gpg_exitCode != 0)
success = false;
}
return success;
}
public boolean decrypt ( String passPhrase,String User,String inFile)
{
boolean success=false;
//File temp= createTempFile (inStr);
String OutFile=progtempdir+"/"+inFile+".txt";
inFile =progtempdir+"/"+inFile+".gpg";
//if (temp!=null)
//{
success = runGnuPG ("-r " + User+" --passphrase \""+passPhrase+"\" -o \""+OutFile+"\" --decrypt \""+inFile+"\"", passPhrase);
//temp.delete ();
if (success && this.gpg_exitCode != 0)
success = false;
//}
return success;
}
//method for running the commandline and passed instructions passed into it.
private boolean runGnuPG (String commandArgs, String inputPassphrase)
{
String kGnuPGCommand = "cmd /c gpg";
Process p=null;
String fullCommand = kGnuPGCommand + " " + commandArgs;
// String fullCommand = commandArgs;
System.out.println (fullCommand);
try
{
p = Runtime.getRuntime().exec(/*"cmd /c md helloboy"*/fullCommand);
}
catch(IOException io)
{
System.out.println ("io Error" + io.getMessage ());
return false;
}
ProcessStreamReader psr_stdout = new ProcessStreamReader(p.getInputStream());
ProcessStreamReader psr_stderr = new ProcessStreamReader(p.getErrorStream());
psr_stdout.start();
psr_stderr.start();
//System.out.println("hello");
if (inputPassphrase != null)
{
String tempstr[]=splitstr(inputPassphrase);
//BufferedOutputStream out = new BufferedOutputStream(p.getOutputStream());
//BufferedWriter out = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
//OutputStream out=p.getOutputStream();
for (int i=0;i<tempstr.length;i++){
System.out.println(tempstr);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
try
{
//out.write(inputPassphrase);
out.write(tempstr[i]);
try{p.waitFor();}catch(Exception ex){}
out.close();
// System.out.println("hello");
}
catch(IOException io)
{
System.out.println("Exception at write! " + io.getMessage ());
return false;
}
}
}
try
{
p.waitFor();
psr_stdout.join();
psr_stderr.join();
}
catch(Exception i/*InterruptedException i*/)
{
System.out.println("Exception at waitfor! " + i.getMessage ());
return false;
}
try
{
gpg_exitCode = p.exitValue ();
}
catch (IllegalThreadStateException itse)
{
return false;
}
gpg_result = psr_stdout.getString();
gpg_err = psr_stdout.getString();
return true;
}
String [] addvalue(String atr, String [] arrstr){
String [] retstr =null;
//System.out.println(arrstr.length);
try{
if (arrstr.length>0){
retstr=new String[arrstr.length+1];
System.arraycopy(arrstr, 0, retstr, 0, arrstr.length);
retstr[arrstr.length]=atr;
}
else{
retstr=new String[1];
retstr[0]=atr;
}
}catch(Exception ex){
retstr=new String[1];
retstr[0]=atr;
}
return retstr;
}
String [] splitstr(String inp){
String [] temp=null; String atr="";
for (int i=0;i<inp.length();i++){
if (inp.charAt(i)==' '){
temp=addvalue(atr,temp);
atr="";
}
else{
atr=atr+inp.charAt(i);
}
}
temp=addvalue(atr,temp);
//System.out.println(temp.length);
//atr="";
return temp;
}
//this is the thread handling the procedures for the runGnuPG method
class ProcessStreamReader extends Thread
{
StringBuffer stream;
InputStreamReader in;
final static int BUFFER_SIZE = 1024;
/**
* Creates new ProcessStreamReader object.
*
* @param in
*/
ProcessStreamReader (InputStream in)
{
super();
this.in = new InputStreamReader(in);
this.stream = new StringBuffer();
}
public void run()
{
try
{
int read;
char[] c = new char[BUFFER_SIZE];
while ((read = in.read(c, 0, BUFFER_SIZE - 1)) > 0)
{
stream.append(c, 0, read);
if (read < BUFFER_SIZE - 1) break;
}
}
catch(IOException io) {}
}
String getString()
{
return stream.toString();
}
}
Edited by: user13340119 on Jul 17, 2011 11:53 PM
Edited by: user13340119 on Jul 17, 2011 11:53 PM
Edited by: user13340119 on Jul 17, 2011 11:54 PM
Edited by: sabre150 on 18-Jul-2011 01:50
Added code tags to make the code readable.