package org.bouncycastle.openpgp.examples;

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.io.PrintStream;
import java.security.SecureRandom;
import java.security.Security;
import org.bouncycastle.bcpg.ArmoredOutputStream;
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.PGPPBEEncryptedData;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.openpgp.jcajce.JcaPGPObjectFactory;
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcePBEDataDecryptorFactoryBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcePBEKeyEncryptionMethodGenerator;
import org.bouncycastle.openpgp.operator.jcajce.JcePGPDataEncryptorBuilder;
import org.bouncycastle.util.io.Streams;

/* JADX INFO: loaded from: classes.dex */
public class PBEFileProcessor {
    private static void decryptFile(InputStream inputStream, char[] cArr) throws IOException, PGPException {
        PrintStream printStream;
        String str;
        JcaPGPObjectFactory jcaPGPObjectFactory = new JcaPGPObjectFactory(PGPUtil.getDecoderStream(inputStream));
        Object objNextObject = jcaPGPObjectFactory.nextObject();
        if (!(objNextObject instanceof PGPEncryptedDataList)) {
            objNextObject = jcaPGPObjectFactory.nextObject();
        }
        PGPPBEEncryptedData pGPPBEEncryptedData = (PGPPBEEncryptedData) ((PGPEncryptedDataList) objNextObject).get(0);
        if (!pGPPBEEncryptedData.isIntegrityProtected()) {
            throw new PGPException("Message is not integrity protected!");
        }
        Object objNextObject2 = new JcaPGPObjectFactory(pGPPBEEncryptedData.getDataStream(new JcePBEDataDecryptorFactoryBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider(BouncyCastleProvider.PROVIDER_NAME).build()).setProvider(BouncyCastleProvider.PROVIDER_NAME).build(cArr))).nextObject();
        if (objNextObject2 instanceof PGPCompressedData) {
            objNextObject2 = new JcaPGPObjectFactory(((PGPCompressedData) objNextObject2).getDataStream()).nextObject();
        }
        PGPLiteralData pGPLiteralData = (PGPLiteralData) objNextObject2;
        InputStream inputStream2 = pGPLiteralData.getInputStream();
        FileOutputStream fileOutputStream = new FileOutputStream(pGPLiteralData.getFileName());
        Streams.pipeAll(inputStream2, fileOutputStream, 8192);
        fileOutputStream.close();
        if (pGPPBEEncryptedData.verify()) {
            printStream = System.err;
            str = "message integrity check passed";
        } else {
            printStream = System.err;
            str = "message failed integrity check";
        }
        printStream.println(str);
    }

    private static void decryptFile(String str, char[] cArr) throws IOException, PGPException {
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(str));
        decryptFile(bufferedInputStream, cArr);
        bufferedInputStream.close();
    }

    private static void encryptFile(String str, String str2, char[] cArr, boolean z) throws IOException {
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(str));
        encryptToStream(bufferedOutputStream, str2, cArr, z);
        bufferedOutputStream.close();
    }

    private static void encryptToStream(OutputStream outputStream, String str, char[] cArr, boolean z) throws IOException {
        if (z) {
            outputStream = new ArmoredOutputStream(outputStream);
        }
        File file = new File(str);
        try {
            PGPEncryptedDataGenerator pGPEncryptedDataGenerator = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(9).setProvider(BouncyCastleProvider.PROVIDER_NAME).setSecureRandom(new SecureRandom()).setWithIntegrityPacket(true));
            pGPEncryptedDataGenerator.addMethod(new JcePBEKeyEncryptionMethodGenerator(cArr).setProvider(BouncyCastleProvider.PROVIDER_NAME));
            OutputStream outputStreamOpen = pGPEncryptedDataGenerator.open(outputStream, new byte[8192]);
            OutputStream outputStreamOpen2 = new PGPCompressedDataGenerator(1).open(outputStreamOpen);
            PGPUtil.writeFileToLiteralData(outputStreamOpen2, 'b', file);
            outputStreamOpen2.flush();
            outputStreamOpen2.close();
            outputStreamOpen.flush();
            outputStreamOpen.close();
            if (z) {
                outputStream.flush();
                outputStream.close();
            }
        } catch (PGPException e) {
            System.err.println(e);
            if (e.getUnderlyingException() != null) {
                e.getUnderlyingException().printStackTrace();
            }
        }
    }

    public static void main(String[] strArr) throws Exception {
        Security.addProvider(new BouncyCastleProvider());
        if (strArr[0].equals("-e")) {
            if (strArr[1].equals("-a")) {
                encryptFile(strArr[2] + ".asc", strArr[2], strArr[3].toCharArray(), true);
                return;
            } else {
                encryptFile(strArr[1] + ".bpg", strArr[1], strArr[2].toCharArray(), false);
                return;
            }
        }
        if (strArr[0].equals("-d")) {
            decryptFile(strArr[1], strArr[2].toCharArray());
        } else {
            System.err.println("usage: PBEFileProcessor -e [-a]|-d file passPhrase");
        }
    }
}
