Search in sources :

Example 1 with InvalidKeyFileException

use of com.keepassdroid.database.exception.InvalidKeyFileException in project KeePassDX by Kunzisoft.

the class PwDatabase method getFileKey.

protected byte[] getFileKey(InputStream keyInputStream) throws InvalidKeyFileException, IOException {
    assert (keyInputStream != null);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Util.copyStream(keyInputStream, bos);
    byte[] keyData = bos.toByteArray();
    ByteArrayInputStream bis = new ByteArrayInputStream(keyData);
    byte[] key = loadXmlKeyFile(bis);
    if (key != null) {
        return key;
    }
    long fileSize = keyData.length;
    if (fileSize == 0) {
        throw new KeyFileEmptyException();
    } else if (fileSize == 32) {
        return keyData;
    } else if (fileSize == 64) {
        byte[] hex = new byte[64];
        try {
            return hexStringToByteArray(new String(keyData));
        } catch (IndexOutOfBoundsException e) {
        // Key is not base 64, treat it as binary data
        }
    }
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw new IOException("SHA-256 not supported");
    }
    // SHA256Digest md = new SHA256Digest();
    byte[] buffer = new byte[2048];
    int offset = 0;
    try {
        md.update(keyData);
    } catch (Exception e) {
        System.out.println(e.toString());
    }
    return md.digest();
}
Also used : KeyFileEmptyException(com.keepassdroid.database.exception.KeyFileEmptyException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest) IOException(java.io.IOException) InvalidKeyFileException(com.keepassdroid.database.exception.InvalidKeyFileException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyFileEmptyException(com.keepassdroid.database.exception.KeyFileEmptyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with InvalidKeyFileException

use of com.keepassdroid.database.exception.InvalidKeyFileException in project KeePassDX by Kunzisoft.

the class SetPassword method run.

@Override
public void run() {
    PwDatabase pm = mDb.pm;
    byte[] backupKey = new byte[pm.masterKey.length];
    System.arraycopy(pm.masterKey, 0, backupKey, 0, backupKey.length);
    // Set key
    try {
        InputStream is = UriUtil.getUriInputStream(ctx, mKeyfile);
        pm.setMasterKey(mPassword, is);
    } catch (InvalidKeyFileException e) {
        erase(backupKey);
        finish(false, e.getMessage());
        return;
    } catch (IOException e) {
        erase(backupKey);
        finish(false, e.getMessage());
        return;
    }
    // Save Database
    mFinish = new AfterSave(backupKey, mFinish);
    SaveDB save = new SaveDB(ctx, mDb, mFinish, mDontSave);
    save.run();
}
Also used : InvalidKeyFileException(com.keepassdroid.database.exception.InvalidKeyFileException) PwDatabase(com.keepassdroid.database.PwDatabase) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 3 with InvalidKeyFileException

use of com.keepassdroid.database.exception.InvalidKeyFileException in project keepass2android by PhilippC.

the class PwDatabaseV3 method getFileKey.

protected byte[] getFileKey(InputStream keyfileStream) throws InvalidKeyFileException, IOException {
    assert (keyfileStream != null);
    byte[] buff = new byte[8000];
    int bytesRead = 0;
    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    while ((bytesRead = keyfileStream.read(buff)) != -1) {
        bao.write(buff, 0, bytesRead);
    }
    byte[] keyFileData = bao.toByteArray();
    ByteArrayInputStream bin = new ByteArrayInputStream(keyFileData);
    if (keyFileData.length == 32) {
        byte[] outputKey = new byte[32];
        if (bin.read(outputKey, 0, 32) != 32) {
            throw new IOException("Error reading key.");
        }
        return outputKey;
    } else if (keyFileData.length == 64) {
        byte[] hex = new byte[64];
        bin.mark(64);
        if (bin.read(hex, 0, 64) != 64) {
            throw new IOException("Error reading key.");
        }
        try {
            return hexStringToByteArray(new String(hex));
        } catch (IndexOutOfBoundsException e) {
            // Key is not base 64, treat it as binary data
            bin.reset();
        }
    }
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw new IOException("SHA-256 not supported");
    }
    // SHA256Digest md = new SHA256Digest();
    byte[] buffer = new byte[2048];
    int offset = 0;
    try {
        while (true) {
            bytesRead = bin.read(buffer, 0, 2048);
            // End of file
            if (bytesRead == -1)
                break;
            md.update(buffer, 0, bytesRead);
            offset += bytesRead;
        }
    } catch (Exception e) {
        System.out.println(e.toString());
    }
    return md.digest();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) InvalidKeyFileException(com.keepassdroid.database.exception.InvalidKeyFileException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyFileEmptyException(com.keepassdroid.database.exception.KeyFileEmptyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

InvalidKeyFileException (com.keepassdroid.database.exception.InvalidKeyFileException)3 IOException (java.io.IOException)3 KeyFileEmptyException (com.keepassdroid.database.exception.KeyFileEmptyException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 MessageDigest (java.security.MessageDigest)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 PwDatabase (com.keepassdroid.database.PwDatabase)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1