javax.crypto.Cipher.update()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(450)

本文整理了Java中javax.crypto.Cipher.update()方法的一些代码示例,展示了Cipher.update()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cipher.update()方法的具体详情如下:
包路径:javax.crypto.Cipher
类名称:Cipher
方法名:update

Cipher.update介绍

[英]Continues a multi-part transformation (encryption or decryption). The input.remaining() bytes starting at input.position() are transformed and stored in the output buffer.

If the output.remaining() is too small to hold the transformed bytes a ShortBufferException is thrown. Use Cipher#getOutputSize to check for the size of the output buffer.
[中]继续多部分转换(加密或解密)。输入。从输入开始的剩余()字节。position()被转换并存储在输出缓冲区中。
如果是输出。剩余()太小,无法容纳转换后的字节。将引发ShortBufferException。使用Cipher#getOutputSize检查输出缓冲区的大小。

代码示例

代码示例来源:origin: google/ExoPlayer

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);
 }
}

代码示例来源:origin: aws/aws-sdk-java

return cipher.update(input, inputOffset, inputLen);

代码示例来源:origin: org.apache.poi/poi

/**
 * 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);
  }
}

代码示例来源:origin: robovm/robovm

/**
 * 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);
  }
}

代码示例来源:origin: kingthy/TVRemoteIME

/**
 * 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);
}

代码示例来源:origin: GlowstoneMC/Glowstone

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));
  }
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

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);
 }
}

代码示例来源:origin: hierynomus/sshj

@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);
  }
}

代码示例来源:origin: plutext/docx4j

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];
}

代码示例来源:origin: plutext/docx4j

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;
}

代码示例来源:origin: org.apache.poi/poi

@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);
  }
}

代码示例来源:origin: plutext/docx4j

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);
  }
}

代码示例来源:origin: plutext/docx4j

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);
  }
}

代码示例来源:origin: org.apache.poi/poi

@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);
  }
}

代码示例来源:origin: org.apache.poi/poi

@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;
}

代码示例来源:origin: org.apache.poi/poi

@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];
}

代码示例来源:origin: apache/nifi

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);
  }
}

代码示例来源:origin: google/data-transfer-project

@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);
  }
 }
}

代码示例来源:origin: org.apache.poi/poi

/**
 * 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;
}

代码示例来源:origin: plutext/docx4j

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);
  }
}

相关文章