private byte[] process(BufferedBlockCipher blockCipher, byte[] in) { byte[] buf = new byte[blockCipher.getOutputSize(in.length)]; int bytesWritten = blockCipher.processBytes(in, 0, in.length, buf, 0); try { bytesWritten += blockCipher.doFinal(buf, bytesWritten); } catch (InvalidCipherTextException e) { throw new IllegalStateException("unable to encrypt/decrypt", e); } if (bytesWritten == buf.length) { return buf; } byte[] out = new byte[bytesWritten]; System.arraycopy(buf, 0, out, 0, bytesWritten); return out; } }
private byte[] crypt(boolean encrypt, byte[] data, byte[] key_e) { BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); CipherParameters params = new ParametersWithIV(new KeyParameter(key_e), initializationVector); cipher.init(encrypt, params); byte[] buffer = new byte[cipher.getOutputSize(data.length)]; int length = cipher.processBytes(data, 0, data.length, buffer, 0); try { length += cipher.doFinal(buffer, length); } catch (InvalidCipherTextException e) { throw new IllegalArgumentException(e); } if (length < buffer.length) { return Arrays.copyOfRange(buffer, 0, length); } return buffer; }
cipher = new BufferedBlockCipher(new CBCBlockCipher(new RijndaelEngine())); cipher.init(true, new ParametersWithIV(new KeyParameter(encryptingKey), iv)); int encryptedBytes = cipher.processBytes(dataToEncrypt, inputOffset, inputLength, output, outputOffset); encryptedBytes += cipher.doFinal(output, outputOffset + encryptedBytes); return encryptedBytes;
public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException { cipher.init(forEncryption, params); }
/** * Encrypts specified byte array. Size must be aligned to 8-bytes boundary. * * @param buffer byte array to encrypt. */ public void encryptBuffer(byte... buffer) { if (buffer.length % (Long.SIZE / Byte.SIZE) != 0) { throw new IllegalArgumentException("Buffer size is not alligned to 8-bytes boundary"); } try { int len = encryptCipher.processBytes(buffer, 0, buffer.length, buffer, 0); encryptCipher.doFinal(buffer, len); } catch (InvalidCipherTextException e) { throw new IllegalArgumentException(e); } }
@Override public Cipher create() { return new BCBlockCipher(new BufferedBlockCipher(new DESEngine())) { @Override protected CipherParameters createParams(byte[] key) { return new DESedeParameters(key); } }; } });
byte[] deriveIv(ParametersWithIV ivKey, int block) { byte[] blockHash = blockHash(block); byte[] iv = new byte[0x10]; cbcAes.init(true, ivKey); cbcAes.processBytes(blockHash, 0, blockHash.length, iv, 0); return iv; }
bufLen = bufferedBlockCipher.getOutputSize(updateSize); bufLen = bufferedBlockCipher.getUpdateOutputSize(updateSize);
protected int engineUpdate( byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) { return cipher.processBytes(input, inputOffset, inputLen, output, outputOffset); }
private BufferedBlockCipher ase256CtrCipher(boolean forEncryption, byte[] key, byte[] iv) { BlockCipher engine = new AESEngine(); BufferedBlockCipher cipher = new BufferedBlockCipher(new SICBlockCipher(engine)); CipherParameters params = new ParametersWithIV(new KeyParameter(key), iv); cipher.init(forEncryption, params); return cipher; }
/** * Finishes and voids this cipher output stream. * Calling this method causes all remaining buffered bytes to get written * and padding to get added if necessary. * <p> * Note that after a call to this method only {@link #close()} may get * called on this cipher output stream * The result of calling any other method (including this one) is undefined! * * @throws IOException If out or cipher aren't properly initialized, * the stream has been closed, an I/O error occured the cipher * text is invalid, i.e. required padding information is missing. */ @OverridingMethodsMustInvokeSuper protected void finish() throws IOException { checkOpen(); int cipherLen = cipher.getOutputSize(0); byte[] cipherOut = this.cipherOut; if (cipherLen > cipherOut.length) this.cipherOut = cipherOut = new byte[cipherLen]; try { cipherLen = cipher.doFinal(cipherOut, 0); } catch (InvalidCipherTextException ex) { throw new IOException(ex); } delegate.write(cipherOut, 0, cipherLen); }
@Override public int doFinal(final byte[] out, final int outOff) throws CryptoException { try { return cipherDelegate.doFinal(out, outOff); } catch (InvalidCipherTextException e) { throw new CryptoException("Error finalizing cipher", e); } }
private BufferedBlockCipher setupBlockCipher(final byte[] sharedKey, final byte[] ivData, final boolean forEncryption) { // Setup cipher parameters with key and IV. final KeyParameter keyParam = new KeyParameter(sharedKey); final CipherParameters params = new ParametersWithIV(keyParam, ivData); // Setup AES cipher in CBC mode with PKCS7 padding. final BlockCipherPadding padding = new PKCS7Padding(); final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding); cipher.reset(); cipher.init(forEncryption, params); return cipher; }
protected byte[] engineUpdate( byte[] input, int inputOffset, int inputLen) { int length = cipher.getUpdateOutputSize(inputLen); if (length > 0) { byte[] out = new byte[length]; cipher.processBytes(input, inputOffset, inputLen, out, 0); return out; } cipher.processBytes(input, inputOffset, inputLen, null, 0); return null; }
protected int engineGetOutputSize( int inputLen) { return cipher.getOutputSize(inputLen); }
@Override protected BufferedBlockCipherAdapter newCipher(final CiphertextHeader header, final boolean mode) { final BufferedBlockCipher cipher = blockCipherSpec.newInstance(); CipherParameters params = new KeyParameter(lookupKey(header.getKeyName()).getEncoded()); final String algName = cipher.getUnderlyingCipher().getAlgorithmName(); if (algName.endsWith("CBC") || algName.endsWith("OFB") || algName.endsWith("CFB")) { params = new ParametersWithIV(params, header.getNonce()); } cipher.init(mode, params); return new BufferedBlockCipherAdapter(cipher); } }
@Override public void reset() { cipherDelegate.reset(); } }
byte[] crypt(boolean encrypt, byte[] iv, byte[] data, byte[] keyE) { try { BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); CipherParameters params = new ParametersWithIV(new KeyParameter(keyE), iv); cipher.init(encrypt, params); byte[] buffer = new byte[cipher.getOutputSize(data.length)]; int length = cipher.processBytes(data, 0, data.length, buffer, 0); length += cipher.doFinal(buffer, length); if (length < buffer.length) { return Arrays.copyOfRange(buffer, 0, length); } return buffer; } catch (InvalidCipherTextException e) { throw new RuntimeException(e); } }
cipher= new BufferedBlockCipher(new CBCBlockCipher(new AESEngine())); cipher.init(false, new ParametersWithIV(new KeyParameter(encryptingKey), iv)); int decryptedBytes = cipher.processBytes(dataToDecrypt, inputOffset, inputLength, output, outputOffset); decryptedBytes += cipher.doFinal(output, outputOffset + decryptedBytes); return decryptedBytes;