private static Cipher func_75886_a(int p_75886_0_, String p_75886_1_, Key p_75886_2_) {
   try {
     Cipher cipher = Cipher.getInstance(p_75886_1_);
     cipher.init(p_75886_0_, p_75886_2_);
     return cipher;
   } catch (InvalidKeyException invalidkeyexception) {
     invalidkeyexception.printStackTrace();
   } catch (NoSuchAlgorithmException nosuchalgorithmexception) {
     nosuchalgorithmexception.printStackTrace();
   } catch (NoSuchPaddingException nosuchpaddingexception) {
     nosuchpaddingexception.printStackTrace();
   }
   System.err.println("Cipher creation failed!");
   return null;
 }
  public Boolean verifySignature(byte[] originalData, byte[] signedData) {
    boolean verified = false;

    try {
      Signature sig = Signature.getInstance("SHA1withDSA", "SUN");
      sig.initVerify(getPublicKey());
      sig.update(originalData, 0, originalData.length);
      verified = sig.verify(signedData);
    } catch (SignatureException ex) {
      ex.printStackTrace();
      Logger.getLogger(SignatureVerifier.class.getName()).log(Level.SEVERE, null);
    } catch (InvalidKeyException ex) {
      ex.printStackTrace();
      Logger.getLogger(SignatureVerifier.class.getName()).log(Level.SEVERE, null);
    } catch (NoSuchAlgorithmException ex) {
      ex.printStackTrace();
      Logger.getLogger(SignatureVerifier.class.getName()).log(Level.SEVERE, null);
    } catch (NoSuchProviderException ex) {
      ex.printStackTrace();
      Logger.getLogger(SignatureVerifier.class.getName()).log(Level.SEVERE, null);
    }

    return verified;
  }
  public boolean delete(String filename, UserToken token) {
    try {
      String remotePath;
      if (filename.charAt(0) == '/') {
        remotePath = filename.substring(1);
      } else {
        remotePath = filename;
      }
      Envelope env = new Envelope("DELETEF"); // Success
      env.addObject(remotePath);
      env.addObject(token);
      String concat =
          remotePath
              + token.toString()
              + "DELETEF"
              + nonce; // concatinates all of the objects in envelope
      byte[] hasharray = concat.getBytes(); // turn the concat into a byte array
      Mac mac = Mac.getInstance("HmacSHA1");
      mac.init(HMACkey);
      mac.update(hasharray);
      String stringhash =
          new String(mac.doFinal(), "UTF8"); // turn the hash into a string for easy comparision!
      env.addObject(stringhash);
      env.addObject(nonce);
      nonce++;

      byte[] envBytes = Envelope.toByteArray(env);

      // Encrypt envelope w/ AES
      Cipher cipher = Cipher.getInstance("AES");
      cipher.init(Cipher.ENCRYPT_MODE, AESkey);
      byte[] cipherBytes = cipher.doFinal(envBytes);

      output.writeObject(cipherBytes);

      byte[] responseCipherBytes = (byte[]) input.readObject();

      // Decrypt response
      cipher.init(Cipher.DECRYPT_MODE, AESkey);
      byte[] responseBytes = cipher.doFinal(responseCipherBytes);

      env = Envelope.getEnvelopefromBytes(responseBytes);
      System.out.println(env.getMessage());
      if ((Integer) env.getObjContents().get(1) == nonce) {
        String hash = (String) env.getObjContents().get(0);
        concat = env.getMessage() + nonce; // reconstructs the hash
        hasharray = concat.getBytes();
        mac = Mac.getInstance("HmacSHA1");
        File HASHfile = new File("FHASHKey.bin");
        FileInputStream fis = new FileInputStream(HASHfile);
        ObjectInputStream ois = new ObjectInputStream(fis);
        Key HMACkey = (Key) ois.readObject();
        mac.init(HMACkey);
        mac.update(hasharray);
        String newhash = new String(mac.doFinal(), "UTF8");
        nonce++;

        if (hash.equals(newhash) != true) // check hashes for equality
        {
          System.out.println("HASH EQUALITY FAIL");
          return false;
        }

        if (env.getMessage().compareTo("OK") == 0) {
          System.out.printf("File %s deleted successfully\n", filename);
        } else {
          System.out.printf("Error deleting file %s (%s)\n", filename, env.getMessage());
          return false;
        }
      }
    } catch (IllegalBlockSizeException ex) {
      ex.printStackTrace(System.err);
    } catch (BadPaddingException ex) {
      ex.printStackTrace(System.err);
    } catch (InvalidKeyException ex) {
      ex.printStackTrace(System.err);
    } catch (NoSuchAlgorithmException ex) {
      ex.printStackTrace(System.err);
    } catch (NoSuchPaddingException ex) {
      ex.printStackTrace(System.err);
    } catch (IOException e1) {
      e1.printStackTrace(System.err);
    } catch (ClassNotFoundException e1) {
      e1.printStackTrace(System.err);
    }

    return true;
  }
Exemple #4
0
  @Override
  public void run() {

    ChukasaModel chukasaModel = chukasaModelManagementComponent.get(adaptiveBitrateStreaming);
    String streamPath = chukasaModel.getStreamPath();
    String tempEncPath = chukasaModel.getTempEncPath();
    int tsPacketLength = chukasaModel.getHlsConfiguration().getMpeg2TsPacketLength();

    int seqTsEnc = 0; // getSeqTsEnc();
    seqTsEnc = chukasaModel.getSeqTsEnc();
    if (chukasaModel.getChukasaSettings().getStreamingType() == StreamingType.OKKAKE) {
      seqTsEnc = chukasaModel.getSeqTsOkkake() - 1;
    }
    if (chukasaModel.isFlagLastTs()) {
      seqTsEnc = chukasaModel.getSeqTsLast();
    }

    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");

      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");

      chukasaModel.getKeyArrayList().add(keyPre);
      chukasaModel = chukasaModelManagementComponent.update(adaptiveBitrateStreaming, chukasaModel);

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

      byte[] iv = c.getIV();

      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();

      chukasaModel.getIvArrayList().add(ivHex);
      chukasaModel = chukasaModelManagementComponent.update(adaptiveBitrateStreaming, chukasaModel);

      fis =
          new FileInputStream(
              tempEncPath
                  + FILE_SEPARATOR
                  + chukasaModel.getChukasaConfiguration().getStreamFileNamePrefix()
                  + seqTsEnc
                  + chukasaModel.getHlsConfiguration().getStreamExtension());
      bis = new BufferedInputStream(fis);
      fos =
          new FileOutputStream(
              streamPath
                  + FILE_SEPARATOR
                  + chukasaModel.getChukasaConfiguration().getStreamFileNamePrefix()
                  + seqTsEnc
                  + chukasaModel.getHlsConfiguration().getStreamExtension());
      cos = new CipherOutputStream(fos, c);
      if (chukasaModel.getChukasaSettings().getStreamingType() == StreamingType.OKKAKE) {
        // TODO:
        fis =
            new FileInputStream(
                tempEncPath
                    + FILE_SEPARATOR
                    + "fileSequenceEncoded"
                    + seqTsEnc
                    + chukasaModel.getHlsConfiguration().getStreamExtension());
        bis = new BufferedInputStream(fis);
        fos =
            new FileOutputStream(
                streamPath
                    + FILE_SEPARATOR
                    + chukasaModel.getChukasaConfiguration().getStreamFileNamePrefix()
                    + seqTsEnc
                    + chukasaModel.getHlsConfiguration().getStreamExtension());
        cos = new CipherOutputStream(fos, c);
      }

      byte[] buf = new byte[tsPacketLength];

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

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