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; }
protected int getKeySizeInBytes() { return encryptionInfo.getHeader().getKeySize()/8; }
@Override public StandardEncryptionHeader clone() throws CloneNotSupportedException { return (StandardEncryptionHeader)super.clone(); } }
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); }
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; }
public ChunkedCipherInputStream(DocumentInputStream stream, long size) throws GeneralSecurityException { _size = size; _stream = stream; _cipher = getCipher(_info.getHeader().getAlgorithm(), _info.getHeader().getCipherMode(), _secretKey, _info.getHeader().getKeySalt()); }
/** * Sets the keySize (in bits). Before calling this method, make sure * to set the cipherAlgorithm, as the amount of keyBits gets validated against * the list of allowed keyBits of the corresponding cipherAlgorithm * * @param keyBits */ protected void setKeySize(int keyBits) { this.keyBits = keyBits; for (int allowedBits : getCipherAlgorithm().allowedKeySize) { if (allowedBits == keyBits) { return; } } throw new EncryptedDocumentException("KeySize "+keyBits+" not allowed for cipher "+getCipherAlgorithm()); }
private byte[] nextChunk() throws GeneralSecurityException, IOException { int index = (int)(_pos >> 12); byte[] blockKey = new byte[4]; LittleEndian.putInt(blockKey, 0, index); byte[] iv = generateIv(_info.getHeader().getAlgorithm(), _info.getHeader().getKeySalt(), blockKey); _cipher.init(Cipher.DECRYPT_MODE, _secretKey, new IvParameterSpec(iv)); if (_lastIndex != index) _stream.skip((index - _lastIndex) << 12); byte[] block = new byte[Math.min(_stream.available(), 4096)]; _stream.readFully(block); _lastIndex = index + 1; return _cipher.doFinal(block); } }
public EncryptionInfo(DirectoryNode dir) throws IOException { DocumentInputStream dis = dir.createDocumentInputStream("EncryptionInfo"); versionMajor = dis.readShort(); versionMinor = dis.readShort(); encryptionFlags = dis.readInt(); if (versionMajor == 4 && versionMinor == 4 && encryptionFlags == 0x40) { StringBuilder builder = new StringBuilder(); byte[] xmlDescriptor = new byte[dis.available()]; dis.read(xmlDescriptor); for (byte b : xmlDescriptor) builder.append((char)b); String descriptor = builder.toString(); header = new EncryptionHeader(descriptor); verifier = new EncryptionVerifier(descriptor); } else { int hSize = dis.readInt(); header = new EncryptionHeader(dis); if (header.getAlgorithm()==EncryptionHeader.ALGORITHM_RC4) { verifier = new EncryptionVerifier(dis, 20); } else { verifier = new EncryptionVerifier(dis, 32); } } }
@SuppressWarnings("unused") protected int getBlockSizeInBytes() { return encryptionInfo.getHeader().getBlockSize(); }
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); }
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; }
public ChunkedCipherInputStream(DocumentInputStream stream, long size) throws GeneralSecurityException { _size = size; _stream = stream; _cipher = getCipher(_info.getHeader().getAlgorithm(), _info.getHeader().getCipherMode(), _secretKey, _info.getHeader().getKeySalt()); }
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); byte encKey[] = CryptoFunctions.generateKey(skey.getEncoded(), hashAlgo, blockKey, 16); SecretKey key = new SecretKeySpec(encKey, skey.getAlgorithm()); if (cipher == null) { EncryptionHeader em = encryptionInfo.getHeader(); cipher = CryptoFunctions.getCipher(key, em.getCipherAlgorithm(), null, null, encryptMode); } else { cipher.init(encryptMode, key); } return cipher; }
private byte[] nextChunk() throws GeneralSecurityException, IOException { int index = (int)(_pos >> 12); byte[] blockKey = new byte[4]; LittleEndian.putInt(blockKey, 0, index); byte[] iv = generateIv(_info.getHeader().getAlgorithm(), _info.getHeader().getKeySalt(), blockKey); _cipher.init(Cipher.DECRYPT_MODE, _secretKey, new IvParameterSpec(iv)); if (_lastIndex != index) _stream.skip((index - _lastIndex) << 12); byte[] block = new byte[Math.min(_stream.available(), 4096)]; _stream.readFully(block); _lastIndex = index + 1; return _cipher.doFinal(block); } }
public EncryptionInfo(DirectoryNode dir) throws IOException { DocumentInputStream dis = dir.createDocumentInputStream("EncryptionInfo"); versionMajor = dis.readShort(); versionMinor = dis.readShort(); encryptionFlags = dis.readInt(); if (versionMajor == 4 && versionMinor == 4 && encryptionFlags == 0x40) { StringBuilder builder = new StringBuilder(); byte[] xmlDescriptor = new byte[dis.available()]; dis.read(xmlDescriptor); for (byte b : xmlDescriptor) builder.append((char)b); String descriptor = builder.toString(); header = new EncryptionHeader(descriptor); verifier = new EncryptionVerifier(descriptor); } else { int hSize = dis.readInt(); header = new EncryptionHeader(dis); if (header.getAlgorithm()==EncryptionHeader.ALGORITHM_RC4) { verifier = new EncryptionVerifier(dis, 20); } else { verifier = new EncryptionVerifier(dis, 32); } } }
@SuppressWarnings("unused") protected int getBlockSizeInBytes() { return encryptionInfo.getHeader().getBlockSize(); }
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; }
this.ca = d.getEncryptionInfo().getHeader().getCipherAlgorithm(); this.cm = d.getEncryptionInfo().getHeader().getChainingMode(); try { if (!d.verifyPassword(this.hocr.getPassword())) {
protected int getKeySizeInBytes() { return getEncryptionInfo().getHeader().getKeySize()/8; }