Search in sources :

Example 16 with ParametersWithIV

use of org.spongycastle.crypto.params.ParametersWithIV in project KeePassDX by Kunzisoft.

the class PwStreamCipherFactory method getChaCha20.

private static StreamCipher getChaCha20(byte[] key) {
    // Build stream cipher key
    byte[] hash = CryptoUtil.hashSha512(key);
    byte[] key32 = new byte[32];
    byte[] iv = new byte[12];
    System.arraycopy(hash, 0, key32, 0, 32);
    System.arraycopy(hash, 32, iv, 0, 12);
    KeyParameter keyParam = new KeyParameter(key32);
    ParametersWithIV ivParam = new ParametersWithIV(keyParam, iv);
    StreamCipher cipher = new ChaCha7539Engine();
    cipher.init(true, ivParam);
    return cipher;
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) ChaCha7539Engine(org.spongycastle.crypto.engines.ChaCha7539Engine) KeyParameter(org.spongycastle.crypto.params.KeyParameter) StreamCipher(org.spongycastle.crypto.StreamCipher)

Example 17 with ParametersWithIV

use of org.spongycastle.crypto.params.ParametersWithIV in project rskj by rsksmart.

the class KeyCrypterAes method decrypt.

/**
 * Decrypt bytes previously encrypted with this class.
 *
 * @param dataToDecrypt    The data to decrypt
 * @param key              The AES key to use for decryption
 * @return                 The decrypted bytes
 * @throws                 KeyCrypterException if bytes could not be decrypted
 */
@Override
public byte[] decrypt(EncryptedData dataToDecrypt, KeyParameter key) {
    checkNotNull(dataToDecrypt);
    checkNotNull(key);
    try {
        ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(key.getKey()), dataToDecrypt.initialisationVector);
        // Decrypt the message.
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, keyWithIv);
        byte[] cipherBytes = dataToDecrypt.encryptedBytes;
        byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)];
        final int length1 = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0);
        final int length2 = cipher.doFinal(decryptedBytes, length1);
        return Arrays.copyOf(decryptedBytes, length1 + length2);
    } catch (Exception e) {
        throw new KeyCrypterException("Could not decrypt bytes", e);
    }
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) KeyParameter(org.spongycastle.crypto.params.KeyParameter) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine)

Aggregations

ParametersWithIV (org.spongycastle.crypto.params.ParametersWithIV)17 KeyParameter (org.spongycastle.crypto.params.KeyParameter)12 AESFastEngine (org.spongycastle.crypto.engines.AESFastEngine)11 BufferedBlockCipher (org.spongycastle.crypto.BufferedBlockCipher)8 CBCBlockCipher (org.spongycastle.crypto.modes.CBCBlockCipher)8 PaddedBufferedBlockCipher (org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher)7 SICBlockCipher (org.spongycastle.crypto.modes.SICBlockCipher)6 IOException (java.io.IOException)4 BCECPrivateKey (org.spongycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)3 ECPoint (org.spongycastle.math.ec.ECPoint)3 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 DataLengthException (org.spongycastle.crypto.DataLengthException)2 InvalidCipherTextException (org.spongycastle.crypto.InvalidCipherTextException)2 StreamCipher (org.spongycastle.crypto.StreamCipher)2 PBEParametersGenerator (org.spongycastle.crypto.PBEParametersGenerator)1 AESEngine (org.spongycastle.crypto.engines.AESEngine)1 ChaCha7539Engine (org.spongycastle.crypto.engines.ChaCha7539Engine)1 Salsa20Engine (org.spongycastle.crypto.engines.Salsa20Engine)1