use of com.keepassdroid.database.exception.PwDbOutputException in project keepass2android by PhilippC.
the class PwDbOutput method setIVs.
protected SecureRandom setIVs(PwDbHeader header) throws PwDbOutputException {
SecureRandom random;
try {
random = SecureRandom.getInstance("SHA1PRNG");
} catch (NoSuchAlgorithmException e) {
throw new PwDbOutputException("Does not support secure random number generation.");
}
random.nextBytes(header.encryptionIV);
random.nextBytes(header.masterSeed);
random.nextBytes(header.transformSeed);
return random;
}
use of com.keepassdroid.database.exception.PwDbOutputException 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.");
}
}
use of com.keepassdroid.database.exception.PwDbOutputException in project keepass2android by PhilippC.
the class PwDbV3Output method outputHeader.
public PwDbHeader outputHeader(OutputStream os) throws PwDbOutputException {
// Build header
PwDbHeaderV3 header = new PwDbHeaderV3();
header.signature1 = PwDbHeader.PWM_DBSIG_1;
header.signature2 = PwDbHeaderV3.DBSIG_2;
header.flags = PwDbHeaderV3.FLAG_SHA2;
if (mPM.getEncAlgorithm() == PwEncryptionAlgorithm.Rjindal) {
header.flags |= PwDbHeaderV3.FLAG_RIJNDAEL;
} else if (mPM.getEncAlgorithm() == PwEncryptionAlgorithm.Twofish) {
header.flags |= PwDbHeaderV3.FLAG_TWOFISH;
} else {
throw new PwDbOutputException("Unsupported algorithm.");
}
header.version = PwDbHeaderV3.DBVER_DW;
header.numGroups = mPM.getGroups().size();
header.numEntries = mPM.entries.size();
header.numKeyEncRounds = mPM.getNumKeyEncRecords();
setIVs(header);
// Write checksum Checksum
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new PwDbOutputException("SHA-256 not implemented here.");
}
NullOutputStream nos;
nos = new NullOutputStream();
DigestOutputStream dos = new DigestOutputStream(nos, md);
BufferedOutputStream bos = new BufferedOutputStream(dos);
try {
outputPlanGroupAndEntries(bos);
bos.flush();
bos.close();
} catch (IOException e) {
throw new PwDbOutputException("Failed to generate checksum.");
}
header.contentsHash = md.digest();
// Output header
PwDbHeaderOutputV3 pho = new PwDbHeaderOutputV3(header, os);
try {
pho.output();
} catch (IOException e) {
throw new PwDbOutputException("Failed to output the header.");
}
return header;
}
use of com.keepassdroid.database.exception.PwDbOutputException in project keepass2android by PhilippC.
the class PwDbV3Output method outputPlanGroupAndEntries.
public void outputPlanGroupAndEntries(OutputStream os) throws PwDbOutputException {
// long size = 0;
// Groups
List<PwGroupV3> groups = mPM.getGroups();
for (int i = 0; i < groups.size(); i++) {
PwGroupV3 pg = (PwGroupV3) groups.get(i);
PwGroupOutputV3 pgo = new PwGroupOutputV3(pg, os);
try {
pgo.output();
} catch (IOException e) {
throw new PwDbOutputException("Failed to output a group: " + e.getMessage());
}
}
// Entries
for (int i = 0; i < mPM.entries.size(); i++) {
PwEntryV3 pe = (PwEntryV3) mPM.entries.get(i);
PwEntryOutputV3 peo = new PwEntryOutputV3(pe, os);
try {
peo.output();
} catch (IOException e) {
throw new PwDbOutputException("Failed to output an entry.");
}
}
}
Aggregations