2016-04-01 55 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

記住這些都有安全隱患,正如文檔中指出的那樣:如果您對自己的工作不太在意,它基本上相當於將鑰匙存放在您的門墊下。