Package org.apache.poi

Examples of org.apache.poi.EncryptedDocumentException


      if (key instanceof Biff8RC4Key) {
          _cipher = new Biff8RC4(initialOffset, (Biff8RC4Key)key);
      } else if (key instanceof Biff8XORKey) {
          _cipher = new Biff8XOR(initialOffset, (Biff8XORKey)key);
      } else {
          throw new EncryptedDocumentException("Crypto API not supported yet.");
      }

    if (in instanceof LittleEndianInput) {
      // accessing directly is an optimisation
      _le = (LittleEndianInput) in;
View Full Code Here


  /**
   * @return <code>true</code> if the keyDigest is compatible with the specified saltData and saltHash
   */
  public boolean validate(byte[] saltData, byte[] saltHash) {
      throw new EncryptedDocumentException("validate is not supported (in super-class).");
  }
View Full Code Here

              case ENCRYPTION_OTHER_RC4:
                  // handled below
                  break;
              case ENCRYPTION_OTHER_CAPI_2:
              case ENCRYPTION_OTHER_CAPI_3:
                  throw new EncryptedDocumentException(
                          "HSSF does not currently support CryptoAPI encryption");
              default:
                  throw new RecordFormatException("Unknown encryption info " + _encryptionInfo);
          }
          _minorVersionNo = in.readUShort();
View Full Code Here

        }
       
        // check for an encrypted .xlsx file - they get OLE2 wrapped
        try {
          directory.getEntry("EncryptedPackage");
          throw new EncryptedDocumentException("The supplied spreadsheet seems to be an Encrypted .xlsx file. " +
              "It must be decrypted before use by XSSF, it cannot be used by HSSF");
        } catch (FileNotFoundException e) {
            // fall through
        }
View Full Code Here

            keyData = ed.getEncryption().getKeyData();
            if (keyData == null) {
                throw new NullPointerException("keyData not set");
            }
        } catch (Exception e) {
            throw new EncryptedDocumentException("Unable to parse keyData");
        }

        setKeySize((int)keyData.getKeyBits());
        setFlags(0);
        setSizeExtra(0);
        setCspName(null);
        setBlockSize(keyData.getBlockSize());

        int keyBits = (int)keyData.getKeyBits();
       
        CipherAlgorithm ca = CipherAlgorithm.fromXmlId(keyData.getCipherAlgorithm().toString(), keyBits);
        setCipherAlgorithm(ca);
        setCipherProvider(ca.provider);

        switch (keyData.getCipherChaining().intValue()) {
        case STCipherChaining.INT_CHAINING_MODE_CBC:
            setChainingMode(ChainingMode.cbc);
            break;
        case STCipherChaining.INT_CHAINING_MODE_CFB:
            setChainingMode(ChainingMode.cfb);
            break;
        default:
            throw new EncryptedDocumentException("Unsupported chaining mode - "+keyData.getCipherChaining().toString());
        }
   
        int hashSize = keyData.getHashSize();
       
        HashAlgorithm ha = HashAlgorithm.fromEcmaId(keyData.getHashAlgorithm().toString());
        setHashAlgorithm(ha);

        if (getHashAlgorithmEx().hashSize != hashSize) {
            throw new EncryptedDocumentException("Unsupported hash algorithm: " +
                    keyData.getHashAlgorithm() + " @ " + hashSize + " bytes");
        }

        int saltLength = keyData.getSaltSize();
        setKeySalt(keyData.getSaltValue());
        if (getKeySalt().length != saltLength) {
            throw new EncryptedDocumentException("Invalid salt length");
        }
       
        CTDataIntegrity di = ed.getEncryption().getDataIntegrity();
        setEncryptedHmacKey(di.getEncryptedHmacKey());
        setEncryptedHmacValue(di.getEncryptedHmacValue());
View Full Code Here

    }

    // make method visible for this package
    protected void setKeySalt(byte salt[]) {
        if (salt == null || salt.length != getBlockSize()) {
            throw new EncryptedDocumentException("invalid verifier salt");
        }
        super.setKeySalt(salt);
    }
View Full Code Here

                Mac x509Hmac = CryptoFunctions.getMac(hashAlgo);
                x509Hmac.init(getSecretKey());
                ace.certVerifier = x509Hmac.doFinal(ace.x509.getEncoded());
            }
        } catch (GeneralSecurityException e) {
            throw new EncryptedDocumentException(e);
        }
  }
