List of usage examples for org.bouncycastle.crypto.paddings PaddedBufferedBlockCipher processBytes
public int processBytes(byte[] in, int inOff, int len, byte[] out, int outOff) throws DataLengthException, IllegalStateException
From source file:at.archistar.crypto.symmetric.AESEncryptor.java
private static byte[] cipherData(PaddedBufferedBlockCipher cipher, byte[] data) throws IllegalStateException, InvalidCipherTextException { int minSize = cipher.getOutputSize(data.length); byte[] outBuf = new byte[minSize]; int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0); int length2 = cipher.doFinal(outBuf, length1); int actualLength = length1 + length2; byte[] result = new byte[actualLength]; System.arraycopy(outBuf, 0, result, 0, result.length); return result; }
From source file:com.github.flbaue.jcrypttool.v2.domain.AesEncryptionService.java
License:Apache License
@Override public String encryptString(final String string, final String password) throws EncryptionFailedException { try {// w ww. j av a2 s.co m final byte[] salt = generateSalt(); final byte[] key = generateKey(password, salt); final byte[] iv = generateIV(); final byte[] outputInitBlock = generateOutputInitBlock(salt, iv); final PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); final KeyParameter keyParam = new KeyParameter(key); final CipherParameters params = new ParametersWithIV(keyParam, iv); cipher.init(true, params); final byte in[] = string.getBytes(); final byte out[] = new byte[cipher.getOutputSize(in.length)]; final int len1 = cipher.processBytes(in, 0, in.length, out, 0); cipher.doFinal(out, len1); final byte[] result = Arrays.concatenate(outputInitBlock, out); return Base64.toBase64String(result); } catch (InvalidKeySpecException | NoSuchAlgorithmException | InvalidCipherTextException e) { throw new EncryptionFailedException(e); } }
From source file:com.github.flbaue.jcrypttool.v2.domain.AesEncryptionService.java
License:Apache License
@Override public String decryptString(final String string, final String password) throws DecryptionFailedException { try {/*from w w w . j a va 2 s .c o m*/ byte[] inputBytes = Base64.decode(string); byte[] salt = extractSalt(inputBytes); byte[] iv = extractIV(inputBytes); byte[] key = generateKey(password, salt); byte[] encryptedData = extractEncryptedData(inputBytes); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); KeyParameter keyParam = new KeyParameter(key); CipherParameters params = new ParametersWithIV(keyParam, iv); cipher.init(false, params); final byte out[] = new byte[cipher.getOutputSize(encryptedData.length)]; int length = cipher.processBytes(encryptedData, 0, encryptedData.length, out, 0); byte[] result; length += cipher.doFinal(out, length); result = new byte[length]; System.arraycopy(out, 0, result, 0, length); return new String(result); } catch (InvalidCipherTextException | InvalidKeySpecException | NoSuchAlgorithmException e) { throw new DecryptionFailedException(e); } }
From source file:com.github.horrorho.inflatabledonkey.crypto.AESCBC.java
License:Open Source License
public static byte[] decryptAESCBC(byte[] key, byte[] iv, byte[] data) { // AES CBC PKCS7 decrypt try {//from w w w .jav a 2s .c om CipherParameters cipherParameters = new ParametersWithIV(new KeyParameter(key), iv); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESFastEngine()), new PKCS7Padding()); cipher.init(false, cipherParameters); byte[] buffer = new byte[cipher.getOutputSize(data.length)]; int pos = cipher.processBytes(data, 0, data.length, buffer, 0); pos += cipher.doFinal(buffer, pos); return Arrays.copyOf(buffer, pos); } catch (DataLengthException | IllegalStateException | InvalidCipherTextException ex) { throw new IllegalArgumentException("decrypt failed", ex); } }
From source file:com.googlecode.jsendnsca.encryption.AESEncryptor.java
License:Apache License
public void encrypt(byte[] passiveCheckBytes, byte[] initVector, String password) { RijndaelEngine engine = new RijndaelEngine(_keyByteLength * 8); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CFBBlockCipher(engine, 8), new ZeroBytePadding()); try {// w w w .j av a 2s.co m byte[] sessionKey = new byte[_keyByteLength]; byte[] passwordBytes = password.getBytes("US-ASCII"); System.arraycopy(passwordBytes, 0, sessionKey, 0, Math.min(_keyByteLength, passwordBytes.length)); byte[] iv = new byte[_keyByteLength]; System.arraycopy(initVector, 0, iv, 0, Math.min(_keyByteLength, initVector.length)); cipher.init(true, new ParametersWithIV(new KeyParameter(sessionKey), iv)); byte[] cipherText = new byte[cipher.getOutputSize(passiveCheckBytes.length)]; int cipherLength = cipher.processBytes(passiveCheckBytes, 0, passiveCheckBytes.length, cipherText, 0); cipherLength = cipherLength + cipher.doFinal(cipherText, cipherLength); int bytesToCopy = Math.min(passiveCheckBytes.length, cipherLength); System.arraycopy(cipherText, 0, passiveCheckBytes, 0, bytesToCopy); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.googlecode.jsendnsca.encryption.BlowfishEncryptor.java
License:Apache License
@Override public void encrypt(final byte[] passiveCheckBytes, final byte[] initVector, final String password) { final BlowfishEngine engine = new BlowfishEngine(); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CFBBlockCipher(engine, 8), new ZeroBytePadding()); try {/*w w w.j ava 2s. c o m*/ final byte[] passwordBytes = password.getBytes("US-ASCII"); assertValidPasswordBytesLength(passwordBytes); final byte[] sessionKey = new byte[KEY_BYTES_LENGTH]; System.arraycopy(passwordBytes, 0, sessionKey, 0, Math.min(KEY_BYTES_LENGTH, passwordBytes.length)); final byte[] iv = new byte[KEY_BYTES_LENGTH]; System.arraycopy(initVector, 0, iv, 0, Math.min(KEY_BYTES_LENGTH, initVector.length)); cipher.init(true, new ParametersWithIV(new KeyParameter(sessionKey), iv)); final byte[] cipherText = new byte[cipher.getOutputSize(passiveCheckBytes.length)]; int cipherLength = cipher.processBytes(passiveCheckBytes, 0, passiveCheckBytes.length, cipherText, 0); cipherLength = cipherLength + cipher.doFinal(cipherText, cipherLength); final int bytesToCopy = Math.min(passiveCheckBytes.length, cipherLength); System.arraycopy(cipherText, 0, passiveCheckBytes, 0, bytesToCopy); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.logicoy.pdmp.pmpi.crypto.EncryptionClient.java
License:Apache License
public String getAES256Cipher(String inputText, String password, boolean decrypt) { KeyIVGenerator derivedFromPassword = new KeyIVGenerator(password); // Set up key and IV these are derived from the password, using MD5 very simple. See class details. try {/*from ww w .j ava 2 s .c o m*/ byte[] buffer; //Sting to encrypt if (!decrypt) { buffer = inputText.getBytes(Charset.forName("UTF-8")); } else { buffer = Base64.decode(inputText); } // Aes Encryption BlockCipher blockCipher = new AESEngine(); // Mode CBC blockCipher = new CBCBlockCipher(blockCipher); BlockCipherPadding padding = new PKCS7Padding(); // Get our Cipher. PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher, padding); // Initialize the cipher. cipher.init(!decrypt, new ParametersWithIV(new KeyParameter(derivedFromPassword.getKey()), derivedFromPassword.getIV())); //byte[] bytes_output = cipher.doFinal(buffer,0); byte bytes_output[] = new byte[cipher.getOutputSize(buffer.length)]; int len = cipher.processBytes(buffer, 0, buffer.length, bytes_output, 0); int noOfBytesCopied = cipher.doFinal(bytes_output, len); if (!decrypt) { //Return base 64 encrypted text. return new String(Base64.encode(bytes_output), Charset.forName("UTF-8")); //return Convert.ToBase64String(bytes_output, Base64FormattingOptions.InsertLineBreaks); } else { //Return plain text. return new String(bytes_output, Charset.forName("UTF-8")); } } catch (Exception e) { logger.severe(" Failure attempting to AES256 encrypt/decrypt the xml " + e.toString()); e.printStackTrace(); throw new RuntimeException(" Failure attempting AES256 " + (decrypt ? "decryption" : "encryption") + " :" + e.getMessage()); } finally { derivedFromPassword.Clean(); } }
From source file:com.oneops.cms.crypto.CmsCryptoDES.java
License:Apache License
/** * Encrypt.// ww w. jav a2s . co m * * @param instr the instr * @return the string * @throws java.security.GeneralSecurityException the general security exception */ @Override public String encrypt(String instr) throws GeneralSecurityException { long t1 = System.currentTimeMillis(); byte[] in = instr.getBytes(); PaddedBufferedBlockCipher encryptor = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine())); encryptor.init(true, keyParameter); byte[] cipherText = new byte[encryptor.getOutputSize(in.length)]; int outputLen = encryptor.processBytes(in, 0, in.length, cipherText, 0); ByteArrayOutputStream os = new ByteArrayOutputStream(); try { encryptor.doFinal(cipherText, outputLen); Hex.encode(cipherText, os); } catch (Exception e) { e.printStackTrace(); throw new GeneralSecurityException(e); } long t2 = System.currentTimeMillis(); logger.debug("Time taken to encrypt(millis) :" + (t2 - t1)); return ENC_PREFIX + os.toString(); }
From source file:com.oneops.cms.crypto.CmsCryptoDES.java
License:Apache License
private String decryptStr(String instr) throws GeneralSecurityException { if (StringUtils.isEmpty(instr)) { return instr; }/*from w w w .j a v a2 s . co m*/ long t1 = System.currentTimeMillis(); PaddedBufferedBlockCipher decryptor = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine())); decryptor.init(false, keyParameter); byte[] in = null; byte[] cipherText = null; try { in = Hex.decode(instr); cipherText = new byte[decryptor.getOutputSize(in.length)]; int outputLen = decryptor.processBytes(in, 0, in.length, cipherText, 0); decryptor.doFinal(cipherText, outputLen); } catch (Exception e) { throw new GeneralSecurityException(e); } long t2 = System.currentTimeMillis(); logger.debug("Time taken to decrypt(millis) : " + (t2 - t1)); return (new String(cipherText)).replaceAll("\\u0000+$", ""); }
From source file:com.thoughtworks.go.security.DESEncrypter.java
License:Apache License
private static String decrypt(byte[] key, String cipherText) throws CryptoException { try {//from w ww. j a v a 2s. com PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine())); cipher.init(false, new KeyParameter(key)); byte[] cipherTextBytes = DECODER.decode(cipherText); byte[] plainTextBytes = new byte[cipher.getOutputSize(cipherTextBytes.length)]; int outputLength = cipher.processBytes(cipherTextBytes, 0, cipherTextBytes.length, plainTextBytes, 0); cipher.doFinal(plainTextBytes, outputLength); int paddingStarts = plainTextBytes.length - 1; for (; paddingStarts >= 0; paddingStarts--) { if (plainTextBytes[paddingStarts] != 0) { break; } } return new String(plainTextBytes, 0, paddingStarts + 1); } catch (Exception e) { throw new CryptoException(e); } }