@Override public void confirmPassword(String password) { // see [MS-OFFCRYPTO] - 2.3.3 EncryptionVerifier Random r = new SecureRandom(); byte[] salt = new byte[16], verifier = new byte[16]; r.nextBytes(salt); r.nextBytes(verifier); confirmPassword(password, null, null, salt, verifier, null); }
@Override public OutputStream getDataStream(final DirectoryNode dir) throws IOException, GeneralSecurityException { createEncryptionInfoEntry(dir); DataSpaceMapUtils.addDefaultDataSpace(dir); return new StandardCipherOutputStream(dir); }
StandardEncryptionVerifier ver = (StandardEncryptionVerifier)getEncryptionInfo().getVerifier(); SecretKey secretKey = generateSecretKey(password, ver, getKeySizeInBytes()); setSecretKey(secretKey); Cipher cipher = getCipher(secretKey, null);
dec.setEncryptionInfo(info); info.setDecryptor(dec); StandardEncryptor enc = new StandardEncryptor(); enc.setEncryptionInfo(info); info.setEncryptor(enc);
protected int getKeySizeInBytes() { return getEncryptionInfo().getHeader().getKeySize()/8; }
@SuppressWarnings("resource") private StandardCipherOutputStream(DirectoryNode dir, File fileOut) throws IOException { // although not documented, we need the same padding as with agile encryption // and instead of calculating the missing bytes for the block size ourselves // we leave it up to the CipherOutputStream, which generates/saves them on close() // ... we can't use "NoPadding" here // // see also [MS-OFFCRYPT] - 2.3.4.15 // The final data block MUST be padded to the next integral multiple of the // KeyData.blockSize value. Any padding bytes can be used. Note that the StreamSize // field of the EncryptedPackage field specifies the number of bytes of // unencrypted data as specified in section 2.3.4.4. super( new CipherOutputStream(new FileOutputStream(fileOut), getCipher(getSecretKey(), "PKCS5Padding")) ); this.fileOut = fileOut; this.dir = dir; }
dec.setEncryptionInfo(info); info.setDecryptor(dec); StandardEncryptor enc = new StandardEncryptor(); enc.setEncryptionInfo(info); info.setEncryptor(enc);
protected void createEncryptionInfoEntry(DirectoryNode dir) throws IOException { final EncryptionInfo info = getEncryptionInfo(); final StandardEncryptionHeader header = (StandardEncryptionHeader)info.getHeader(); final StandardEncryptionVerifier verifier = (StandardEncryptionVerifier)info.getVerifier(); EncryptionRecord er = new EncryptionRecord(){ @Override public void write(LittleEndianByteArrayOutputStream bos) { bos.writeShort(info.getVersionMajor()); bos.writeShort(info.getVersionMinor()); bos.writeInt(info.getEncryptionFlags()); header.write(bos); verifier.write(bos); } }; createEncryptionEntry(dir, "EncryptionInfo", er); // TODO: any properties??? }
@SuppressWarnings("resource") private StandardCipherOutputStream(DirectoryNode dir, File fileOut) throws IOException { // although not documented, we need the same padding as with agile encryption // and instead of calculating the missing bytes for the block size ourselves // we leave it up to the CipherOutputStream, which generates/saves them on close() // ... we can't use "NoPadding" here // // see also [MS-OFFCRYPT] - 2.3.4.15 // The final data block MUST be padded to the next integral multiple of the // KeyData.blockSize value. Any padding bytes can be used. Note that the StreamSize // field of the EncryptedPackage field specifies the number of bytes of // unencrypted data as specified in section 2.3.4.4. super( new CipherOutputStream(new FileOutputStream(fileOut), getCipher(getSecretKey(), "PKCS5Padding")) ); this.fileOut = fileOut; this.dir = dir; }
StandardEncryptionVerifier ver = (StandardEncryptionVerifier)getEncryptionInfo().getVerifier(); SecretKey secretKey = generateSecretKey(password, ver, getKeySizeInBytes()); setSecretKey(secretKey); Cipher cipher = getCipher(secretKey, null);
private Cipher getCipher(SecretKey key, String padding) { EncryptionVerifier ver = getEncryptionInfo().getVerifier(); return CryptoFunctions.getCipher(key, ver.getCipherAlgorithm(), ver.getChainingMode(), null, Cipher.ENCRYPT_MODE, padding); }
@Override public void confirmPassword(String password) { // see [MS-OFFCRYPTO] - 2.3.3 EncryptionVerifier Random r = new SecureRandom(); byte[] salt = new byte[16], verifier = new byte[16]; r.nextBytes(salt); r.nextBytes(verifier); confirmPassword(password, null, null, salt, verifier, null); }
@Override public OutputStream getDataStream(final DirectoryNode dir) throws IOException, GeneralSecurityException { createEncryptionInfoEntry(dir); DataSpaceMapUtils.addDefaultDataSpace(dir); return new StandardCipherOutputStream(dir); }
protected int getKeySizeInBytes() { return getEncryptionInfo().getHeader().getKeySize()/8; }
protected void createEncryptionInfoEntry(DirectoryNode dir) throws IOException { final EncryptionInfo info = getEncryptionInfo(); final StandardEncryptionHeader header = (StandardEncryptionHeader)info.getHeader(); final StandardEncryptionVerifier verifier = (StandardEncryptionVerifier)info.getVerifier(); EncryptionRecord er = new EncryptionRecord(){ @Override public void write(LittleEndianByteArrayOutputStream bos) { bos.writeShort(info.getVersionMajor()); bos.writeShort(info.getVersionMinor()); bos.writeInt(info.getEncryptionFlags()); header.write(bos); verifier.write(bos); } }; createEncryptionEntry(dir, "EncryptionInfo", er); // TODO: any properties??? }
private Cipher getCipher(SecretKey key, String padding) { EncryptionVerifier ver = getEncryptionInfo().getVerifier(); return CryptoFunctions.getCipher(key, ver.getCipherAlgorithm(), ver.getChainingMode(), null, Cipher.ENCRYPT_MODE, padding); }