예제 #1
0
  public void initSiteEncryptionService() throws Exception {
    try {
      FileInputStream fis = new FileInputStream(secFile);
      Properties props = new Properties();
      props.load(fis);
      String k = props.getProperty("k");
      String d = props.getProperty("d");
      sun.misc.BASE64Decoder bde = new sun.misc.BASE64Decoder();
      // Decrypt the key
      byte[] key = decrypt(basePair.getDecryptor(), k);
      byte[] digest = bde.decodeBuffer(d);
      md5.reset();
      byte[] d2 = md5.digest(key);
      if (!Arrays.equals(digest, d2)) {
        throw (Exception) new Exception("FATAL -- Tampered key");
      }
      sitePair = initCiphers(key);

      // Get rid of the base pair

      basePair.setEncryptor(null);
      basePair.setDecryptor(null);
      basePair = null;
    } catch (FileNotFoundException fnfe) {
      throw (Exception)
          new Exception("Encryption utility not installed under current TRAFCIHOME")
              .initCause(fnfe);

    } catch (IOException ioe) {
      throw (Exception) new Exception("IO Exception").initCause(ioe);
    }
  }
예제 #2
0
  public void setupSiteEncryptionService() throws Exception {
    try {
      makeSecurityDir();

      KeyGenerator keyGen = KeyGenerator.getInstance("DES");

      SecretKey sKey = keyGen.generateKey();
      byte[] encKey = sKey.getEncoded();
      // We write two files, a digest of the key and the
      // encrypted key itself
      md5.reset();
      byte[] digest = md5.digest(encKey);
      sun.misc.BASE64Encoder ben = new sun.misc.BASE64Encoder();
      String d = ben.encode(digest);
      // Now encrypt the generated key with DBT cipher
      byte[] enc = basePair.getEncryptor().doFinal(encKey);
      String k = ben.encode(enc);
      Properties props = new Properties();
      props.put("d", d);
      props.put("k", k);
      File theSecFile = new File(secFile);
      FileOutputStream fos = new FileOutputStream(theSecFile);
      props.store(fos, "TRAFCI Security Initialization File");
      fos.flush();
      fos.close();
      Runtime.getRuntime().exec("chmod 600 " + secFile);
    } catch (IllegalBlockSizeException ibse) {
      throw (Exception) new Exception("Invalid cipher block size").initCause(ibse);
    } catch (BadPaddingException bpe) {
      throw (Exception) new Exception("Incorrect cipher padding").initCause(bpe);
    } catch (FileNotFoundException fnfe) {
      throw (Exception)
          new Exception("Encryption utility not installed under current NVTHOME").initCause(fnfe);

    } catch (NoSuchAlgorithmException nsae) {
      throw (Exception) new Exception("Invalid cipher algorithm").initCause(nsae);
    } catch (IOException ioe) {
      throw (Exception) new Exception("IO Exception").initCause(ioe);
    }
  }
  private OutputStream createOutputStream(File file) throws IOException {
    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    if (ObjectHelper.isNotEmpty(strategy.getSpoolChiper())) {
      try {
        if (ciphers == null) {
          ciphers = new CipherPair(strategy.getSpoolChiper());
        }
      } catch (GeneralSecurityException e) {
        throw new IOException(e.getMessage(), e);
      }
      out =
          new CipherOutputStream(out, ciphers.getEncryptor()) {
            boolean closed;

            public void close() throws IOException {
              if (!closed) {
                super.close();
                closed = true;
              }
            }
          };
    }
    return out;
  }