private int nonFlushingUpdate(byte[] in, int inOffset, int length, byte[] out, int outOffset) { try { return cipher.update(in, inOffset, length, out, outOffset); } catch (ShortBufferException e) { // Should never happen. throw new RuntimeException(e); } }
return cipher.update(input, inputOffset, inputLen);
/** * Helper function for overriding the cipher invocation, i.e. XOR doesn't use a cipher * and uses it's own implementation */ protected int invokeCipher(int totalBytes, boolean doFinal) throws GeneralSecurityException { if (doFinal) { return cipher.doFinal(chunk, 0, totalBytes, chunk); } else { return cipher.update(chunk, 0, totalBytes, chunk); } }
/** * Writes the {@code len} bytes from buffer {@code b} starting at offset * {@code off} to this cipher output stream. * * @param b * the buffer. * @param off * the offset to start at. * @param len * the number of bytes. * @throws IOException * if an error occurs. */ @Override public void write(byte[] b, int off, int len) throws IOException { if (len == 0) { return; } byte[] result = cipher.update(b, off, len); if (result != null) { out.write(result); } }
/** * Signs the ADB SHA1 payload with the private key of this object. * @param payload SHA1 payload to sign * @return Signed SHA1 payload * @throws GeneralSecurityException If signing fails */ public byte[] signAdbTokenPayload(byte[] payload) throws GeneralSecurityException { Cipher c = Cipher.getInstance("RSA/ECB/NoPadding"); c.init(Cipher.ENCRYPT_MODE, keyPair.getPrivate()); c.update(SIGNATURE_PADDING); return c.doFinal(payload); }
public void crypt(ByteBuf msg, List<Object> out) { ByteBuffer outBuffer = ByteBuffer.allocate(msg.readableBytes()); try { cipher.update(msg.nioBuffer(), outBuffer); } catch (ShortBufferException e) { throw new AssertionError("Encryption buffer was too short", e); } outBuffer.flip(); out.add(Unpooled.wrappedBuffer(outBuffer)); } }
private void process(ByteBuffer inBuffer, ByteBuffer outBuffer) throws IOException { try { int inputSize = inBuffer.remaining(); // Cipher#update will maintain crypto context. int n = cipher.update(inBuffer, outBuffer); if (n < inputSize) { /** * Typically code will not get here. Cipher#update will consume all * input data and put result in outBuffer. * Cipher#doFinal will reset the crypto context. */ contextReset = true; cipher.doFinal(inBuffer, outBuffer); } } catch (Exception e) { throw new IOException(e); } }
@Override public void update(byte[] input, int inputOffset, int inputLen) { try { cipher.update(input, inputOffset, inputLen, input, inputOffset); } catch (ShortBufferException e) { throw new SSHRuntimeException(e); } }
public synchronized int read() { int ch = super.read(); if (ch == -1) return -1; oneByte[0] = (byte) ch; try { cipher.update(oneByte, 0, 1, oneByte); } catch (ShortBufferException e) { throw new EncryptedDocumentException(e); } return oneByte[0]; }
public synchronized int read(byte b[], int off, int len) { int readLen = super.read(b, off, len); if (readLen ==-1) return -1; try { cipher.update(b, off, readLen, b, off); } catch (ShortBufferException e) { throw new EncryptedDocumentException(e); } return readLen; }
@Override public void write(int b) { try { oneByte[0] = (byte)b; cipher.update(oneByte, 0, 1, oneByte, 0); super.write(oneByte); } catch (Exception e) { throw new EncryptedDocumentException(e); } }
public void write(int b) { try { oneByte[0] = (byte)b; cipher.update(oneByte, 0, 1, oneByte, 0); super.write(oneByte); } catch (Exception e) { throw new EncryptedDocumentException(e); } }
public void write(byte[] b, int off, int len) { try { cipher.update(b, off, len, b, off); super.write(b, off, len); } catch (Exception e) { throw new EncryptedDocumentException(e); } }
@Override public void write(byte[] b, int off, int len) { try { cipher.update(b, off, len, b, off); super.write(b, off, len); } catch (Exception e) { throw new EncryptedDocumentException(e); } }
@Override public synchronized int read(byte b[], int off, int len) { int readLen = super.read(b, off, len); if (readLen ==-1) { return -1; } try { cipher.update(b, off, readLen, b, off); } catch (ShortBufferException e) { throw new EncryptedDocumentException(e); } return readLen; }
@Override public synchronized int read() { int ch = super.read(); if (ch == -1) { return -1; } oneByte[0] = (byte) ch; try { cipher.update(oneByte, 0, 1, oneByte); } catch (ShortBufferException e) { throw new EncryptedDocumentException(e); } return oneByte[0]; }
public static void processStreams(Cipher cipher, InputStream in, OutputStream out) { try { final byte[] buffer = new byte[BUFFER_SIZE]; int len; while ((len = in.read(buffer)) > 0) { final byte[] decryptedBytes = cipher.update(buffer, 0, len); if (decryptedBytes != null) { out.write(decryptedBytes); } } out.write(cipher.doFinal()); } catch (Exception e) { throw new ProcessException(e); } }
@Override public String encrypt(String data) { try { Cipher cipher = Cipher.getInstance(transformation); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] salt = new byte[8]; SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.nextBytes(salt); cipher.update(salt); byte[] encrypted = cipher.doFinal(data.getBytes(Charsets.UTF_8)); return BaseEncoding.base64Url().encode(encrypted); } catch (BadPaddingException | IllegalBlockSizeException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e) { monitor.severe(() -> format("Exception encrypting data, length: %s", data.length()), e); throw new RuntimeException(e); } } }
/** * Helper function for overriding the cipher invocation, i.e. XOR doesn't use a cipher * and uses it's own implementation * * @throws BadPaddingException * @throws IllegalBlockSizeException * @throws ShortBufferException */ protected int invokeCipher(int posInChunk, boolean doFinal) throws GeneralSecurityException { byte plain[] = (plainByteFlags.isEmpty()) ? null : chunk.clone(); int ciLen = (doFinal) ? cipher.doFinal(chunk, 0, posInChunk, chunk) : cipher.update(chunk, 0, posInChunk, chunk); if (plain != null) { int i = plainByteFlags.nextSetBit(0); while (i >= 0 && i < posInChunk) { chunk[i] = plain[i]; i = plainByteFlags.nextSetBit(i+1); } } return ciLen; }
public void confirmPassword(String password, byte keySpec[], byte keySalt[], byte verifier[], byte verifierSalt[], byte integritySalt[]) { BinaryRC4EncryptionVerifier ver = builder.getVerifier(); ver.setSalt(verifierSalt); SecretKey skey = BinaryRC4Decryptor.generateSecretKey(password, ver); setSecretKey(skey); try { Cipher cipher = BinaryRC4Decryptor.initCipherForBlock(null, 0, builder, skey, Cipher.ENCRYPT_MODE); byte encryptedVerifier[] = new byte[16]; cipher.update(verifier, 0, 16, encryptedVerifier); ver.setEncryptedVerifier(encryptedVerifier); org.docx4j.org.apache.poi.poifs.crypt.HashAlgorithm hashAlgo = ver .getHashAlgorithm(); MessageDigest hashAlg = CryptoFunctions.getMessageDigest(hashAlgo); byte calcVerifierHash[] = hashAlg.digest(verifier); byte encryptedVerifierHash[] = cipher.doFinal(calcVerifierHash); ver.setEncryptedVerifierHash(encryptedVerifierHash); } catch (GeneralSecurityException e) { throw new EncryptedDocumentException("Password confirmation failed", e); } }