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[] 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; } }
protected int engineUpdate( byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) { return cipher.processBytes(input, inputOffset, inputLen, output, outputOffset); }
public int processBytes(byte[] in, int inOff, int len, byte[] out, int outOff) throws DataLengthException { return cipher.processBytes(in, inOff, len, out, outOff); }
@Override public int update(byte[] in, int inOff, int bytes, byte[] out, int outOff) { return wrappedCipher.processBytes(in, inOff, bytes, out, outOff); }
@Override public int update(byte[] in, int inOff, int bytes, byte[] out, int outOff) { return wrappedCipher.processBytes(in, inOff, bytes, out, outOff); }
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; }
@Override public int processBytes(final byte[] in, final int inOff, final int len, final byte[] out, final int outOff) throws CryptoException { try { return cipherDelegate.processBytes(in, inOff, len, out, outOff); } catch (RuntimeException e) { throw new CryptoException("Cipher processing error", e); } }
/** * 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); } }
/** * Decrypts specified byte array. Size must be aligned to 8-bytes boundary. * * @param buffer byte array to decrypt. */ public void decryptBuffer(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 = decryptCipher.processBytes(buffer, 0, buffer.length, buffer, 0); decryptCipher.doFinal(buffer, len); } catch (InvalidCipherTextException e) { throw new IllegalArgumentException(e); } }
private byte[] transform(final BufferedBlockCipher cipher, final byte[] data) { final byte[] buf = new byte[cipher.getOutputSize(data.length)]; int length = cipher.processBytes(data, 0, data.length, buf, 0); try { length += cipher.doFinal(buf, length); } catch (final InvalidCipherTextException e) { return null; } return Arrays.copyOf(buf, length); }
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[] transform(final BufferedBlockCipher cipher, final byte[] data) { final byte[] buf = new byte[cipher.getOutputSize(data.length)]; int length = cipher.processBytes(data, 0, data.length, buf, 0); try { length += cipher.doFinal(buf, length); } catch (final InvalidCipherTextException e) { return null; } return Arrays.copyOf(buf, length); }
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; } }
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; }
void decryptBlock(KeyParameter fileKey, byte[] iv, byte[] in, int length, byte[] out) { cbcAes.init(false, new ParametersWithIV(fileKey, iv)); cbcAes.processBytes(in, 0, length, out, 0); }
@Override public byte[] aes256CtrEncrypt(byte[] m, byte[] key, byte[] iv) { validateAes256CtrEncrypt(m, key, iv); try { BufferedBlockCipher cipher = ase256CtrCipher(true, key, iv); byte[] cipherText = new byte[cipher.getOutputSize(m.length)]; int len = cipher.processBytes(m, 0, m.length, cipherText, 0); cipher.doFinal(cipherText, len); return cipherText; } catch (InvalidCipherTextException e) { // Not possible since we're not using padding. throw new CryptoProviderException("Invalid cipher text in aes256CtrEncrypt.", e); } }
@Override public byte[] aes256CtrDecrypt(byte[] c, byte[] key, byte[] iv) { validateAes256CtrDecrypt(c, key, iv); try { BufferedBlockCipher cipher = ase256CtrCipher(false, key, iv); byte[] clearText = new byte[cipher.getOutputSize(c.length)]; int len = cipher.processBytes(c, 0, c.length, clearText, 0); cipher.doFinal(clearText, len); return clearText; } catch (InvalidCipherTextException e) { // Not possible since we're not using padding. throw new CryptoProviderException("Invalid cipher text in aes256CtrDecrypt.", e); } }
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; }
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); } }