我已經創建了一個SSIS包,它使用了一個執行進程任務,在這個任務中執行一個批處理文件。這個批處理文件正在執行一個java CLass文件。 java代碼從目錄中讀取PGP文件,然後導入私鑰並執行gpg filename.gpg命令。在成功執行時,它會提示輸入密碼。Gpg4win:避免密碼提示,在java代碼
GPG --allow祕密密鑰導入--import PrivateKey.gpg
GPG filename.gpg
提示輸入密碼
我想要實現的是避免這種提示,在我的java代碼中自動解密過程。那麼是否存在一條命令,我可以通過該命令輸入密碼短語而無需獲得提示。
這裏是我的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;