@Override public InputStream createDecryptionStream(InputStream in) { try { return new CryptoInputStream(cipherMode, properties, in, key, new IvParameterSpec(iv)); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Overrides the {@link java.io.InputStream#read()}. Reads the next byte of * data from the input stream. * * @return the next byte of data, or <code>-1</code> if the end of the * stream is reached. * @throws IOException if an I/O error occurs. */ @Override public int read() throws IOException { int n; while ((n = read(oneByteBuf, 0, 1)) == 0) { //NOPMD /* no op */ } return (n == -1) ? -1 : oneByteBuf[0] & 0xff; }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { try { cis.close(); } finally { super.channelInactive(ctx); } } }
checkStream(); int remaining = outBuffer.remaining(); if (remaining <= 0) { int nd = decryptMore(); if (nd < 0) { return -1;
/** * Constructs a {@link CryptoInputStream}. * * @param input the input data. * @param cipher the cipher instance. * @param bufferSize the bufferSize. * @param key crypto key for the cipher. * @param params the algorithm parameters. * @throws IOException if an I/O error occurs. */ protected CryptoInputStream(Input input, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) throws IOException { this.input = input; this.cipher = cipher; this.bufferSize = CryptoInputStream.checkBufferSize(cipher, bufferSize); this.key = key; this.params = params; if (!(params instanceof IvParameterSpec)) { // other AlgorithmParameterSpec such as GCMParameterSpec is not // supported now. throw new IOException("Illegal parameters"); } inBuffer = ByteBuffer.allocateDirect(this.bufferSize); outBuffer = ByteBuffer.allocateDirect(this.bufferSize + cipher.getBlockSize()); outBuffer.limit(0); initCipher(); }
/** * Constructs a {@link CtrCryptoInputStream}. * * @param input the input data. * @param cipher the CryptoCipher instance. * @param bufferSize the bufferSize. * @param key crypto key for the cipher. * @param iv Initialization vector for the cipher. * @param streamOffset the start offset in the stream. * @throws IOException if an I/O error occurs. */ protected CtrCryptoInputStream(Input input, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) throws IOException { super(input, cipher, bufferSize, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv)); this.initIV = iv.clone(); this.iv = iv.clone(); CryptoInputStream.checkStreamCipher(cipher); resetStreamOffset(streamOffset); }
/** * Overrides the {@link InputStream#available()}. Returns an estimate of the * number of bytes that can be read (or skipped over) from this input stream * without blocking by the next invocation of a method for this input * stream. * * @return an estimate of the number of bytes that can be read (or skipped * over) from this input stream without blocking or {@code 0} when * it reaches the end of the input stream. * @throws IOException if an I/O error occurs. */ @Override public int available() throws IOException { checkStream(); return input.available() + outBuffer.remaining(); }
/** * Constructs a {@link CryptoOutputStream}. * * @param output the output stream. * @param cipher the CryptoCipher instance. * @param bufferSize the bufferSize. * @param key crypto key for the cipher. * @param params the algorithm parameters. * @throws IOException if an I/O error occurs. */ protected CryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) throws IOException { this.output = output; this.bufferSize = CryptoInputStream.checkBufferSize(cipher, bufferSize); this.cipher = cipher; this.key = key; this.params = params; if (!(params instanceof IvParameterSpec)) { // other AlgorithmParameterSpec such as GCMParameterSpec is not // supported now. throw new IOException("Illegal parameters"); } inBuffer = ByteBuffer.allocateDirect(this.bufferSize); outBuffer = ByteBuffer.allocateDirect(this.bufferSize + cipher.getBlockSize()); initCipher(); }
@Override public void channelRead(ChannelHandlerContext ctx, Object data) throws Exception { byteChannel.feedData((ByteBuf) data); byte[] decryptedData = new byte[byteChannel.readableBytes()]; int offset = 0; while (offset < decryptedData.length) { offset += cis.read(decryptedData, offset, decryptedData.length - offset); } ctx.fireChannelRead(Unpooled.wrappedBuffer(decryptedData, 0, decryptedData.length)); }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { try { cis.close(); } finally { super.channelInactive(ctx); } } }
public long skip(long n) throws IOException { Utils.checkArgument(n >= 0, "Negative skip length."); checkStream(); outBuffer.clear(); nd = decryptMore(); if (nd < 0) { break;
/** * Constructs a {@link CtrCryptoOutputStream}. * * @param output the output stream. * @param cipher the CryptoCipher instance. * @param bufferSize the bufferSize. * @param key crypto key for the cipher. * @param iv Initialization vector for the cipher. * @param streamOffset the start offset in the data. * @throws IOException if an I/O error occurs. */ protected CtrCryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) throws IOException { super(output, cipher, bufferSize, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv)); CryptoInputStream.checkStreamCipher(cipher); this.streamOffset = streamOffset; this.initIV = iv.clone(); this.iv = iv.clone(); resetCipher(); }
@Override public InputStream createDecryptionStream(InputStream in) { try { return new CryptoInputStream(cipherMode, properties, in, key, new IvParameterSpec(iv)); } catch (IOException e) { throw new RuntimeException(e); } }
@Override public void channelRead(ChannelHandlerContext ctx, Object data) throws Exception { byteChannel.feedData((ByteBuf) data); byte[] decryptedData = new byte[byteChannel.readableBytes()]; int offset = 0; while (offset < decryptedData.length) { offset += cis.read(decryptedData, offset, decryptedData.length - offset); } ctx.fireChannelRead(Unpooled.wrappedBuffer(decryptedData, 0, decryptedData.length)); }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { try { cis.close(); } finally { super.channelInactive(ctx); } } }
checkStream(); if (b == null) { throw new NullPointerException(); int nd = decryptMore(); if (nd <= 0) { return nd;
private CryptoInputStream createInputStream(ReadableByteChannel ch) throws IOException { return new CryptoInputStream(cipher, conf, ch, key, new IvParameterSpec(inIv)); }
@Override public void channelRead(ChannelHandlerContext ctx, Object data) throws Exception { byteChannel.feedData((ByteBuf) data); byte[] decryptedData = new byte[byteChannel.readableBytes()]; int offset = 0; while (offset < decryptedData.length) { offset += cis.read(decryptedData, offset, decryptedData.length - offset); } ctx.fireChannelRead(Unpooled.wrappedBuffer(decryptedData, 0, decryptedData.length)); }
@Override public InputStream createDecryptionStream(InputStream in) { try { return new CryptoInputStream(cipherMode, properties, in, key, new IvParameterSpec(iv)); } catch (IOException e) { throw new RuntimeException(e); } }
private CryptoInputStream createInputStream(ReadableByteChannel ch) throws IOException { return new CryptoInputStream(cipher, conf, ch, key, new IvParameterSpec(inIv)); }