Search in sources :

Example 1 with PwDbHeader

use of com.keepassdroid.database.PwDbHeader in project KeePassDX by Kunzisoft.

the class PwManagerOutputTest method testFinalKey.

public void testFinalKey() throws PwDbOutputException {
    ByteArrayOutputStream bActual = new ByteArrayOutputStream();
    PwDbV3Output pActual = new PwDbV3OutputDebug(mPM, bActual, true);
    PwDbHeader hActual = pActual.outputHeader(bActual);
    byte[] finalKey = pActual.getFinalKey(hActual);
    assertArrayEquals("Keys mismatched", mPM.finalKey, finalKey);
}
Also used : PwDbV3Output(com.keepassdroid.database.save.PwDbV3Output) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PwDbV3OutputDebug(com.keepassdroid.database.save.PwDbV3OutputDebug) PwDbHeader(com.keepassdroid.database.PwDbHeader)

Example 2 with PwDbHeader

use of com.keepassdroid.database.PwDbHeader in project KeePassDX by Kunzisoft.

the class PwDbV3Output method output.

@Override
public void output() throws PwDbOutputException {
    prepForOutput();
    PwDbHeader header = outputHeader(mOS);
    byte[] finalKey = getFinalKey(header);
    Cipher cipher;
    try {
        if (mPM.algorithm == PwEncryptionAlgorithm.Rjindal) {
            cipher = CipherFactory.getInstance("AES/CBC/PKCS5Padding");
        } else if (mPM.algorithm == PwEncryptionAlgorithm.Twofish) {
            cipher = CipherFactory.getInstance("Twofish/CBC/PKCS7PADDING");
        } else {
            throw new Exception();
        }
    } catch (Exception e) {
        throw new PwDbOutputException("Algorithm not supported.");
    }
    try {
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(finalKey, "AES"), new IvParameterSpec(header.encryptionIV));
        CipherOutputStream cos = new CipherOutputStream(mOS, cipher);
        BufferedOutputStream bos = new BufferedOutputStream(cos);
        outputPlanGroupAndEntries(bos);
        bos.flush();
        bos.close();
    } catch (InvalidKeyException e) {
        throw new PwDbOutputException("Invalid key");
    } catch (InvalidAlgorithmParameterException e) {
        throw new PwDbOutputException("Invalid algorithm parameter.");
    } catch (IOException e) {
        throw new PwDbOutputException("Failed to output final encrypted part.");
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) PwDbHeader(com.keepassdroid.database.PwDbHeader) BufferedOutputStream(java.io.BufferedOutputStream) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 3 with PwDbHeader

use of com.keepassdroid.database.PwDbHeader in project keepass2android by PhilippC.

the class PwDbV3Output method output.

@Override
public void output() throws PwDbOutputException {
    prepForOutput();
    PwDbHeader header = outputHeader(mOS);
    byte[] finalKey = getFinalKey(header);
    Cipher cipher;
    try {
        if (mPM.algorithm == PwEncryptionAlgorithm.Rjindal) {
            cipher = CipherFactory.getInstance("AES/CBC/PKCS5Padding");
        } else if (mPM.algorithm == PwEncryptionAlgorithm.Twofish) {
            cipher = CipherFactory.getInstance("TWOFISH/CBC/PKCS7PADDING");
        } else {
            throw new Exception();
        }
    } catch (Exception e) {
        throw new PwDbOutputException("Algorithm not supported.");
    }
    try {
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(finalKey, "AES"), new IvParameterSpec(header.encryptionIV));
        CipherOutputStream cos = new CipherOutputStream(mOS, cipher);
        BufferedOutputStream bos = new BufferedOutputStream(cos);
        outputPlanGroupAndEntries(bos);
        bos.flush();
        bos.close();
    } catch (InvalidKeyException e) {
        throw new PwDbOutputException("Invalid key");
    } catch (InvalidAlgorithmParameterException e) {
        throw new PwDbOutputException("Invalid algorithm parameter.");
    } catch (IOException e) {
        throw new PwDbOutputException("Failed to output final encrypted part.");
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) PwDbHeader(com.keepassdroid.database.PwDbHeader) BufferedOutputStream(java.io.BufferedOutputStream) PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) IOException(java.io.IOException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

PwDbHeader (com.keepassdroid.database.PwDbHeader)3 PwDbOutputException (com.keepassdroid.database.exception.PwDbOutputException)2 BufferedOutputStream (java.io.BufferedOutputStream)2 IOException (java.io.IOException)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Cipher (javax.crypto.Cipher)2 CipherOutputStream (javax.crypto.CipherOutputStream)2 IvParameterSpec (javax.crypto.spec.IvParameterSpec)2 SecretKeySpec (javax.crypto.spec.SecretKeySpec)2 PwDbV3Output (com.keepassdroid.database.save.PwDbV3Output)1 PwDbV3OutputDebug (com.keepassdroid.database.save.PwDbV3OutputDebug)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1