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; } }
@Override public int getOutputSize(final int len) { return cipherDelegate.getOutputSize(len); }
protected int engineGetOutputSize( int inputLen) { return cipher.getOutputSize(inputLen); }
public int getOutputSize(int len) { return cipher.getOutputSize(len); }
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[] 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[] 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; } }
/** * <p>decrypt.</p> * * @param toDecrypt a byte. * @return an array of byte. */ public byte[] decrypt(byte... toDecrypt) { synchronized (decryptCipher) { byte[] out = new byte[decryptCipher.getOutputSize(toDecrypt.length)]; int len = decryptCipher.processBytes(toDecrypt, 0, toDecrypt.length, out, 0); try { decryptCipher.doFinal(out, len); return out; } catch (InvalidCipherTextException e) { throw new IllegalArgumentException(e); } } }
/** * Encrypts the provided byte array. * @param input * Byte array to be encrypted * @return The encrypted version of the input byte array (in base 64). */ public byte[] encrypt(final byte[] input) { if (input == null || input.length == 0) { return input; } try { BufferedBlockCipher cipher = getCipher(true); byte[] output = new byte[cipher.getOutputSize(input.length)]; int length = cipher.processBytes(input, 0, input.length, output, 0); length += cipher.doFinal(output, length); return Base64.getEncoder().encode(Arrays.copyOfRange(output, 0, length)); } catch (Exception e) { throw new LocalizableValidationException(e, "Encryption error!", "common.ecryption.error"); } }
/** * Returns a byte array containing the encrypted version of the string. * * @param toEncrypt a {@link java.lang.String} object. * @return an array of byte. */ public byte[] encrypt(String toEncrypt) { synchronized (encryptCipher) { byte[] bytes = padMod(toEncrypt, 8); try { byte[] out = new byte[encryptCipher.getOutputSize(bytes.length)]; int len = encryptCipher.processBytes(bytes, 0, bytes.length, out, 0); encryptCipher.doFinal(out, len); return out; } catch (CryptoException e) { throw new IllegalArgumentException(e); } } }
/** * Run the given data through the initialized underlying cipher and return the result. * * @param input Input data. * * @return Result of cipher acting on input. */ private byte[] process(final byte[] input) { final byte[] output = new byte[cipher.getOutputSize(input.length)]; int processed = cipher.processBytes(input, 0, input.length, output, 0); try { processed += cipher.doFinal(output, processed); } catch (InvalidCipherTextException e) { throw new CryptoException("Cipher error", e); } return Arrays.copyOfRange(output, 0, processed); } }
/** * Decrypts the provided byte array. * @param input * Byte array (in base 64) to be decrypted * @return The decrypted version of the input byte array. */ public byte[] decrypt(final byte[] input) { if (input == null || input.length == 0) { return input; } try { BufferedBlockCipher cipher = getCipher(false); byte[] bytes = Base64.getDecoder().decode(input); byte[] output = new byte[cipher.getOutputSize(bytes.length)]; int length = cipher.processBytes(bytes, 0, bytes.length, output, 0); length += cipher.doFinal(output, length); return Arrays.copyOfRange(output, 0, length); } catch (Exception e) { throw new LocalizableValidationException(e, "Decryption error!", "common.dercyption.error"); } }
@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); } }
@Override public byte[] crypt(boolean encrypt, byte[] data, byte[] key_e, byte[] initializationVector) { 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; }