View Full Code Here

        keyData.setHashSize(hashAlgo.hashSize);
        keyPass.setHashSize(hashAlgo.hashSize);

        STCipherAlgorithm.Enum xmlCipherAlgo = STCipherAlgorithm.Enum.forString(header.getCipherAlgorithm().xmlId);
        if (xmlCipherAlgo == null) {
            throw new EncryptedDocumentException("CipherAlgorithm "+header.getCipherAlgorithm()+" not supported.");
        }
        keyData.setCipherAlgorithm(xmlCipherAlgo);
        keyPass.setCipherAlgorithm(xmlCipherAlgo);
       
        switch (header.getChainingMode()) {
        case cbc:
            keyData.setCipherChaining(STCipherChaining.CHAINING_MODE_CBC);
            keyPass.setCipherChaining(STCipherChaining.CHAINING_MODE_CBC);
            break;
        case cfb:
            keyData.setCipherChaining(STCipherChaining.CHAINING_MODE_CFB);
            keyPass.setCipherChaining(STCipherChaining.CHAINING_MODE_CFB);
            break;
        default:
            throw new EncryptedDocumentException("ChainingMode "+header.getChainingMode()+" not supported.");
        }
       
        STHashAlgorithm.Enum xmlHashAlgo = STHashAlgorithm.Enum.forString(hashAlgo.ecmaString);
        if (xmlHashAlgo == null) {
            throw new EncryptedDocumentException("HashAlgorithm "+hashAlgo+" not supported.");
        }
        keyData.setHashAlgorithm(xmlHashAlgo);
        keyPass.setHashAlgorithm(xmlHashAlgo);

        keyData.setSaltValue(header.getKeySalt());
        keyPass.setSaltValue(ver.getSalt());
        keyPass.setEncryptedVerifierHashInput(ver.getEncryptedVerifier());
        keyPass.setEncryptedVerifierHashValue(ver.getEncryptedVerifierHash());
        keyPass.setEncryptedKeyValue(ver.getEncryptedKey());
       
        CTDataIntegrity hmacData = edRoot.addNewDataIntegrity();
        hmacData.setEncryptedHmacKey(header.getEncryptedHmacKey());
        hmacData.setEncryptedHmacValue(header.getEncryptedHmacValue());
       
        for (AgileCertificateEntry ace : ver.getCertificates()) {
            keyEnc = keyEncList.addNewKeyEncryptor();
            keyEnc.setUri(certificateUri);
            CTCertificateKeyEncryptor certData = keyEnc.addNewEncryptedCertificateKey();
            try {
                certData.setX509Certificate(ace.x509.getEncoded());
            } catch (CertificateEncodingException e) {
                throw new EncryptedDocumentException(e);
            }
            certData.setEncryptedKeyValue(ace.encryptedKey);
            certData.setCertVerifier(ace.certVerifier);
        }
       
        XmlOptions xo = new XmlOptions();
        xo.setCharacterEncoding("UTF-8");
        Map<String,String> nsMap = new HashMap<String,String>();
        nsMap.put(passwordUri.toString(),"p");
        nsMap.put(certificateUri.toString(), "c");
        xo.setUseDefaultNamespace();
        xo.setSaveSuggestedPrefixes(nsMap);
        xo.setSaveNamespacesFirst();
        xo.setSaveAggressiveNamespaces();
        // setting standalone doesn't work with xmlbeans-2.3
        xo.setSaveNoXmlDecl();
       
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bos.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n".getBytes("UTF-8"));
        ed.save(bos, xo);

        final byte buf[] = new byte[5000];       
        LittleEndianByteArrayOutputStream leos = new LittleEndianByteArrayOutputStream(buf, 0);
        EncryptionInfo info = builder.getInfo();

        // EncryptionVersionInfo (4 bytes): A Version structure (section 2.1.4), where
        // Version.vMajor MUST be 0x0004 and Version.vMinor MUST be 0x0004
        leos.writeShort(info.getVersionMajor());
        leos.writeShort(info.getVersionMinor());
        // Reserved (4 bytes): A value that MUST be 0x00000040
        leos.writeInt(info.getEncryptionFlags());
        leos.write(bos.toByteArray());
       
        dir.createDocument("EncryptionInfo", leos.getWriteIndex(), new POIFSWriterListener() {
            public void processPOIFSWriterEvent(POIFSWriterEvent event) {
                try {
                    event.getStream().write(buf, 0, event.getLimit());
                } catch (IOException e) {
                    throw new EncryptedDocumentException(e);
                }
            }
        });
    }
View Full Code Here

            try {
                byte hmacValueFilled[] = getBlock0(hmacValue, getNextBlockSize(hmacValue.length, blockSize));
                byte encryptedHmacValue[] = cipher.doFinal(hmacValueFilled);
                header.setEncryptedHmacValue(encryptedHmacValue);
            } catch (GeneralSecurityException e) {
                throw new EncryptedDocumentException(e);
            }

            createEncryptionInfoEntry(dir);
           
            int oleStreamSize = (int)(fileOut.length()+LittleEndianConsts.LONG_SIZE);
View Full Code Here

                fis.close();
                fileOut.delete();

                leos.close();
            } catch (IOException e) {
                throw new EncryptedDocumentException(e);
            }
        }
View Full Code Here

TOP

Related Classes of org.apache.poi.EncryptedDocumentException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.