@Override protected Cipher initCipherForBlock(Cipher cipher, int block, boolean lastChunk) throws GeneralSecurityException { return BinaryRC4Decryptor.initCipherForBlock(cipher, block, getEncryptionInfo(), getSecretKey(), Cipher.ENCRYPT_MODE); }
protected BinaryRC4EncryptionHeader() { setCipherAlgorithm(CipherAlgorithm.rc4); setKeySize(40); setBlockSize(-1); setCipherProvider(CipherProvider.rc4); setHashAlgorithm(HashAlgorithm.md5); setSizeExtra(0); setFlags(0); setCspName(""); setChainingMode(null); }
protected BinaryRC4EncryptionVerifier() { setSpinCount(-1); setCipherAlgorithm(CipherAlgorithm.rc4); setChainingMode(null); setEncryptedKey(null); setHashAlgorithm(HashAlgorithm.md5); }
protected BinaryRC4EncryptionVerifier(LittleEndianInput is) { byte salt[] = new byte[16]; is.readFully(salt); setSalt(salt); byte encryptedVerifier[] = new byte[16]; is.readFully(encryptedVerifier); setEncryptedVerifier(encryptedVerifier); byte encryptedVerifierHash[] = new byte[16]; is.readFully(encryptedVerifierHash); setEncryptedVerifierHash(encryptedVerifierHash); setSpinCount(-1); setCipherAlgorithm(CipherAlgorithm.rc4); setChainingMode(null); setEncryptedKey(null); setHashAlgorithm(HashAlgorithm.md5); }
@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); } }
@Override public void initialize(EncryptionInfo info, CipherAlgorithm cipherAlgorithm, HashAlgorithm hashAlgorithm, int keyBits, int blockSize, ChainingMode chainingMode) { info.setHeader(new BinaryRC4EncryptionHeader()); info.setVerifier(new BinaryRC4EncryptionVerifier()); Decryptor dec = new BinaryRC4Decryptor(); dec.setEncryptionInfo(info); info.setDecryptor(dec); Encryptor enc = new BinaryRC4Encryptor(); enc.setEncryptionInfo(info); info.setEncryptor(enc); } }
@Override public Cipher initCipherForBlock(Cipher cipher, int block) throws GeneralSecurityException { return initCipherForBlock(cipher, block, getEncryptionInfo(), getSecretKey(), Cipher.DECRYPT_MODE); }
@Override public void write(LittleEndianByteArrayOutputStream bos) { byte salt[] = getSalt(); assert (salt.length == 16); bos.write(salt); byte encryptedVerifier[] = getEncryptedVerifier(); assert (encryptedVerifier.length == 16); bos.write(encryptedVerifier); byte encryptedVerifierHash[] = getEncryptedVerifierHash(); assert (encryptedVerifierHash.length == 16); bos.write(encryptedVerifierHash); }
@Override public OutputStream getDataStream(DirectoryNode dir) throws IOException, GeneralSecurityException { return new BinaryRC4CipherOutputStream(dir); }
@Override public InputStream getDataStream(InputStream stream, int size, int initialPos) throws IOException, GeneralSecurityException { return new BinaryRC4CipherInputStream(stream, size, initialPos); }
protected int getKeySizeInBytes() { return getEncryptionInfo().getHeader().getKeySize() / 8; }
@Override protected Cipher initCipherForBlock(Cipher existing, int block) throws GeneralSecurityException { return BinaryRC4Decryptor.this.initCipherForBlock(existing, block); }
@Override protected void createEncryptionInfoEntry(DirectoryNode dir, File tmpFile) throws IOException, GeneralSecurityException { BinaryRC4Encryptor.this.createEncryptionInfoEntry(dir); }
@Override public void confirmPassword(String password) { Random r = new SecureRandom(); byte salt[] = new byte[16]; byte verifier[] = new byte[16]; r.nextBytes(salt); r.nextBytes(verifier); confirmPassword(password, null, null, verifier, salt, null); }
@Override public void flush() throws IOException { writeChunk(false); super.flush(); } }
@Override public void initialize(EncryptionInfo info, LittleEndianInput dis) throws IOException { int vMajor = info.getVersionMajor(); int vMinor = info.getVersionMinor(); assert (vMajor == 1 && vMinor == 1); info.setHeader(new BinaryRC4EncryptionHeader()); info.setVerifier(new BinaryRC4EncryptionVerifier(dis)); Decryptor dec = new BinaryRC4Decryptor(); dec.setEncryptionInfo(info); info.setDecryptor(dec); Encryptor enc = new BinaryRC4Encryptor(); enc.setEncryptionInfo(info); info.setEncryptor(enc); }
@Override public BinaryRC4CipherOutputStream getDataStream(OutputStream stream, int initialOffset) throws IOException, GeneralSecurityException { return new BinaryRC4CipherOutputStream(stream); }
@Override @SuppressWarnings("resource") public ChunkedCipherInputStream getDataStream(DirectoryNode dir) throws IOException, GeneralSecurityException { DocumentInputStream dis = dir.createDocumentInputStream(DEFAULT_POIFS_ENTRY); length = dis.readLong(); return new BinaryRC4CipherInputStream(dis, length); }
protected void createEncryptionInfoEntry(DirectoryNode dir) throws IOException { DataSpaceMapUtils.addDefaultDataSpace(dir); final EncryptionInfo info = getEncryptionInfo(); final BinaryRC4EncryptionHeader header = (BinaryRC4EncryptionHeader)info.getHeader(); final BinaryRC4EncryptionVerifier verifier = (BinaryRC4EncryptionVerifier)info.getVerifier(); EncryptionRecord er = new EncryptionRecord() { @Override public void write(LittleEndianByteArrayOutputStream bos) { bos.writeShort(info.getVersionMajor()); bos.writeShort(info.getVersionMinor()); header.write(bos); verifier.write(bos); } }; DataSpaceMapUtils.createEncryptionEntry(dir, "EncryptionInfo", er); }