Пример #1
0
    /** Read a Key Pair */
    public Object parseObject(PemObject obj) throws IOException {
      boolean isEncrypted = false;
      String dekInfo = null;
      List headers = obj.getHeaders();

      for (Iterator it = headers.iterator(); it.hasNext(); ) {
        PemHeader hdr = (PemHeader) it.next();

        if (hdr.getName().equals("Proc-Type") && hdr.getValue().equals("4,ENCRYPTED")) {
          isEncrypted = true;
        } else if (hdr.getName().equals("DEK-Info")) {
          dekInfo = hdr.getValue();
        }
      }

      //
      // extract the key
      //
      byte[] keyBytes = obj.getContent();

      try {
        if (isEncrypted) {
          StringTokenizer tknz = new StringTokenizer(dekInfo, ",");
          String dekAlgName = tknz.nextToken();
          byte[] iv = Hex.decode(tknz.nextToken());

          return new PEMEncryptedKeyPair(dekAlgName, iv, keyBytes, pemKeyPairParser);
        }

        return pemKeyPairParser.parse(keyBytes);
      } catch (IOException e) {
        if (isEncrypted) {
          throw new PEMException("exception decoding - please check password and data.", e);
        } else {
          throw new PEMException(e.getMessage(), e);
        }
      } catch (IllegalArgumentException e) {
        if (isEncrypted) {
          throw new PEMException("exception decoding - please check password and data.", e);
        } else {
          throw new PEMException(e.getMessage(), e);
        }
      }
    }