Search in sources :

Example 1 with StreamCipherEncryptor

use of net.viperfish.journal.streamCipher.StreamCipherEncryptor in project vsDiaryWriter by shilongdai.

the class StreamCipherTransformer method decryptData.

@Override
protected byte[] decryptData(String cData) {
    ByteParameterPair pair = ByteParameterPair.valueOf(cData);
    int keysize = StreamCipherEncryptors.INSTANCE.getKeySize(algName) / 8;
    int ivsize = StreamCipherEncryptors.INSTANCE.getIVSize(algName) / 8;
    byte[] keyCombo = new byte[keysize + ivsize];
    hkdf.init(new HKDFParameters(masterKey, pair.getFirst(), "Encryption Key".getBytes()));
    hkdf.generateBytes(keyCombo, 0, keyCombo.length);
    byte[] key = new byte[keysize];
    byte[] iv = new byte[ivsize];
    System.arraycopy(keyCombo, 0, key, 0, keysize);
    System.arraycopy(keyCombo, keysize, iv, 0, ivsize);
    StreamCipherEncryptor enc = StreamCipherEncryptors.INSTANCE.getEncryptor(algName);
    return enc.decrypt(pair.getSecond(), key, iv);
}
Also used : ByteParameterPair(net.viperfish.framework.ByteParameterPair) HKDFParameters(org.bouncycastle.crypto.params.HKDFParameters) StreamCipherEncryptor(net.viperfish.journal.streamCipher.StreamCipherEncryptor)

Example 2 with StreamCipherEncryptor

use of net.viperfish.journal.streamCipher.StreamCipherEncryptor in project vsDiaryWriter by shilongdai.

the class StreamCipherTransformer method encryptData.

@Override
protected String encryptData(byte[] bytes) {
    byte[] randomSource = new byte[16];
    rand.nextBytes(randomSource);
    int keysize = StreamCipherEncryptors.INSTANCE.getKeySize(algName) / 8;
    int ivsize = StreamCipherEncryptors.INSTANCE.getIVSize(algName) / 8;
    byte[] keyCombo = new byte[keysize + ivsize];
    hkdf.init(new HKDFParameters(masterKey, randomSource, "Encryption Key".getBytes()));
    hkdf.generateBytes(keyCombo, 0, keyCombo.length);
    byte[] key = new byte[keysize];
    byte[] iv = new byte[ivsize];
    System.arraycopy(keyCombo, 0, key, 0, keysize);
    System.arraycopy(keyCombo, keysize, iv, 0, ivsize);
    StreamCipherEncryptor enc = StreamCipherEncryptors.INSTANCE.getEncryptor(algName);
    byte[] ecrypted = enc.encrypt(bytes, key, iv);
    ByteParameterPair pair = new ByteParameterPair(randomSource, ecrypted);
    return pair.toString();
}
Also used : ByteParameterPair(net.viperfish.framework.ByteParameterPair) HKDFParameters(org.bouncycastle.crypto.params.HKDFParameters) StreamCipherEncryptor(net.viperfish.journal.streamCipher.StreamCipherEncryptor)

Aggregations

ByteParameterPair (net.viperfish.framework.ByteParameterPair)2 StreamCipherEncryptor (net.viperfish.journal.streamCipher.StreamCipherEncryptor)2 HKDFParameters (org.bouncycastle.crypto.params.HKDFParameters)2