Search in sources :

Example 1 with EncryptionHeader

use of org.apache.poi.poifs.crypt.EncryptionHeader in project poi by apache.

the class StandardDecryptor method getCipher.

private Cipher getCipher(SecretKey key) {
    EncryptionHeader em = getEncryptionInfo().getHeader();
    ChainingMode cm = em.getChainingMode();
    assert (cm == ChainingMode.ecb);
    return CryptoFunctions.getCipher(key, em.getCipherAlgorithm(), cm, null, Cipher.DECRYPT_MODE);
}
Also used : ChainingMode(org.apache.poi.poifs.crypt.ChainingMode) EncryptionHeader(org.apache.poi.poifs.crypt.EncryptionHeader)

Example 2 with EncryptionHeader

use of org.apache.poi.poifs.crypt.EncryptionHeader in project poi by apache.

the class AgileDecryptor method initCipherForBlock.

protected static Cipher initCipherForBlock(Cipher existing, int block, boolean lastChunk, EncryptionInfo encryptionInfo, SecretKey skey, int encryptionMode) throws GeneralSecurityException {
    EncryptionHeader header = encryptionInfo.getHeader();
    String padding = (lastChunk ? "PKCS5Padding" : "NoPadding");
    if (existing == null || !existing.getAlgorithm().endsWith(padding)) {
        existing = getCipher(skey, header.getCipherAlgorithm(), header.getChainingMode(), header.getKeySalt(), encryptionMode, padding);
    }
    byte[] blockKey = new byte[4];
    LittleEndian.putInt(blockKey, 0, block);
    byte[] iv = generateIv(header.getHashAlgorithm(), header.getKeySalt(), blockKey, header.getBlockSize());
    AlgorithmParameterSpec aps;
    if (header.getCipherAlgorithm() == CipherAlgorithm.rc2) {
        aps = new RC2ParameterSpec(skey.getEncoded().length * 8, iv);
    } else {
        aps = new IvParameterSpec(iv);
    }
    existing.init(encryptionMode, skey, aps);
    return existing;
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec) RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) EncryptionHeader(org.apache.poi.poifs.crypt.EncryptionHeader)

Example 3 with EncryptionHeader

use of org.apache.poi.poifs.crypt.EncryptionHeader in project poi by apache.

the class CryptoAPIDecryptor method initCipherForBlock.

protected static Cipher initCipherForBlock(Cipher cipher, int block, EncryptionInfo encryptionInfo, SecretKey skey, int encryptMode) throws GeneralSecurityException {
    EncryptionVerifier ver = encryptionInfo.getVerifier();
    HashAlgorithm hashAlgo = ver.getHashAlgorithm();
    byte[] blockKey = new byte[4];
    LittleEndian.putUInt(blockKey, 0, block);
    MessageDigest hashAlg = CryptoFunctions.getMessageDigest(hashAlgo);
    hashAlg.update(skey.getEncoded());
    byte[] encKey = hashAlg.digest(blockKey);
    EncryptionHeader header = encryptionInfo.getHeader();
    int keyBits = header.getKeySize();
    encKey = CryptoFunctions.getBlock0(encKey, keyBits / 8);
    if (keyBits == 40) {
        encKey = CryptoFunctions.getBlock0(encKey, 16);
    }
    SecretKey key = new SecretKeySpec(encKey, skey.getAlgorithm());
    if (cipher == null) {
        cipher = CryptoFunctions.getCipher(key, header.getCipherAlgorithm(), null, null, encryptMode);
    } else {
        cipher.init(encryptMode, key);
    }
    return cipher;
}
Also used : EncryptionVerifier(org.apache.poi.poifs.crypt.EncryptionVerifier) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) MessageDigest(java.security.MessageDigest) EncryptionHeader(org.apache.poi.poifs.crypt.EncryptionHeader) HashAlgorithm(org.apache.poi.poifs.crypt.HashAlgorithm)

Aggregations

EncryptionHeader (org.apache.poi.poifs.crypt.EncryptionHeader)3 MessageDigest (java.security.MessageDigest)1 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)1 SecretKey (javax.crypto.SecretKey)1 IvParameterSpec (javax.crypto.spec.IvParameterSpec)1 RC2ParameterSpec (javax.crypto.spec.RC2ParameterSpec)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1 ChainingMode (org.apache.poi.poifs.crypt.ChainingMode)1 EncryptionVerifier (org.apache.poi.poifs.crypt.EncryptionVerifier)1 HashAlgorithm (org.apache.poi.poifs.crypt.HashAlgorithm)1