📄 gnupg.java
字号:
* * @param inStr * signature to verify * @return true if verified. */ public boolean verify(String signedString, String dataString) { boolean success = false; File signedFile = createTempFile(signedString); File dataFile = createTempFile(dataString); if ((signedFile != null) && (dataFile != null)) { success = runGnuPG("--verify " + signedFile.getAbsolutePath() + " " + dataFile.getAbsolutePath(), null); signedFile.delete(); dataFile.delete(); if (success && this.gpg_exitCode != 0) { success = false; } } return success; } /** * Get processing result * * @return result string. */ public String getResult() { return gpg_result; } /** * Get error output from GnuPG process * * @return error string. */ public String getErrorString() { return gpg_err; } /** * Get GnuPG exit code * * @return exit code. */ public int getExitCode() { return gpg_exitCode; } /** * Runs GnuPG external program * * @param commandArgs * command line arguments * @param inputStr * string to pass to GnuPG process * @return true if success. */ private boolean runGnuPG(String commandArgs, String inputStr) { Process p; String fullCommand = kGnuPGCommand + " " + commandArgs; // String fullCommand = commandArgs; try { p = Runtime.getRuntime().exec(fullCommand); } catch (IOException io) { System.out.println("io Error " + io.getMessage()); com.valhalla.Logger.logException(io); return false; } if (inputStr != null) { BufferedWriter out = new BufferedWriter(new OutputStreamWriter(p .getOutputStream())); try { out.write(inputStr); out.close(); } catch (IOException io) { System.out.println("Exception at write! " + io.getMessage()); return false; } } ProcessStreamReader psr_stdout = new ProcessStreamReader(p .getInputStream(), "ERROR"); ProcessStreamReader psr_stderr = new ProcessStreamReader(p .getErrorStream(), "OUTPUT"); psr_stdout.start(); psr_stderr.start(); try { psr_stdout.join(); psr_stderr.join(); } catch (InterruptedException i) { System.out.println("Exception at join! " + i.getMessage()); return false; } try { p.waitFor(); } catch (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_stderr.getString(); return true; } /** * A utility method for creating a unique temporary file when needed by one * of the main methods. <BR> * The file handle is store in tmpFile object var. * * @param inStr * data to write into the file. * @return true if success */ private File createTempFile(String inStr) { File tmpFile = null; FileWriter fw; try { tmpFile = File.createTempFile("YGnuPG", null); } catch (Exception e) { System.out.println("Cannot create temp file " + e.getMessage()); return null; } try { fw = new FileWriter(tmpFile); fw.write(inStr); fw.flush(); fw.close(); } catch (Exception e) { // delete our file: tmpFile.delete(); System.out.println("Cannot write temp file " + e.getMessage()); return null; } return tmpFile; } /** * Default constructor */ public GnuPG() { kGnuPGCommand = Settings.getInstance().getProperty("gpgApplication", "gpg") + " " + kGnuPGArgs; } public GnuPG(String command) { kGnuPGCommand = command; } /** * Description of the Method * * @param xEncryptedData * Description of the Parameter * @return Description of the Return Value */ public String decryptExtension(String xEncryptedData) { String gnupgPassword = BuddyList.getInstance().getGnuPGPassword(); String encoding = null; xEncryptedData = xEncryptedData.replaceAll("(\n)+$", ""); xEncryptedData = xEncryptedData.replaceAll("^(\n)+", ""); if ((gnupgPassword != null) && decrypt("-----BEGIN PGP MESSAGE-----\nVersion: bla\n\n" + xEncryptedData + "\n-----END PGP MESSAGE-----\n", gnupgPassword)) { try { String systemEncoding = new String(getResult().getBytes(), "UTF8"); encoding = systemEncoding; } catch (java.io.UnsupportedEncodingException e) { } } return encoding.replaceAll("\n+$", ""); } /** * Description of the Method * * @param Data * Description of the Parameter * @param gnupgSecretKey * Description of the Parameter * @param gnupgPublicKey * Description of the Parameter * @return Description of the Return Value */ public String encryptExtension(String Data, String gnupgSecretKey, String gnupgPublicKey) { String encryptedData = null; try { byte[] utf8 = Data.getBytes("UTF8"); String string = new String(utf8, MiscUtils.streamEncoding()); Data = string; } catch (java.io.UnsupportedEncodingException e) { } if (encrypt(Data, gnupgSecretKey, gnupgPublicKey)) { encryptedData = getResult(); encryptedData = encryptedData.replaceAll( "-----BEGIN PGP MESSAGE-----(\n.*)+\n\n", ""); encryptedData = encryptedData.replaceAll( "\n-----END PGP MESSAGE-----\n", ""); } return encryptedData; } /** * Description of the Method * * @param Data * Description of the Parameter * @param gnupgSecretKey * Description of the Parameter * @param gnupgPublicKey * Description of the Parameter * @return Description of the Return Value */ public String signExtension(String Data, String gnupgSecretKey) { String gnupgPassword = BuddyList.getInstance().getGnuPGPassword(); String signedData = null; try { byte[] utf8 = Data.getBytes("UTF8"); String string = new String(utf8, MiscUtils.streamEncoding()); Data = string; } catch (java.io.UnsupportedEncodingException e) { } if ((gnupgPassword != null) && (sign(Data, gnupgSecretKey, gnupgPassword))) { signedData = getResult(); signedData = signedData.replaceAll( "-----BEGIN PGP SIGNATURE-----(\n.*)+\n\n", ""); signedData = signedData.replaceAll( "\n-----END PGP SIGNATURE-----\n", ""); signedData = signedData.replaceAll("^(\n)+", ""); signedData = signedData.replaceAll("(\n)+$", ""); } return signedData; } public String verifyExtension(String xSignedData, String messageBody) { String id = null; try { byte[] utf8 = messageBody.getBytes("UTF8"); String string = new String(utf8, MiscUtils.streamEncoding()); messageBody = string; } catch (java.io.UnsupportedEncodingException e) { } messageBody = messageBody.replaceAll("(\n)+$", ""); xSignedData = xSignedData.replaceAll("(\n)+$", ""); messageBody = messageBody.replaceAll("^(\n)+", ""); xSignedData = xSignedData.replaceAll("^(\n)+", ""); if (verify("-----BEGIN PGP SIGNATURE-----\nVersion: bla\n\n" + xSignedData + "\n-----END PGP SIGNATURE-----", messageBody)) { id = getErrorString(); id = id.replaceAll(".*ID (.*)(\n.*)+", "$1"); } return id; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -