use of i2p.bote.fileencryption.EncryptedInputStream in project i2p.i2p-bote by i2p.
the class DebugSupport method getUndecryptableFiles.
/**
* Tests all encrypted I2P-Bote files and returns a list containing those that
* cannot be decrypted.
* @return A list of problem files, or an empty list if no problems were found
* @throws PasswordException
* @throws IOException
* @throws GeneralSecurityException
*/
public List<File> getUndecryptableFiles() throws PasswordException, IOException, GeneralSecurityException {
// make sure the password is correct
byte[] password = passwordHolder.getPassword();
if (password == null)
throw new PasswordException();
File passwordFile = configuration.getPasswordFile();
boolean correct = FileEncryptionUtil.isPasswordCorrect(password, passwordFile);
if (!correct)
throw new PasswordException();
// make a list of all encrypted files
List<File> files = new ArrayList<File>();
files.add(configuration.getIdentitiesFile());
files.add(configuration.getAddressBookFile());
File[] emailFolders = new File[] { configuration.getInboxDir(), configuration.getOutboxDir(), configuration.getSentFolderDir(), configuration.getTrashFolderDir() };
;
for (File dir : emailFolders) files.addAll(Arrays.asList(dir.listFiles()));
for (Iterator<File> iter = files.iterator(); iter.hasNext(); ) {
File file = iter.next();
FileInputStream stream = new FileInputStream(file);
try {
Util.readBytes(new EncryptedInputStream(stream, password));
// no PasswordException or other exception occurred, so the file is good
iter.remove();
} catch (Exception e) {
// leave the file in the list and log
log.debug("Can't decrypt file <" + file.getAbsolutePath() + ">", e);
} finally {
if (stream != null)
stream.close();
}
}
return files;
}
use of i2p.bote.fileencryption.EncryptedInputStream in project i2p.i2p-bote by i2p.
the class Identities method readIdentities.
/**
* Reads <code>Identities</code> from the encrypted identities file. Each identity
* is defined by one line that contains two to four tab-separated fields:<br/>
* Email Identity key, Public Name, Description, and Email Address.
* The first two are mandatory, the last two are optional.<br/>
* <p/>
* Additionally, the file can set a default Email Identity by including a
* line that starts with "Default ", followed by an Email Destination key.<br/>
* The destination key must match one of the Email Identities defined in
* the file.
* <p/>
* An Email Identity key consists of two public keys and two private keys, whereas
* an Email Destination consists only of two public keys.
* @throws PasswordException
* @throws IOException
* @throws GeneralSecurityException
*/
private void readIdentities() throws PasswordException, IOException, GeneralSecurityException {
log.debug("Reading identities file: <" + identitiesFile.getAbsolutePath() + ">");
if (!identitiesFile.exists()) {
log.debug("Identities file does not exist: <" + identitiesFile.getAbsolutePath() + ">");
identities = new TreeSet<EmailIdentity>(new IdentityComparator());
return;
}
BufferedReader input = null;
try {
InputStream encryptedStream = new EncryptedInputStream(new FileInputStream(identitiesFile), passwordHolder);
input = new BufferedReader(new InputStreamReader(encryptedStream));
// No PasswordException occurred, so read the input stream
Properties properties = new Properties();
properties.load(new InputStreamReader(encryptedStream));
loadFromProperties(properties, false, false);
} finally {
if (input != null)
try {
input.close();
} catch (IOException e) {
log.error("Error closing input stream.", e);
}
}
}
use of i2p.bote.fileencryption.EncryptedInputStream in project i2p.i2p-bote by i2p.
the class MigrateTo028 method migrateAddressBookIfNeeded.
private void migrateAddressBookIfNeeded(Configuration configuration, PasswordHolder passwordHolder) throws FileNotFoundException, IOException, GeneralSecurityException, PasswordException {
File addressBookFile = configuration.getAddressBookFile();
if (!addressBookFile.exists())
return;
BufferedReader input = null;
try {
InputStream encryptedStream = new EncryptedInputStream(new FileInputStream(addressBookFile), passwordHolder);
input = new BufferedReader(new InputStreamReader(encryptedStream));
// No PasswordException occurred, so read the input stream and call migrateAddressBook() if needed
List<String> lines = Util.readLines(encryptedStream);
if (!isAddressBookFileMigrated(lines))
migrateAddressBook(lines, configuration, passwordHolder);
} finally {
if (input != null)
input.close();
}
}
use of i2p.bote.fileencryption.EncryptedInputStream in project i2p.i2p-bote by i2p.
the class AddressBook method readContacts.
/**
* Reads an <code>AddressBook</code> from the encrypted address book file.
* Each contact is defined by one line that contains an Email Destination
* and a name, separated by a tab character.
* @throws PasswordException
*/
private void readContacts() throws PasswordException {
if (!addressFile.exists()) {
log.debug("Address file does not exist: <" + addressFile.getAbsolutePath() + ">");
contacts = new TreeSet<Contact>(new ContactComparator());
return;
}
log.debug("Reading address book from <" + addressFile.getAbsolutePath() + ">");
BufferedReader input = null;
try {
InputStream encryptedStream = new EncryptedInputStream(new FileInputStream(addressFile), passwordHolder);
input = new BufferedReader(new InputStreamReader(encryptedStream));
// No PasswordException occurred, so parse the input stream
Properties properties = new Properties();
properties.load(new InputStreamReader(encryptedStream));
loadFromProperties(properties, false, false);
} catch (PasswordException e) {
throw e;
} catch (Exception e) {
log.error("Can't read address book.", e);
} finally {
if (input != null)
try {
input.close();
} catch (IOException e) {
log.error("Error closing input stream.", e);
}
}
}
use of i2p.bote.fileencryption.EncryptedInputStream in project i2p.i2p-bote by i2p.
the class EmailFolder method createFolderElement.
@Override
protected Email createFolderElement(File emailFile) throws Exception {
InputStream emailStream = null;
try {
emailStream = new BufferedInputStream(new EncryptedInputStream(new FileInputStream(emailFile), passwordHolder));
InputStream metadataStream = null;
File metadataFile = getMetadataFile(emailFile);
if (metadataFile.exists())
metadataStream = new BufferedInputStream(new EncryptedInputStream(new FileInputStream(metadataFile), passwordHolder));
Email email = new Email(emailStream, metadataStream, passwordHolder);
String messageIdString = emailFile.getName().substring(0, 44);
email.setMessageID(messageIdString);
return email;
} finally {
if (emailStream != null)
emailStream.close();
}
}
Aggregations