int getOutputSize(int inputLen) { return cipher.getOutputSize(inputLen); } }
public static ByteBuffer decrypt( ByteBuffer encrypted ) { if ( encrypted == null || !encrypted.hasRemaining() ) { return encrypted; } try { SecretKeySpec sKeySpec = new SecretKeySpec( getRawKey( encryptionSeed ), "AES" ); Cipher cipher = Cipher.getInstance( "AES" ); cipher.init( Cipher.DECRYPT_MODE, sKeySpec ); ByteBuffer decrypted = ByteBuffer.allocate( cipher.getOutputSize( encrypted.remaining() ) ); cipher.doFinal( encrypted, decrypted ); decrypted.rewind(); return decrypted; } catch ( Exception e ) { throw new IllegalStateException( e ); } }
public static ByteBuffer encrypt( ByteBuffer clear ) { if ( clear == null || !clear.hasRemaining() ) { return clear; } try { SecretKeySpec sKeySpec = new SecretKeySpec( getRawKey( encryptionSeed ), "AES" ); Cipher cipher = Cipher.getInstance( "AES" ); cipher.init( Cipher.ENCRYPT_MODE, sKeySpec ); ByteBuffer encrypted = ByteBuffer.allocate( cipher.getOutputSize( clear.remaining() ) ); cipher.doFinal( clear, encrypted ); encrypted.rewind(); return encrypted; } catch ( Exception e ) { throw new IllegalStateException( e ); } }
private ByteBuf decryptData(SecretKey dataKeySecret, MessageMetadata msgMetadata, ByteBuf payload) { // unpack iv and encrypted data ByteString ivString = msgMetadata.getEncryptionParam(); ivString.copyTo(iv, 0); GCMParameterSpec gcmParams = new GCMParameterSpec(tagLen, iv); ByteBuf targetBuf = null; try { cipher.init(Cipher.DECRYPT_MODE, dataKeySecret, gcmParams); ByteBuffer sourceNioBuf = payload.nioBuffer(payload.readerIndex(), payload.readableBytes()); int maxLength = cipher.getOutputSize(payload.readableBytes()); targetBuf = PooledByteBufAllocator.DEFAULT.buffer(maxLength, maxLength); ByteBuffer targetNioBuf = targetBuf.nioBuffer(0, maxLength); int decryptedSize = cipher.doFinal(sourceNioBuf, targetNioBuf); targetBuf.writerIndex(decryptedSize); } catch (InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | ShortBufferException e) { log.error("{} Failed to decrypt message {}", logCtx, e.getMessage()); if (targetBuf != null) { targetBuf.release(); targetBuf = null; } } return targetBuf; }
int outputSize = cipher.getOutputSize(inputBuffer.length); if ((outputBuffer == null) || (outputBuffer.length < outputSize)) { this.outputBuffer = new byte[outputSize];
int maxLength = cipher.getOutputSize(payload.readableBytes()); targetBuf = PooledByteBufAllocator.DEFAULT.buffer(maxLength, maxLength); ByteBuffer targetNioBuf = targetBuf.nioBuffer(0, maxLength);
int getOutputSize(int inputLen) { return cipher.getOutputSize(inputLen); } }
@Override public void cipher(ByteBuf in, ByteBuf out) throws ShortBufferException { int readableBytes = in.readableBytes(); byte[] heapIn = bufToByte( in ); byte[] heapOut = heapOutLocal.get(); int outputSize = cipher.getOutputSize( readableBytes ); if ( heapOut.length < outputSize ) { heapOut = new byte[ outputSize ]; heapOutLocal.set( heapOut ); } out.writeBytes( heapOut, 0, cipher.update( heapIn, 0, readableBytes, heapOut ) ); }
@Override public ByteBuf cipher(ChannelHandlerContext ctx, ByteBuf in) throws ShortBufferException { int readableBytes = in.readableBytes(); byte[] heapIn = bufToByte( in ); ByteBuf heapOut = ctx.alloc().heapBuffer( cipher.getOutputSize( readableBytes ) ); heapOut.writerIndex( cipher.update( heapIn, 0, readableBytes, heapOut.array(), heapOut.arrayOffset() ) ); return heapOut; }
byte[] cipherText = new byte[cipher.getOutputSize(input.length)]; int ctLength = cipher.update(input, 0, input.length, cipherText, 0); ctLength += cipher.doFinal(cipherText, ctLength); byte[] plainText = new byte[cipher.getOutputSize(ctLength)]; int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0); ptLength += cipher.doFinal(plainText, ptLength);
/** * Returns the minimal possible size of the * Generic[Stream|Block]Cipher structure under this * connection state. */ protected int getMinFragmentSize() { // block ciphers return value with padding included return encCipher.getOutputSize(1+hash_size); // 1 byte for data }
/** * Returns the minimal upper bound of the content size enclosed * into the Generic[Stream|Block]Cipher structure of specified size. * For stream ciphers the returned value will be exact value. */ protected int getContentSize(int generic_cipher_size) { //it does not take the padding of block ciphered structures //into account (so returned value can be greater than actual) return decCipher.getOutputSize(generic_cipher_size)-hash_size; }
/** * Returns the minimal possible size of the * Generic[Stream|Block]Cipher structure under this * connection state. */ protected int getMinFragmentSize() { // block ciphers return value with padding included return encCipher.getOutputSize(1+hash_size); // 1 byte for data }
/** * Returns the size of the Generic[Stream|Block]Cipher structure * corresponding to the content data of specified size. */ protected int getFragmentSize(int content_size) { return encCipher.getOutputSize(content_size+hash_size); }
/** * Returns the size of the Generic[Stream|Block]Cipher structure * corresponding to the content data of specified size. */ protected int getFragmentSize(int content_size) { return encCipher.getOutputSize(content_size+hash_size); }
/** * Returns the minimal upper bound of the content size enclosed * into the Generic[Stream|Block]Cipher structure of specified size. * For stream ciphers the returned value will be exact value. */ protected int getContentSize(int generic_cipher_size) { //it does not take the padding of block ciphered structures //into account (so returned value can be greater than actual) return decCipher.getOutputSize(generic_cipher_size)-hash_size; }
protected byte[] decryptAes(byte[] data, byte[] key, byte[] iv) throws GeneralSecurityException { SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); IvParameterSpec ivps = new IvParameterSpec(iv, 0, 16); aesCipher.init(Cipher.DECRYPT_MODE, keySpec, ivps, appContext.random()); byte[] decryptedData = new byte[aesCipher.getOutputSize(data.length)]; int decLen = aesCipher.doFinal(data, 0, data.length, decryptedData, 0); byte[] ret = new byte[decLen]; System.arraycopy(decryptedData, 0, ret, 0, decLen); return ret; } }
public char[] getDecryptedCharArray() { try { byte[] decrypted = new byte[decrypter.getOutputSize(encLength)]; int decLen = decrypter.update(encrypted, 0, encLength, decrypted, 0); decrypter.doFinal(decrypted, decLen); decrypted = ArrayUtils.slice(decrypted, 0, actualLength - 1); return ArrayUtils.bytesToChar(decrypted); } catch (ShortBufferException | IllegalBlockSizeException | BadPaddingException ex) { throw new RuntimeException(ex); } }
@Override public ByteBuf cipher(ChannelHandlerContext ctx, ByteBuf in) throws ShortBufferException { int readableBytes = in.readableBytes(); byte[] heapIn = bufToByte( in ); ByteBuf heapOut = ctx.alloc().heapBuffer( cipher.getOutputSize( readableBytes ) ); heapOut.writerIndex( cipher.update( heapIn, 0, readableBytes, heapOut.array(), heapOut.arrayOffset() ) ); return heapOut; }
@Override public ByteBuf process(ChannelHandlerContext ctx, ByteBuf source) throws ShortBufferException { ensureNotDisposed(); int inBytes = source.readableBytes(); ByteBuf asHeapBuf = asHeapBuf(source); ByteBuf out = ctx.alloc().heapBuffer(cipher.getOutputSize(inBytes)); out.writerIndex(cipher.update(asHeapBuf.array(), asHeapBuf.arrayOffset(), inBytes, out.array(), out.arrayOffset())); return out; }