我已经创建了一个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;