2016-04-01 52 views
0

我已经创建了一个SSIS包,它使用了一个执行进程任务,在这个任务中执行一个批处理文件。这个批处理文件正在执行一个java CLass文件。 java代码从目录中读取PGP文件,然后导入私钥并执行gpg filename.gpg命令。在成功执行时,它会提示输入密码。Gpg4win:避免密码提示,在java代码

GPG --allow秘密密钥导入--import PrivateKey.gpg
GPG filename.gpg
提示输入密码

我想要实现的是避免这种提示,在我的java代码中自动解密过程。那么是否存在一条命令,我可以通过该命令输入密码短语而无需获得提示。

Prompt Issue Image

这里是我的Java代码

private void decryptFiles(String encryptedFilePath, String keyFilePath){ 
    try{ 
     //location to encrypted gpg file 
     File encryptedFileLocation = new File(encryptedFilePath); 
     //location for key file that will be loaded 
     File keyFileLocation = new File(keyFilePath); 

     //changing dir and loading key 
     String []cmd = {"gpg", "--allow-secret-key-import --import Key.gpg"}; 

     ProcessBuilder builder = new ProcessBuilder(cmd); 

     Runtime runtime = Runtime.getRuntime(); 
     Process p = runtime.exec(cmd, null, keyFileLocation); 

     AutoDecrypter decrypterObj = new AutoDecrypter(); 
     if(p!=null) 
      decrypterObj.displayCommandPromptOutput(p); 
     else 
      System.out.println("Unable to create Process"); 

     if(encryptedFileLocation.isDirectory()){ 
      //all file with gpg extension will be stored in this array 
      File[] allGpgFiles = encryptedFileLocation.listFiles(decrypterObj.new GpgFileFilter()); 
      builder.directory(encryptedFileLocation); 
      cmd = new String[2]; 

      cmd[0] = "gpg"; 
      for(File f : allGpgFiles){ 
       //taking feed files and decrypting them one by one 
       String fileName = (f.getName()).substring(0,(f.getName()).length()-4); 

       cmd[1] = "--output D:\\AutoDecrypt_Feeds\\feed\\"+f.getName()+" --passphrase 123 --symmetric D:\\AutoDecrypt_Feeds\\feed\\"+fileName; 

       System.out.println((f.getName()).substring(0,(f.getName()).length()-4)); 
       builder.command(cmd); 
       p = builder.start(); 

       decrypterObj.displayCommandPromptOutput(p); 
       System.out.println("- Decrypted"); 
      } 

     }else{ 
      System.out.println("Enter a directory!!"); 
     } 

    }catch(Exception e){ 
     System.out.println(e); 
    } 
} 

private void displayCommandPromptOutput(Process p) throws IOException { 
    //output of command prompt is displayed 
    BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream())); 
    String cmdOutuptTxt = ""; 
    while((cmdOutuptTxt=bfr.readLine())!=null){ 
     System.out.println(cmdOutuptTxt); 
    } 
} 

//to get only gpg files from any directory 
class GpgFileFilter implements FilenameFilter{ 
    @Override 
    public boolean accept(File dir, String fileName) { 
     return fileName.endsWith(".gpg"); 
    } 
} 

这是我试过了,没有错误没有任何提示,但它不解密文件
CMD [0] = “GPG” ;
cmd 1 =“--output D:\ AutoDecrypt_Feeds \ feed \”+ f.getName()+“--passphrase 123 - 对称D:\ AutoDecrypt_Feeds \ feed \”+ fileName;

回答

0

您可以使用options to specify a passphrase

--passphrase文件文件

从文件中读取文件中的密码。只有第一行将从文件文件中读取。只有在提供了一个密码短语的情况下,才能使用该密码。显然,如果其他用户可以读取该文件,则存储在文件 中的密码短语具有可疑的安全性。如果可以避免,则不要使用此选项 。请注意,如果还提供了选项--batch,则此密码仅用于 。这与 GnuPG版本1.x不同。

- 密码字符串

使用字符串作为密码。只有在只提供了一个密码短语时才能使用。显然,这在多用户系统上非常有问题的安全性。如果 可以避免,请不要使用此选项。请注意,只有在选项 - 批次也被使用时才会使用此密码。这与GnuPG版本1.x不同。

或者你可以generate a key without a passphrase

记住这些都有安全隐患,正如文档中指出的那样:如果您对自己的工作不太在意,它基本上相当于将钥匙存放在您的门垫下。