org.apache.poi.poifs.crypt
Class CryptoFunctions

java.lang.Object
  extended by org.apache.poi.poifs.crypt.CryptoFunctions

public class CryptoFunctions
extends java.lang.Object

Helper functions used for standard and agile encryption


Constructor Summary
CryptoFunctions()
           
 
Method Summary
static byte[] generateIv(HashAlgorithm hashAlgorithm, byte[] salt, byte[] blockKey, int blockSize)
          2.3.4.12 Initialization Vector Generation (Agile Encryption) Initialization vectors are used in all cases for agile encryption.
static byte[] generateKey(byte[] passwordHash, HashAlgorithm hashAlgorithm, byte[] blockKey, int keySize)
          2.3.4.11 Encryption Key Generation (Agile Encryption) ...
static byte[] getBlock0(byte[] hash, int size)
           
static byte[] getBlock36(byte[] hash, int size)
           
static javax.crypto.Cipher getCipher(javax.crypto.SecretKey key, CipherAlgorithm cipherAlgorithm, ChainingMode chain, byte[] vec, int cipherMode)
           
static javax.crypto.Cipher getCipher(javax.crypto.SecretKey key, CipherAlgorithm cipherAlgorithm, ChainingMode chain, byte[] vec, int cipherMode, java.lang.String padding)
           
static javax.crypto.Mac getMac(HashAlgorithm hashAlgorithm)
           
static java.security.MessageDigest getMessageDigest(HashAlgorithm hashAlgorithm)
           
static byte[] getUtf16LeString(java.lang.String str)
           
static byte[] hashPassword(java.lang.String password, HashAlgorithm hashAlgorithm, byte[] salt, int spinCount)
          2.3.4.7 ECMA-376 Document Encryption Key Generation (Standard Encryption) 2.3.4.11 Encryption Key Generation (Agile Encryption) The encryption key for ECMA-376 document encryption [ECMA-376] using agile encryption MUST be generated by using the following method, which is derived from PKCS #5: Password-Based Cryptography Version 2.0 [RFC2898].
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CryptoFunctions

public CryptoFunctions()
Method Detail

hashPassword

public static byte[] hashPassword(java.lang.String password,
                                  HashAlgorithm hashAlgorithm,
                                  byte[] salt,
                                  int spinCount)
2.3.4.7 ECMA-376 Document Encryption Key Generation (Standard Encryption) 2.3.4.11 Encryption Key Generation (Agile Encryption) The encryption key for ECMA-376 document encryption [ECMA-376] using agile encryption MUST be generated by using the following method, which is derived from PKCS #5: Password-Based Cryptography Version 2.0 [RFC2898]. Let H() be a hashing algorithm as determined by the PasswordKeyEncryptor.hashAlgorithm element, H_n be the hash data of the n-th iteration, and a plus sign (+) represent concatenation. The password MUST be provided as an array of Unicode characters. Limitations on the length of the password and the characters used by the password are implementation-dependent. The initial password hash is generated as follows: - H_0 = H(salt + password) The salt used MUST be generated randomly. The salt MUST be stored in the PasswordKeyEncryptor.saltValue element contained within the \EncryptionInfo stream (1) as specified in section 2.3.4.10. The hash is then iterated by using the following approach: - H_n = H(iterator + H_n-1) where iterator is an unsigned 32-bit value that is initially set to 0x00000000 and then incremented monotonically on each iteration until PasswordKey.spinCount iterations have been performed. The value of iterator on the last iteration MUST be one less than PasswordKey.spinCount. For POI, H_final will be calculated by generateKey(byte[],HashAlgorithm,byte[],int)

Parameters:
password -
hashAlgorithm -
salt -
spinCount -
Returns:
the hashed password

generateIv

public static byte[] generateIv(HashAlgorithm hashAlgorithm,
                                byte[] salt,
                                byte[] blockKey,
                                int blockSize)
2.3.4.12 Initialization Vector Generation (Agile Encryption) Initialization vectors are used in all cases for agile encryption. An initialization vector MUST be generated by using the following method, where H() is a hash function that MUST be the same as specified in section 2.3.4.11 and a plus sign (+) represents concatenation: 1. If a blockKey is provided, let IV be a hash of the KeySalt and the following value: blockKey: IV = H(KeySalt + blockKey) 2. If a blockKey is not provided, let IV be equal to the following value: KeySalt:IV = KeySalt. 3. If the number of bytes in the value of IV is less than the the value of the blockSize attribute corresponding to the cipherAlgorithm attribute, pad the array of bytes by appending 0x36 until the array is blockSize bytes. If the array of bytes is larger than blockSize bytes, truncate the array to blockSize bytes.


generateKey

public static byte[] generateKey(byte[] passwordHash,
                                 HashAlgorithm hashAlgorithm,
                                 byte[] blockKey,
                                 int keySize)
2.3.4.11 Encryption Key Generation (Agile Encryption) ... continued ... The final hash data that is used for an encryption key is then generated by using the following method: - H_final = H(H_n + blockKey) where blockKey represents an array of bytes used to prevent two different blocks from encrypting to the same cipher text. If the size of the resulting H_final is smaller than that of PasswordKeyEncryptor.keyBits, the key MUST be padded by appending bytes with a value of 0x36. If the hash value is larger in size than PasswordKeyEncryptor.keyBits, the key is obtained by truncating the hash value.

Parameters:
passwordHash -
hashAlgorithm -
blockKey -
keySize -
Returns:
intermediate key

getCipher

public static javax.crypto.Cipher getCipher(javax.crypto.SecretKey key,
                                            CipherAlgorithm cipherAlgorithm,
                                            ChainingMode chain,
                                            byte[] vec,
                                            int cipherMode)

getCipher

public static javax.crypto.Cipher getCipher(javax.crypto.SecretKey key,
                                            CipherAlgorithm cipherAlgorithm,
                                            ChainingMode chain,
                                            byte[] vec,
                                            int cipherMode,
                                            java.lang.String padding)
Parameters:
key -
chain -
vec -
cipherMode - Cipher.DECRYPT_MODE or Cipher.ENCRYPT_MODE
Returns:
the requested cipher
Throws:
java.security.GeneralSecurityException

getBlock36

public static byte[] getBlock36(byte[] hash,
                                int size)

getBlock0

public static byte[] getBlock0(byte[] hash,
                               int size)

getUtf16LeString

public static byte[] getUtf16LeString(java.lang.String str)

getMessageDigest

public static java.security.MessageDigest getMessageDigest(HashAlgorithm hashAlgorithm)

getMac

public static javax.crypto.Mac getMac(HashAlgorithm hashAlgorithm)


Copyright 2014 The Apache Software Foundation or its licensors, as applicable.