Search in sources :

Example 11 with HashAlgorithm

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

the class BinaryRC4Encryptor method confirmPassword.

@Override
public void confirmPassword(String password, byte[] keySpec, byte[] keySalt, byte[] verifier, byte[] verifierSalt, byte[] integritySalt) {
    BinaryRC4EncryptionVerifier ver = (BinaryRC4EncryptionVerifier) getEncryptionInfo().getVerifier();
    ver.setSalt(verifierSalt);
    SecretKey skey = BinaryRC4Decryptor.generateSecretKey(password, ver);
    setSecretKey(skey);
    try {
        Cipher cipher = BinaryRC4Decryptor.initCipherForBlock(null, 0, getEncryptionInfo(), skey, Cipher.ENCRYPT_MODE);
        byte[] encryptedVerifier = new byte[16];
        cipher.update(verifier, 0, 16, encryptedVerifier);
        ver.setEncryptedVerifier(encryptedVerifier);
        HashAlgorithm hashAlgo = ver.getHashAlgorithm();
        MessageDigest hashAlg = CryptoFunctions.getMessageDigest(hashAlgo);
        byte[] calcVerifierHash = hashAlg.digest(verifier);
        byte[] encryptedVerifierHash = cipher.doFinal(calcVerifierHash);
        ver.setEncryptedVerifierHash(encryptedVerifierHash);
    } catch (GeneralSecurityException e) {
        throw new EncryptedDocumentException("Password confirmation failed", e);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) GeneralSecurityException(java.security.GeneralSecurityException) Cipher(javax.crypto.Cipher) MessageDigest(java.security.MessageDigest) HashAlgorithm(org.apache.poi.poifs.crypt.HashAlgorithm)

Aggregations

HashAlgorithm (org.apache.poi.poifs.crypt.HashAlgorithm)11 SecretKey (javax.crypto.SecretKey)7 Cipher (javax.crypto.Cipher)6 SecretKeySpec (javax.crypto.spec.SecretKeySpec)6 MessageDigest (java.security.MessageDigest)5 GeneralSecurityException (java.security.GeneralSecurityException)4 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)4 CryptoFunctions.getCipher (org.apache.poi.poifs.crypt.CryptoFunctions.getCipher)3 Mac (javax.crypto.Mac)2 CipherAlgorithm (org.apache.poi.poifs.crypt.CipherAlgorithm)2 EncryptionVerifier (org.apache.poi.poifs.crypt.EncryptionVerifier)2 STHashAlgorithm (com.microsoft.schemas.office.x2006.encryption.STHashAlgorithm)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 BigInteger (java.math.BigInteger)1 ChainingMode (org.apache.poi.poifs.crypt.ChainingMode)1 EncryptionHeader (org.apache.poi.poifs.crypt.EncryptionHeader)1 AgileCertificateEntry (org.apache.poi.poifs.crypt.agile.AgileEncryptionVerifier.AgileCertificateEntry)1 XmlCursor (org.apache.xmlbeans.XmlCursor)1