⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gnupg.java

📁 网站即时通讯系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.valhalla.misc;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import com.valhalla.jbother.BuddyList;import com.valhalla.settings.Settings;/** * A class that implements PGP interface for Java. * <P> *  * It calls gpg (GnuPG) program to do all the PGP commands. $Id:$ *  * @author Yaniv Yemini, January 2004. * @author Based on a class GnuPG by John Anderson, which can be found * @author at: *         http://lists.gnupg.org/pipermail/gnupg-devel/2002-February/018098.html * @author modified for use in JBother by Andrey Zakirov, February 2005 * @created March 9, 2005 * @version 0.5.1 * @see GnuPG - http://www.gnupg.org/ */public class GnuPG {    // Constants:    private final String kGnuPGCommand;    private static final String kGnuPGArgs = " --batch --armor --output -";    // Class vars:    private int gpg_exitCode = -1;    private String gpg_result;    private String gpg_err;    /**     * Reads an output stream from an external process. Imeplemented as a thred.     *      * @author synic     * @created March 9, 2005     */    class ProcessStreamReader extends Thread {        InputStream is;        String type;        OutputStream os;        String fullLine = "";        /**         * Constructor for the ProcessStreamReader object         *          * @param is         *            Description of the Parameter         * @param type         *            Description of the Parameter         */        ProcessStreamReader(InputStream is, String type) {            this(is, type, null);        }        /**         * Constructor for the ProcessStreamReader object         *          * @param is         *            Description of the Parameter         * @param type         *            Description of the Parameter         * @param redirect         *            Description of the Parameter         */        ProcessStreamReader(InputStream is, String type, OutputStream redirect) {            this.is = is;            this.type = type;            this.os = redirect;        }        /**         * Main processing method for the ProcessStreamReader object         */        public void run() {            try {                InputStreamReader isr = new InputStreamReader(is);                BufferedReader br = new BufferedReader(isr);                String line = null;                while ((line = br.readLine()) != null) {                    fullLine = fullLine + line + "\n";                }            } catch (IOException ioe) {                ioe.printStackTrace();            }        }        /**         * Gets the string attribute of the ProcessStreamReader object         *          * @return The string value         */        String getString() {            return fullLine;        }    }    /**     * Sign     *      * @param inStr     *            input string to sign     * @param secID     *            ID of secret key to sign with     * @param passPhrase     *            passphrase for the secret key to sign with     * @return true upon success     */    public boolean sign(String inStr, String secID, String passPhrase) {        boolean success = false;        File tmpFile = createTempFile(inStr);        if (tmpFile != null) {            success = runGnuPG("-u " + secID + " --passphrase-fd 0 -b "                    + tmpFile.getAbsolutePath(), passPhrase);            tmpFile.delete();            if (success && this.gpg_exitCode != 0) {                success = false;            }        }        return success;    }    /**     * ClearSign     *      * @param inStr     *            input string to sign     * @param secID     *            ID of secret key to sign with     * @param passPhrase     *            passphrase for the secret key to sign with     * @return true upon success     */    public boolean clearSign(String inStr, String secID, String passPhrase) {        boolean success = false;        File tmpFile = createTempFile(inStr);        if (tmpFile != null) {            success = runGnuPG("-u " + secID                    + " --passphrase-fd 0 --clearsign "                    + tmpFile.getAbsolutePath(), passPhrase);            tmpFile.delete();            if (success && this.gpg_exitCode != 0) {                success = false;            }        }        return success;    }    /**     * Signs and encrypts a string     *      * @param inStr     *            input string to encrypt     * @param secID     *            ID of secret key to sign with     * @param keyID     *            ID of public key to encrypt with     * @param passPhrase     *            passphrase for the secret key to sign with     * @return true upon success     */    public boolean signAndEncrypt(String inStr, String secID, String keyID,            String passPhrase) {        boolean success = false;        File tmpFile = createTempFile(inStr);        if (tmpFile != null) {            success = runGnuPG("-u " + secID + " -r " + keyID                    + " --passphrase-fd 0 -se " + tmpFile.getAbsolutePath(),                    passPhrase);            tmpFile.delete();            if (success && this.gpg_exitCode != 0) {                success = false;            }        }        return success;    }    /**     * Encrypt     *      * @param inStr     *            input string to encrypt     * @param secID     *            ID of secret key to use     * @param keyID     *            ID of public key to encrypt with     * @return true upon success     */    public boolean encrypt(String inStr, String secID, String keyID) {        boolean success;        success = runGnuPG("-u " + secID + " -r " + keyID + " --encrypt", inStr);        if (success && this.gpg_exitCode != 0) {            success = false;        }        return success;    }    /**     * Decrypt     *      * @param inStr     *            input string to decrypt     * @param passPhrase     *            passphrase for the secret key to decrypt with     * @return true upon success     */    public boolean decrypt(String inStr, String passPhrase) {        boolean success = false;        File tmpFile = createTempFile(inStr);        if (tmpFile != null) {            success = runGnuPG("--passphrase-fd 0 --decrypt "                    + tmpFile.getAbsolutePath(), passPhrase);            tmpFile.delete();            if (success && this.gpg_exitCode != 0) {                success = false;            }        }        return success;    }    /**     * List public keys in keyring     *      * @param ID     *            ID of public key to list, blank for all     * @return true upon success     */    public boolean listKeys(String ID) {        boolean success;        success = runGnuPG("--list-keys --with-colons " + ID, null);        if (success && this.gpg_exitCode != 0) {            success = false;        }        return success;    }    /**     * List secret keys in keyring     *      * @param ID     *            ID of secret key to list, blank for all     * @return true upon success     */    public boolean listSecretKeys(String ID) {        boolean success;        success = runGnuPG("--list-secret-keys --with-colons " + ID, null);        if (success && this.gpg_exitCode != 0) {            success = false;        }        return success;    }    /**     * Verify a signature

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -