繁体   English   中英

通过在javascript中设计的中间服务器探针调用java jar方法

[英]Calling a java jar method via mid server probe designed in javascript

我正在设置一个环境,我将 4 个参数(加密文件、密钥文件、传递阶段和默认文件名)传递给 .Jar 并解密文件。 我已经通过 Bouncycastle API 实现了它,它在 Eclipse IDE 上运行良好。

现在我必须在我的 servicenow 中间服务器中设置它。

因此 javascript 探针应该调用这个 jar 文件并将参数作为字符串传递,并且加密文件(驻留在中间服务器上)被解密。

我曾尝试创建一个包含在 servicenow 中的中间服务器脚本和一个探测器,但它返回一个错误,即找不到该方法(实际上在那里)

.JAR 中的类

package pgpDecrypt;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Iterator;

import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.bcpg.CompressionAlgorithmTags;
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.PGPCompressedData;
import org.bouncycastle.openpgp.PGPCompressedDataGenerator;
import org.bouncycastle.openpgp.PGPEncryptedDataGenerator;
import org.bouncycastle.openpgp.PGPEncryptedDataList;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPLiteralData;
import org.bouncycastle.openpgp.PGPOnePassSignatureList;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.openpgp.jcajce.JcaPGPObjectFactory;
import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
import org.bouncycastle.openpgp.operator.jcajce.JcePGPDataEncryptorBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyKeyEncryptionMethodGenerator;
import org.bouncycastle.util.io.Streams;

public class PGPDecryption {
    public static void decryptFile(String inputFileName, String keyFileName, char[] passwd, String defaultFileName)
            throws IOException, NoSuchProviderException {
        Security.addProvider(new BouncyCastleProvider());

        InputStream in = new BufferedInputStream(new FileInputStream(inputFileName));
        InputStream keyIn = new BufferedInputStream(new FileInputStream(keyFileName));
        decryptFiles(in, keyIn, passwd, defaultFileName);
        System.out.print("Default File Name : "+ defaultFileName);
        keyIn.close();
        in.close();
    }

    /**
     * decrypt the passed in message stream
     */
    public static void decryptFiles(InputStream in, InputStream keyIn, char[] passwd, String defaultFileName)
            throws IOException, NoSuchProviderException {
        in = PGPUtil.getDecoderStream(in);

        try {
            JcaPGPObjectFactory pgpF = new JcaPGPObjectFactory(in);
            PGPEncryptedDataList enc;

            Object o = pgpF.nextObject();
            //
            // the first object might be a PGP marker packet.
            //
            if (o instanceof PGPEncryptedDataList) {
                enc = (PGPEncryptedDataList) o;
            } else {
                enc = (PGPEncryptedDataList) pgpF.nextObject();
            }

            //
            // find the secret key
            //
            Iterator it = enc.getEncryptedDataObjects();
            PGPPrivateKey sKey = null;
            PGPPublicKeyEncryptedData pbe = null;
            PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(keyIn),
                    new JcaKeyFingerprintCalculator());

            while (sKey == null && it.hasNext()) {
                pbe = (PGPPublicKeyEncryptedData) it.next();

                sKey = PGPUtilE.findSecretKey(pgpSec, pbe.getKeyID(), passwd);
            }

            if (sKey == null) {
                throw new IllegalArgumentException("secret key for message not found.");
            }

            InputStream clear = pbe
                    .getDataStream(new JcePublicKeyDataDecryptorFactoryBuilder().setProvider("BC").build(sKey));

            JcaPGPObjectFactory plainFact = new JcaPGPObjectFactory(clear);

            PGPCompressedData cData = (PGPCompressedData) plainFact.nextObject();

            InputStream compressedStream = new BufferedInputStream(cData.getDataStream());
            JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(compressedStream);

            Object message = pgpFact.nextObject();

            if (message instanceof PGPLiteralData) {
                PGPLiteralData ld = (PGPLiteralData) message;

                String outFileName = ld.getFileName();
                if (outFileName.length() == 0) {
                    outFileName = defaultFileName;
                }

                InputStream unc = ld.getInputStream();
                OutputStream fOut = new BufferedOutputStream(new FileOutputStream(outFileName));

                Streams.pipeAll(unc, fOut);

                fOut.close();
            } else if (message instanceof PGPOnePassSignatureList) {
                throw new PGPException("encrypted message contains a signed message - not literal data.");
            } else {
                throw new PGPException("message is not a simple encrypted file - type unknown.");
            }

            if (pbe.isIntegrityProtected()) {
                if (!pbe.verify()) {
                    System.err.println("message failed integrity check");
                } else {
                    System.err.println("message integrity check passed");
                }
            } else {
                System.err.println("no message integrity check");
            }
        } catch (PGPException e) {
            System.err.println(e);
            if (e.getUnderlyingException() != null) {
                e.getUnderlyingException().printStackTrace();
            }
        }
    }

    private static void encryptFile(String outputFileName, String inputFileName, String encKeyFileName, boolean armor,
            boolean withIntegrityCheck) throws IOException, NoSuchProviderException, PGPException {
        OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFileName));
        PGPPublicKey encKey = PGPUtilE.readPublicKey(encKeyFileName);
        encryptFile(out, inputFileName, encKey, armor, withIntegrityCheck);
        out.close();
    }

    private static void encryptFile(OutputStream out, String fileName, PGPPublicKey encKey, boolean armor,
            boolean withIntegrityCheck) throws IOException, NoSuchProviderException {
        if (armor) {
            out = new ArmoredOutputStream(out);
        }

        try {
            PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(
                    new JcePGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.CAST5).setWithIntegrityPacket(withIntegrityCheck)
                            .setSecureRandom(new SecureRandom()).setProvider("BC"));

            cPk.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(encKey).setProvider("BC"));

            OutputStream cOut = cPk.open(out, new byte[1 << 16]);

            PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(CompressionAlgorithmTags.ZIP);

            PGPUtil.writeFileToLiteralData(comData.open(cOut), PGPLiteralData.BINARY, new File(fileName),
                    new byte[1 << 16]);

            comData.close();

            cOut.close();

            if (armor) {
                out.close();
            }
        } catch (PGPException e) {
            System.err.println(e);
            if (e.getUnderlyingException() != null) {
                e.getUnderlyingException().printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws Exception {
        Security.addProvider(new BouncyCastleProvider());

        if (args.length == 0) {
            System.err.println(
                    "usage: PGPDecryption -d file [secretKeyFile passPhrase|pubKeyFile]");
            return;
        }

        if (args[0].equals("-e")) {
            if (args[1].equals("-a") || args[1].equals("-ai") || args[1].equals("-ia")) {
                encryptFile(args[2] + ".asc", args[2], args[3], true, (args[1].indexOf('i') > 0));
            } else if (args[1].equals("-i")) {
                encryptFile(args[2] + ".bpg", args[2], args[3], false, true);
            } else {
                encryptFile(args[1] + ".bpg", args[1], args[2], false, false);
            }
        } else if (args[0].equals("-d")) {
            decryptFile(args[1], args[2], args[3].toCharArray(), new File(args[1]).getName() + ".out");
        } else {
            System.err.println(
                    "usage: PGPDecryption -d|-e [-a|ai] file [secretKeyFile passPhrase|pubKeyFile]");
        }
    }
}

脚本包括:


var ProcessPGP = Class.create();

ProcessPGP.prototype = {

    initialize: function() {

        this.Pgp = Packages.pgpDecrypt.PGPDecryption.decryptFile;

        this.inputFile = probe.getParameter("inputFile");
        this.secretFile = probe.getParameter("secretFile");
        this.passPhase = probe.getParameter("passPhase");
        this.defaultName = probe.getParameter("defaultName");

    },

    execute: function() {

        var pgpObj = new this.Pgp(this.inputFile, this.secretFile, this.passPhase, this.defaultName);

    },

    type: ProcessPGP
};

探测 :


var jspr = new JavascriptProbe('ANIRUDGU-68LCS_Dev1');
jspr.setName('TestPGPDemo5');
jspr.setJavascript('var pdf = new ProcessPGP(); res = pdf.execute();');
jspr.addParameter("inputFile", "C:\Users\anirudgu\Desktop\PGPTestKey\TestRun2.pgp");
jspr.addParameter("secretFile", "C:\Users\anirudgu\Desktop\PGPTestKey\anirudguciscocomprivate.asc");
jspr.addParameter("passPhase", "Hello");
jspr.addParameter("defaultName", "FilefromProbe");
jspr.create();

但我面临以下提到的错误:

08/22/19 23:21:58 (097) Worker-Standard:JavascriptProbe-ce4567ebdb9b330045bb9b81ca961910 WARNING *** WARNING *** org.mozilla.javascript.EvaluatorException: Can't find method pgpDecrypt.PGPDecryption.decryptFile(java.lang.String,java.lang.String,java.lang.String,java.lang.String). (script_include:ProcessPGP; line 32)
   EvaluatorException(var pdf = new ProcessPGP(); res = pdf.execute();)

方法decryptFile是静态的。 new关键字只能用于创建实例。 因此,请尝试:

var pgpObj = this.Pgp(this.inputFile, this.secretFile, this.passPhase, this.defaultName);

尝试从以下位置删除“新”:

jspr.setJavascript('var pdf = new ProcessPGP(); res = pdf.execute();');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM