List of usage examples for org.bouncycastle.crypto.engines ChaCha7539Engine ChaCha7539Engine
public ChaCha7539Engine()
From source file:net.java.otr4j.crypto.OtrCryptoEngine4.java
License:LGPL
/** * Encrypt a message using ChaCha20 defined by RFC7539, given the specified key. The nonce is fixed to all * zero-bytes, as we expect to use a new key for every encryption operation. * * @param mkEnc the secret key used for encryption (at least 32 bytes) * @param message the plaintext message to be encrypted (non-null) * @return Returns the encrypted content. *//*from w w w.ja va2 s. c o m*/ @Nonnull public static byte[] encrypt(final byte[] mkEnc, final byte[] message) { requireLengthAtLeast(CHACHA20_KEY_LENGTH_BYTES, mkEnc); requireNonNull(message); final byte[] key = Arrays.copyOf(mkEnc, CHACHA20_KEY_LENGTH_BYTES); assert !allZeroBytes( key) : "Expected non-zero byte array for a key. Something critical might be going wrong."; try { final ChaCha7539Engine engine = new ChaCha7539Engine(); engine.init(true, new ParametersWithIV(new KeyParameter(key, 0, key.length), IV)); final byte[] out = new byte[message.length]; if (engine.processBytes(message, 0, message.length, out, 0) != message.length) { throw new IllegalStateException("Expected to process exactly full size of the message."); } return out; } finally { clear(key); } }
From source file:net.java.otr4j.crypto.OtrCryptoEngine4.java
License:LGPL
/** * Decrypt a ciphertext using ChaCha20 defined by RFC7539, given the specified key. * * @param mkEnc the secret key used for decryption (at least 32 bytes) * @param ciphertext te ciphertext to be decrypted (non-null) * @return Returns the decrypted (plaintext) content. *//*from w w w.ja v a2 s .c o m*/ @Nonnull public static byte[] decrypt(final byte[] mkEnc, final byte[] ciphertext) { requireLengthAtLeast(CHACHA20_KEY_LENGTH_BYTES, mkEnc); requireNonNull(ciphertext); final byte[] key = Arrays.copyOf(mkEnc, CHACHA20_KEY_LENGTH_BYTES); assert !allZeroBytes( key) : "Expected non-zero byte array for a key. Something critical might be going wrong."; try { final ChaCha7539Engine engine = new ChaCha7539Engine(); engine.init(false, new ParametersWithIV(new KeyParameter(key, 0, key.length), IV)); final byte[] out = new byte[ciphertext.length]; if (engine.processBytes(ciphertext, 0, ciphertext.length, out, 0) != ciphertext.length) { throw new IllegalStateException("Expected to process exactly full size of the message."); } return out; } finally { clear(key); } }
From source file:shadowsocks.crypto.Chacha20Crypto.java
License:Apache License
@Override protected StreamCipher createCipher(byte[] iv, boolean encrypt) throws CryptoException { StreamCipher c;//w w w. ja v a 2 s.co m if (mName.equals(CIPHER_CHACHA20_IETF)) { c = new ChaCha7539Engine(); } else { c = new ChaChaEngine(); } ParametersWithIV parameterIV = new ParametersWithIV(new KeyParameter(mKey), iv, 0, mIVLength); c.init(encrypt, parameterIV); return c; }