Search in sources :

Example 1 with FailToSyncCipherDataException

use of net.viperfish.journal.framework.errors.FailToSyncCipherDataException in project vsDiaryWriter by shilongdai.

the class StreamCipherTransformer method setPassword.

@Override
public void setPassword(String pass) throws FailToSyncCipherDataException {
    if (salts == null) {
        byte[] macSalt = new byte[8];
        // left blank intentionally
        byte[] encSalt = new byte[8];
        rand.nextBytes(macSalt);
        salts = new ByteParameterPair(encSalt, macSalt);
        try {
            Files.write(additSalt.toPath(), salts.toString().getBytes(StandardCharsets.US_ASCII), StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        } catch (IOException e) {
            FailToSyncCipherDataException fc = new FailToSyncCipherDataException("Cannot write salt to " + additSalt.getAbsolutePath() + " message:" + e.getMessage());
            fc.initCause(e);
            throw fc;
        }
    }
    masterKey = this.generateKey(pass);
    hkdf.init(new HKDFParameters(masterKey, salts.getSecond(), "Mac Key".getBytes()));
    byte[] macKey = new byte[this.getMacKeySize() / 8];
    hkdf.generateBytes(macKey, 0, macKey.length);
    this.initMac(macKey);
}
Also used : ByteParameterPair(net.viperfish.framework.ByteParameterPair) FailToSyncCipherDataException(net.viperfish.journal.framework.errors.FailToSyncCipherDataException) HKDFParameters(org.bouncycastle.crypto.params.HKDFParameters) IOException(java.io.IOException)

Example 2 with FailToSyncCipherDataException

use of net.viperfish.journal.framework.errors.FailToSyncCipherDataException in project vsDiaryWriter by shilongdai.

the class StreamCipherTransformer method loadSalt.

void loadSalt() {
    if (additSalt.exists()) {
        try {
            byte[] raw = Files.readAllBytes(additSalt.toPath());
            String encoded = new String(raw, StandardCharsets.US_ASCII);
            if (encoded.length() != 0)
                this.salts = ByteParameterPair.valueOf(encoded);
        } catch (IOException e) {
            FailToSyncCipherDataException fc = new FailToSyncCipherDataException("Cannot load salt from:" + additSalt.getAbsolutePath() + " message:" + e.getMessage());
            fc.initCause(e);
            throw new RuntimeException(fc);
        }
    }
}
Also used : FailToSyncCipherDataException(net.viperfish.journal.framework.errors.FailToSyncCipherDataException) IOException(java.io.IOException)

Example 3 with FailToSyncCipherDataException

use of net.viperfish.journal.framework.errors.FailToSyncCipherDataException in project vsDiaryWriter by shilongdai.

the class BlockCipherMacTransformer method setPassword.

/**
 * derive the key from the password
 *
 * @throws FailToSyncCipherDataException
 */
@Override
public void setPassword(String string) throws FailToSyncCipherDataException {
    try {
        String saltRaw = salts.read(StandardCharsets.US_ASCII);
        if (saltRaw.length() > 0) {
            saltPair = ByteParameterPair.valueOf(saltRaw);
        } else {
            byte[] encSalt = new byte[8];
            byte[] macSalt = new byte[8];
            rand.nextBytes(encSalt);
            rand.nextBytes(macSalt);
            saltPair = new ByteParameterPair(encSalt, macSalt);
            salts.write(saltPair.toString(), StandardCharsets.US_ASCII);
        }
    } catch (IOException e) {
        FailToSyncCipherDataException fc = new FailToSyncCipherDataException("Cannot load encryption and mac salt from file");
        fc.initCause(e);
        throw fc;
    }
    this.key = generateKey(string);
    hkdf.init(new HKDFParameters(key, saltPair.getFirst(), "Encryption Key".getBytes()));
    cryptKey = new byte[enc.getKeySize() / 8];
    hkdf.generateBytes(cryptKey, 0, cryptKey.length);
    byte[] macKey = new byte[getMacKeySize() / 8];
    hkdf.init(new HKDFParameters(key, saltPair.getSecond(), "Mac Key".getBytes()));
    hkdf.generateBytes(macKey, 0, macKey.length);
    initMac(macKey);
}
Also used : ByteParameterPair(net.viperfish.framework.ByteParameterPair) FailToSyncCipherDataException(net.viperfish.journal.framework.errors.FailToSyncCipherDataException) HKDFParameters(org.bouncycastle.crypto.params.HKDFParameters) IOException(java.io.IOException)

Example 4 with FailToSyncCipherDataException

use of net.viperfish.journal.framework.errors.FailToSyncCipherDataException in project vsDiaryWriter by shilongdai.

the class CompressMacTest method testWrapper.

protected void testWrapper() {
    JournalTransformer wrapper;
    try {
        CommonFunctions.initDir(testDir);
        wrapper = createTransformer(new File(testDir.getCanonicalPath() + "/salt"));
    } catch (IOException e) {
        System.err.println("cannot load salt");
        throw new RuntimeException(e);
    }
    try {
        wrapper.setPassword("password");
    } catch (FailToSyncCipherDataException e1) {
        throw new RuntimeException(e1);
    }
    Journal j = new Journal();
    j.setSubject("test get");
    j.setContent("test get content");
    try {
        Journal result = wrapper.encryptJournal(j);
        result = wrapper.decryptJournal(result);
        String plainContent = result.getContent();
        Assert.assertEquals("test get content", plainContent);
        Assert.assertEquals("test get", result.getSubject());
    } catch (CipherException | CompromisedDataException e) {
        throw new RuntimeException(e);
    }
}
Also used : FailToSyncCipherDataException(net.viperfish.journal.framework.errors.FailToSyncCipherDataException) JournalTransformer(net.viperfish.journal.framework.JournalTransformer) CompromisedDataException(net.viperfish.journal.framework.errors.CompromisedDataException) CipherException(net.viperfish.journal.framework.errors.CipherException) Journal(net.viperfish.journal.framework.Journal) IOException(java.io.IOException) File(java.io.File)

Aggregations

IOException (java.io.IOException)4 FailToSyncCipherDataException (net.viperfish.journal.framework.errors.FailToSyncCipherDataException)4 ByteParameterPair (net.viperfish.framework.ByteParameterPair)2 HKDFParameters (org.bouncycastle.crypto.params.HKDFParameters)2 File (java.io.File)1 Journal (net.viperfish.journal.framework.Journal)1 JournalTransformer (net.viperfish.journal.framework.JournalTransformer)1 CipherException (net.viperfish.journal.framework.errors.CipherException)1 CompromisedDataException (net.viperfish.journal.framework.errors.CompromisedDataException)1