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 doFinal(final byte[] out, final int outOff) throws CryptoException { try { return cipherDelegate.doFinal(out, outOff); } catch (InvalidCipherTextException e) { throw new CryptoException("Error finalizing cipher", e); } }
@Override public int doFinal(byte[] out, int outOff) throws SecurityException { try { return wrappedCipher.doFinal(out, outOff); } catch (InvalidCipherTextException e) { throw new SecurityException(e); } }
@Override public int doFinal(byte[] out, int outOff) throws SecurityException { try { return wrappedCipher.doFinal(out, outOff); } catch (InvalidCipherTextException e) { throw new SecurityException(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[] 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; } }
public int doFinal(byte[] out, int outOff) throws IllegalStateException, BadPaddingException { try { return cipher.doFinal(out, outOff); } catch (InvalidCipherTextException e) { throw new BadPaddingException(e.getMessage()); } } }
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); } } }
@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); } }