byte[] key = FilePathEncrypt.getPasswordBytes(encryptionKey); encryptedFile = file; file = new FilePathEncrypt.FileEncrypt(fileName, key, file);
@Override public int write(ByteBuffer src, long position) throws IOException { init(); int len = src.remaining(); if ((position & BLOCK_SIZE_MASK) != 0 || int readLen = Math.min(l, available); if (readLen > 0) { readInternal(temp, p, readLen); temp.rewind(); temp.limit(l); temp.rewind(); writeInternal(temp, p, l); long p2 = position + len; size = Math.max(size, p2); if (plus > 0) { temp = ByteBuffer.allocate(plus); writeFully(base, p + HEADER_LENGTH + l, temp); writeInternal(src, position, len); long p2 = position + len; size = Math.max(size, p2);
private void init() throws IOException { if (xts != null) { return; } this.size = base.size() - HEADER_LENGTH; boolean newFile = size < 0; byte[] salt; if (newFile) { byte[] header = Arrays.copyOf(HEADER, BLOCK_SIZE); salt = MathUtils.secureRandomBytes(SALT_LENGTH); System.arraycopy(salt, 0, header, SALT_POS, salt.length); writeFully(base, 0, ByteBuffer.wrap(header)); size = 0; } else { salt = new byte[SALT_LENGTH]; readFully(base, SALT_POS, ByteBuffer.wrap(salt)); if ((size & BLOCK_SIZE_MASK) != 0) { size -= BLOCK_SIZE; } } AES cipher = new AES(); cipher.setKey(SHA256.getPBKDF2( encryptionKey, salt, HASH_ITERATIONS, 16)); encryptionKey = null; xts = new XTS(cipher); }
byte[] key = FilePathEncrypt.getPasswordBytes(encryptionKey); encryptedFile = file; file = new FilePathEncrypt.FileEncrypt(fileName, key, file);
byte[] key = FilePathEncrypt.getPasswordBytes(encryptionKey); encryptedFile = file; file = new FilePathEncrypt.FileEncrypt(fileName, key, file);
byte[] key = FilePathEncrypt.getPasswordBytes(encryptionKey); encryptedFile = file; file = new FilePathEncrypt.FileEncrypt(fileName, key, file);
try { if (decryptKey != null) { fileIn = new FilePathEncrypt.FileEncrypt(fileName, decryptKey, fileIn); fileOut = FilePath.get(temp).open("rw"); if (encryptKey != null) { fileOut = new FilePathEncrypt.FileEncrypt(temp, encryptKey, fileOut);
try { if (decryptKey != null) { fileIn = new FilePathEncrypt.FileEncrypt(fileName, decryptKey, fileIn); fileOut = FilePath.get(temp).open("rw"); if (encryptKey != null) { fileOut = new FilePathEncrypt.FileEncrypt(temp, encryptKey, fileOut);
@Override public int write(ByteBuffer src, long position) throws IOException { init(); int len = src.remaining(); if ((position & BLOCK_SIZE_MASK) != 0 || int readLen = Math.min(l, available); if (readLen > 0) { readInternal(temp, p, readLen); temp.rewind(); temp.limit(l); temp.rewind(); writeInternal(temp, p, l); long p2 = position + len; size = Math.max(size, p2); writeInternal(src, position, len); long p2 = position + len; size = Math.max(size, p2);
@Override public int write(ByteBuffer src, long position) throws IOException { init(); int len = src.remaining(); if ((position & BLOCK_SIZE_MASK) != 0 || int readLen = Math.min(l, available); if (readLen > 0) { readInternal(temp, p, readLen); temp.rewind(); temp.limit(l); temp.rewind(); writeInternal(temp, p, l); long p2 = position + len; size = Math.max(size, p2); writeInternal(src, position, len); long p2 = position + len; size = Math.max(size, p2);
return 0; init(); len = (int) Math.min(len, size - position); if (position >= size) { int l = (len + offset + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE; ByteBuffer temp = ByteBuffer.allocate(l); readInternal(temp, p, l); temp.flip(); temp.limit(offset + len); return len; readInternal(dst, position, len); return len;
return 0; init(); len = (int) Math.min(len, size - position); if (position >= size) { int l = (len + offset + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE; ByteBuffer temp = ByteBuffer.allocate(l); readInternal(temp, p, l); temp.flip(); temp.limit(offset + len); return len; readInternal(dst, position, len); return len;
@Override public FileChannel truncate(long newSize) throws IOException { init(); if (newSize > size) { return this; } if (newSize < 0) { throw new IllegalArgumentException("newSize: " + newSize); } int offset = (int) (newSize & BLOCK_SIZE_MASK); if (offset > 0) { base.truncate(newSize + HEADER_LENGTH + BLOCK_SIZE); } else { base.truncate(newSize + HEADER_LENGTH); } this.size = newSize; pos = Math.min(pos, size); return this; }
@Override public FileChannel truncate(long newSize) throws IOException { init(); if (newSize > size) { return this; } if (newSize < 0) { throw new IllegalArgumentException("newSize: " + newSize); } int offset = (int) (newSize & BLOCK_SIZE_MASK); if (offset > 0) { base.truncate(newSize + HEADER_LENGTH + BLOCK_SIZE); } else { base.truncate(newSize + HEADER_LENGTH); } this.size = newSize; pos = Math.min(pos, size); return this; }
private void writeInternal(ByteBuffer src, long position, int len) throws IOException { ByteBuffer crypt = ByteBuffer.allocate(len); crypt.put(src); crypt.flip(); long block = position / BLOCK_SIZE; int x = 0, l = len; while (l > 0) { xts.encrypt(block++, BLOCK_SIZE, crypt.array(), crypt.arrayOffset() + x); x += BLOCK_SIZE; l -= BLOCK_SIZE; } writeFully(base, position + HEADER_LENGTH, crypt); }
private void writeInternal(ByteBuffer src, long position, int len) throws IOException { ByteBuffer crypt = ByteBuffer.allocate(len); crypt.put(src); crypt.flip(); long block = position / BLOCK_SIZE; int x = 0, l = len; while (l > 0) { xts.encrypt(block++, BLOCK_SIZE, crypt.array(), crypt.arrayOffset() + x); x += BLOCK_SIZE; l -= BLOCK_SIZE; } writeFully(base, position + HEADER_LENGTH, crypt); }
@Override public long size() throws IOException { init(); return size; }
private void readInternal(ByteBuffer dst, long position, int len) throws IOException { int x = dst.position(); readFully(base, position + HEADER_LENGTH, dst); long block = position / BLOCK_SIZE; while (len > 0) { xts.decrypt(block++, BLOCK_SIZE, dst.array(), dst.arrayOffset() + x); x += BLOCK_SIZE; len -= BLOCK_SIZE; } }
@Override public int read(ByteBuffer dst) throws IOException { int len = read(dst, pos); if (len > 0) { pos += len; } return len; }
@Override public FileChannel open(String mode) throws IOException { String[] parsed = parse(name); FileChannel file = FileUtils.open(parsed[1], mode); byte[] passwordBytes = parsed[0].getBytes(Constants.UTF8); return new FileEncrypt(name, passwordBytes, file); }