use of com.keepassdroid.database.exception.PwDbOutputException in project KeePassDX by Kunzisoft.
the class PwDbV4Output method attachStreamEncryptor.
private CipherOutputStream attachStreamEncryptor(PwDbHeaderV4 header, OutputStream os) throws PwDbOutputException {
Cipher cipher;
try {
// mPM.makeFinalKey(header.masterSeed, mPM.kdfParameters);
cipher = engine.getCipher(Cipher.ENCRYPT_MODE, mPM.finalKey, header.encryptionIV);
} catch (Exception e) {
throw new PwDbOutputException("Invalid algorithm.", e);
}
CipherOutputStream cos = new CipherOutputStream(os, cipher);
return cos;
}
use of com.keepassdroid.database.exception.PwDbOutputException in project KeePassDX by Kunzisoft.
the class PwDbV4Output method outputHeader.
@Override
public PwDbHeader outputHeader(OutputStream os) throws PwDbOutputException {
PwDbHeaderV4 header = new PwDbHeaderV4(mPM);
setIVs(header);
PwDbHeaderOutputV4 pho = new PwDbHeaderOutputV4(mPM, header, os);
try {
pho.output();
} catch (IOException e) {
throw new PwDbOutputException("Failed to output the header.", e);
}
hashOfHeader = pho.getHashOfHeader();
headerHmac = pho.headerHmac;
return header;
}
use of com.keepassdroid.database.exception.PwDbOutputException in project KeePassDX by Kunzisoft.
the class PwDbV4Output method setIVs.
@Override
protected SecureRandom setIVs(PwDbHeader header) throws PwDbOutputException {
SecureRandom random = super.setIVs(header);
PwDbHeaderV4 h = (PwDbHeaderV4) header;
random.nextBytes(h.masterSeed);
int ivLength = engine.ivLength();
if (ivLength != h.encryptionIV.length) {
h.encryptionIV = new byte[ivLength];
}
random.nextBytes(h.encryptionIV);
UUID kdfUUID = mPM.kdfParameters.kdfUUID;
KdfEngine kdf = KdfFactory.get(kdfUUID);
kdf.randomize(mPM.kdfParameters);
if (h.version < PwDbHeaderV4.FILE_VERSION_32_4) {
h.innerRandomStream = CrsAlgorithm.Salsa20;
h.innerRandomStreamKey = new byte[32];
} else {
h.innerRandomStream = CrsAlgorithm.ChaCha20;
h.innerRandomStreamKey = new byte[64];
}
random.nextBytes(h.innerRandomStreamKey);
randomStream = PwStreamCipherFactory.getInstance(h.innerRandomStream, h.innerRandomStreamKey);
if (randomStream == null) {
throw new PwDbOutputException("Invalid random cipher");
}
if (h.version < PwDbHeaderV4.FILE_VERSION_32_4) {
random.nextBytes(h.streamStartBytes);
}
return random;
}
use of com.keepassdroid.database.exception.PwDbOutputException 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.");
}
}
use of com.keepassdroid.database.exception.PwDbOutputException in project KeePassDX by Kunzisoft.
the class PwDbV3Output method getFinalKey.
public byte[] getFinalKey(PwDbHeader header) throws PwDbOutputException {
try {
PwDbHeaderV3 h3 = (PwDbHeaderV3) header;
mPM.makeFinalKey(h3.masterSeed, h3.transformSeed, mPM.numKeyEncRounds);
return mPM.finalKey;
} catch (IOException e) {
throw new PwDbOutputException("Key creation failed: " + e.getMessage());
}
}
Aggregations