Example usage for java.security NoSuchProviderException printStackTrace

List of usage examples for java.security NoSuchProviderException printStackTrace

Introduction

In this page you can find the example usage for java.security NoSuchProviderException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:pro.hirooka.streaming_server_for_multiple_platforms.Encrypter.java

@SuppressWarnings("resource")
public void run() {

    SingletonForSSFMP info = null;/*from w w w.j a v a  2  s .  c  o  m*/
    SingletonForSSFMP2 info2 = null;
    SingletonForSSFMP3 info3 = null;

    switch (abs) {
    case 0:
        info = SingletonForSSFMP.getInstance();
        break;
    case 1:
        info2 = SingletonForSSFMP2.getInstance();
        break;
    case 2:
        info3 = SingletonForSSFMP3.getInstance();
        break;
    default:
        //info = SingletonForMyStreamer.getInstance();
        break;
    }

    int seqTsEnc = 0; //info.getSeqTsEnc();
    if (!modeLive.equals("capturedTimeShifted")) {
        if ((abs == 0) && (info != null)) {
            seqTsEnc = info.getSeqTsEnc();
        } else if ((abs == 1) && (info2 != null)) {
            seqTsEnc = info2.getSeqTsEnc();
        } else if ((abs == 2) && (info3 != null)) {
            seqTsEnc = info3.getSeqTsEnc();
        }
    } else if (modeLive.equals("capturedTimeShifted")) {
        if ((abs == 0) && (info != null)) {
            seqTsEnc = info.getSeqTsCapturedTimeShifted();
        } else if ((abs == 1) && (info2 != null)) {
            seqTsEnc = info2.getSeqTsCapturedTimeShifted();
        } else if ((abs == 2) && (info3 != null)) {
            seqTsEnc = info3.getSeqTsCapturedTimeShifted();
        }
    }

    if ((abs == 0) && (info != null) && info.getFlagLastTs()) {
        seqTsEnc = info.getSeqTsLast();
    } else if ((abs == 1) && (info2 != null) && info2.getFlagLastTs()) {
        seqTsEnc = info2.getSeqTsLast();
    } else if ((abs == 2) && (info3 != null) && info3.getFlagLastTs()) {
        seqTsEnc = info3.getSeqTsLast();
    }

    log.debug(MARKER_Encrypter, "{} Begin : Encryption of seqTsEnc : {}",
            Thread.currentThread().getStackTrace()[1].getMethodName(), seqTsEnc);

    Key sKey;
    Cipher c;
    FileOutputStream keyOut;
    FileWriter ivOut;
    FileInputStream fis;
    BufferedInputStream bis;
    FileOutputStream fos;
    CipherOutputStream cos;

    try {

        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

        sKey = makeKey(128); // Key length is 128bit
        c = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
        //         log.debug(MARKER_Encrypter, "{} [c.getAlgorithm()] {}", Thread.currentThread().getStackTrace()[1].getMethodName(), c.getAlgorithm());
        c.init(Cipher.ENCRYPT_MODE, sKey);

        // Set Key File Name at random
        String keyPre = RandomStringUtils.randomAlphabetic(10);
        keyOut = new FileOutputStream(streamPath + FILE_SEPARATOR + keyPre + seqTsEnc + ".key");

        if ((abs == 0) && (info != null)) {
            info.addKeyArrayList(keyPre);
        } else if ((abs == 1) && (info2 != null)) {
            info2.addKeyArrayList(keyPre);
        } else if ((abs == 2) && (info3 != null)) {
            info3.addKeyArrayList(keyPre);
        }

        byte[] keyOutByte = sKey.getEncoded();
        keyOut.write(keyOutByte);
        keyOut.close();

        byte[] iv = c.getIV();
        //         log.debug(MARKER_Encrypter, "{} [iv.length] {} [byte]", Thread.currentThread().getStackTrace()[1].getMethodName(), iv.length);

        String ivHex = "";
        for (int i = 0; i < iv.length; i++) {
            String ivHexTmp = String.format("%02x", iv[i]).toUpperCase();
            ivHex = ivHex + ivHexTmp;
        }

        String ivPre = RandomStringUtils.randomAlphabetic(10);
        ivOut = new FileWriter(streamPath + FILE_SEPARATOR + ivPre + seqTsEnc + ".iv");
        ivOut.write(ivHex);
        ivOut.close();

        //         log.debug(MARKER_Encrypter, "{} [iv] {}", Thread.currentThread().getStackTrace()[1].getMethodName(), ivHex);

        if ((abs == 0) && (info != null)) {
            info.addIvArrayList(ivHex);
        } else if ((abs == 1) && (info2 != null)) {
            info2.addIvArrayList(ivHex);
        } else if ((abs == 2) && (info3 != null)) {
            info3.addIvArrayList(ivHex);
        }

        fis = new FileInputStream(TEMP_PATH_FOR_ENC + FILE_SEPARATOR + "fileSequence" + seqTsEnc + ".ts");
        bis = new BufferedInputStream(fis);
        fos = new FileOutputStream(streamPath + FILE_SEPARATOR + "fileSequenceEnc" + seqTsEnc + ".ts");
        cos = new CipherOutputStream(fos, c);
        if (modeLive.equals("capturedTimeShifted")) {
            fis = new FileInputStream(
                    TEMP_PATH_FOR_ENC + FILE_SEPARATOR + "fileSequenceEncoded" + seqTsEnc + ".ts");
            bis = new BufferedInputStream(fis);
            fos = new FileOutputStream(streamPath + FILE_SEPARATOR + "fileSequenceEnc" + seqTsEnc + ".ts");
            cos = new CipherOutputStream(fos, c);
        }

        byte[] buf = new byte[TS_PACKET_LENGTH];

        int ch;
        while ((ch = bis.read(buf)) != -1) {
            cos.write(buf, 0, ch);
        }
        cos.close();
        fos.close();
        bis.close();
        fis.close();

        log.debug(MARKER_Encrypter, "{} End : Encryption of seqTsEnc : {}",
                Thread.currentThread().getStackTrace()[1].getMethodName(), seqTsEnc);

        if ((abs == 0) && (info != null) && info.getFlagLastTs()) {
            log.debug(MARKER_Encrypter, "{} ALL ENCRYPTION FINISHED!!! {}",
                    Thread.currentThread().getStackTrace()[1].getMethodName(), abs);
        } else if ((abs == 1) && (info2 != null) && info2.getFlagLastTs()) {
            log.debug(MARKER_Encrypter, "{} ALL ENCRYPTION FINISHED!!! {}",
                    Thread.currentThread().getStackTrace()[1].getMethodName(), abs);
        } else if ((abs == 2) && (info3 != null) && info3.getFlagLastTs()) {
            log.debug(MARKER_Encrypter, "{} ALL ENCRYPTION FINISHED!!! {}",
                    Thread.currentThread().getStackTrace()[1].getMethodName(), abs);
        }

    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } // try
}