List of usage examples for org.bouncycastle.crypto.modes CBCBlockCipher CBCBlockCipher
public CBCBlockCipher(BlockCipher cipher)
From source file:org.jpedal.io.DecryptionFactory.java
License:Open Source License
/** * workout key from OE or UE/*from w w w.j a va 2s . c om*/ */ private static byte[] v5Decrypt(final byte[] rawValue, final byte[] key) throws PdfSecurityException { final int ELength = rawValue.length; final byte[] returnKey = new byte[ELength]; try { //setup Cipher final BlockCipher cbc = new CBCBlockCipher(new AESFastEngine()); cbc.init(false, new KeyParameter(key)); //translate bytes int nextBlockSize; for (int i = 0; i < ELength; i += nextBlockSize) { cbc.processBlock(rawValue, i, returnKey, i); nextBlockSize = cbc.getBlockSize(); } } catch (final Exception e) { throw new PdfSecurityException("Exception " + e.getMessage() + " with v5 encoding"); } return returnKey; }
From source file:org.jpedal.io.DecryptionFactory.java
License:Open Source License
/** * decode AES ecnoded data with IV parameters * @param password/* w w w . ja v a 2 s.c om*/ * @param encKey * @param encData a data gained from deducting IV bytes in beginning (encData = data - ivBytes) * @param ivData * @return * @throws Exception */ private static byte[] decodeAES(final byte[] encKey, final byte[] encData, final byte[] ivData) throws Exception { final KeyParameter keyParam = new KeyParameter(encKey); final CipherParameters params = new ParametersWithIV(keyParam, ivData); // setup AES cipher in CBC mode with PKCS7 padding final BlockCipherPadding padding = new PKCS7Padding(); final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding); cipher.reset(); cipher.init(false, params); // create a temporary buffer to decode into (it'll include padding) final byte[] buf = new byte[cipher.getOutputSize(encData.length)]; int len = cipher.processBytes(encData, 0, encData.length, buf, 0); len += cipher.doFinal(buf, len); // remove padding final byte[] out = new byte[len]; System.arraycopy(buf, 0, out, 0, len); // return string representation of decoded bytes return out; }
From source file:org.jpedal.io.security.BouncyCastleDecryption.java
License:Open Source License
@Override public byte[] v5Decrypt(final byte[] rawValue, final byte[] key) throws PdfSecurityException { final int ELength = rawValue.length; final byte[] returnKey = new byte[ELength]; try {//from www.j ava2 s .c o m //setup Cipher final BlockCipher cbc = new CBCBlockCipher(new AESFastEngine()); cbc.init(false, new KeyParameter(key)); //translate bytes int nextBlockSize; for (int i = 0; i < ELength; i += nextBlockSize) { cbc.processBlock(rawValue, i, returnKey, i); nextBlockSize = cbc.getBlockSize(); } } catch (final Exception e) { throw new PdfSecurityException("Exception " + e.getMessage() + " with v5 encoding"); } return returnKey; }
From source file:org.jpedal.io.security.BouncyCastleDecryption.java
License:Open Source License
@Override public byte[] decodeAES(final byte[] encKey, final byte[] encData, final byte[] ivData) throws Exception { final KeyParameter keyParam = new KeyParameter(encKey); final CipherParameters params = new ParametersWithIV(keyParam, ivData); // setup AES cipher in CBC mode with PKCS7 padding final BlockCipherPadding padding = new PKCS7Padding(); final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding);/*from w ww. j a v a 2 s . c o m*/ cipher.reset(); cipher.init(false, params); // create a temporary buffer to decode into (it'll include padding) final byte[] buf = new byte[cipher.getOutputSize(encData.length)]; int len = cipher.processBytes(encData, 0, encData.length, buf, 0); len += cipher.doFinal(buf, len); // remove padding final byte[] out = new byte[len]; System.arraycopy(buf, 0, out, 0, len); // return string representation of decoded bytes return out; }
From source file:org.jruby.ext.openssl.x509store.PEMInputOutput.java
License:LGPL
private static PrivateKey derivePrivateKeyPBES2(EncryptedPrivateKeyInfo eIn, AlgorithmIdentifier algId, char[] password) throws GeneralSecurityException, InvalidCipherTextException { PBES2Parameters pbeParams = PBES2Parameters.getInstance((ASN1Sequence) algId.getParameters()); CipherParameters cipherParams = extractPBES2CipherParams(password, pbeParams); EncryptionScheme scheme = pbeParams.getEncryptionScheme(); BufferedBlockCipher cipher;/*w w w . j av a 2s. c o m*/ if (scheme.getAlgorithm().equals(PKCSObjectIdentifiers.RC2_CBC)) { RC2CBCParameter rc2Params = RC2CBCParameter.getInstance(scheme); byte[] iv = rc2Params.getIV(); CipherParameters param = new ParametersWithIV(cipherParams, iv); cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new RC2Engine())); cipher.init(false, param); } else { byte[] iv = ((ASN1OctetString) scheme.getObject()).getOctets(); // this version, done for BC 1.49 compat, caused #1238. // byte[] iv = ASN1OctetString.getInstance(scheme).getOctets(); CipherParameters param = new ParametersWithIV(cipherParams, iv); cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine())); cipher.init(false, param); } byte[] data = eIn.getEncryptedData(); byte[] out = new byte[cipher.getOutputSize(data.length)]; int len = cipher.processBytes(data, 0, data.length, out, 0); len += cipher.doFinal(out, len); byte[] pkcs8 = new byte[len]; System.arraycopy(out, 0, pkcs8, 0, len); KeyFactory fact = KeyFactory.getInstance("RSA"); // It seems to work for both RSA and DSA. return fact.generatePrivate(new PKCS8EncodedKeySpec(pkcs8)); }
From source file:org.mkdev.ut.Encryptor.java
License:GNU General Public License
public Encryptor(byte[] key) { /*//www . jav a 2 s . co m cipher = new PaddedBlockCipher( new CBCBlockCipher( new DESEngine() ) ); */ cipher = new PaddedBlockCipher(new CBCBlockCipher(new BlowfishEngine())); this.key = new KeyParameter(key); }
From source file:org.opcfoundation.ua.transport.security.BcCryptoProvider.java
License:Open Source License
@Override public int decryptSymm(SecurityToken token, byte[] dataToDecrypt, int inputOffset, int inputLength, byte[] output, int outputOffset) throws ServiceResultException { BufferedBlockCipher cipher = new BufferedBlockCipher(new CBCBlockCipher(new AESEngine())); cipher.init(false, new ParametersWithIV(new KeyParameter(token.getRemoteEncryptingKey()), token.getRemoteInitializationVector())); int decryptedBytes = cipher.processBytes(dataToDecrypt, inputOffset, inputLength, output, outputOffset); try {/* ww w . j av a2s .c o m*/ decryptedBytes += cipher.doFinal(output, outputOffset + decryptedBytes); return decryptedBytes; } catch (DataLengthException e) { logger.error("Input data is not an even number of encryption blocks."); throw new ServiceResultException(StatusCodes.Bad_InternalError, "Error in symmetric decrypt: Input data is not an even number of encryption blocks."); } catch (CryptoException e) { throw new ServiceResultException(StatusCodes.Bad_InternalError, e); } }
From source file:org.opcfoundation.ua.transport.security.BcCryptoProvider.java
License:Open Source License
@Override public int encryptSymm(SecurityToken token, byte[] dataToEncrypt, int inputOffset, int inputLength, byte[] output, int outputOffset) throws ServiceResultException { BufferedBlockCipher cipher = new BufferedBlockCipher(new CBCBlockCipher(new RijndaelEngine())); cipher.init(true, new ParametersWithIV(new KeyParameter(token.getLocalEncryptingKey()), token.getLocalInitializationVector())); int encryptedBytes = cipher.processBytes(dataToEncrypt, inputOffset, inputLength, output, outputOffset); try {//from ww w. j a v a 2s. com encryptedBytes += cipher.doFinal(output, outputOffset + encryptedBytes); return encryptedBytes; } catch (DataLengthException e) { logger.error("Input data is not an even number of encryption blocks."); throw new ServiceResultException(StatusCodes.Bad_InternalError, "Error in symmetric decrypt: Input data is not an even number of encryption blocks."); } catch (CryptoException e) { throw new ServiceResultException(StatusCodes.Bad_InternalError, e); } }
From source file:org.opcfoundation.ua.transport.tcp.impl.ChunkSymmDecryptVerifier.java
License:Open Source License
private int symmetricDecrypt(SecurityToken token, byte[] dataToDecrypt, int inputOffset, int inputLength, byte[] output, int outputOffset) throws ServiceResultException { //Make new RijndaelEngine RijndaelEngine engine = new RijndaelEngine(128); //Make CBC blockcipher BufferedBlockCipher cipher = new BufferedBlockCipher(new CBCBlockCipher(engine)); //find right decryption key and right initialization vector KeyParameter secret = new KeyParameter(token.getRemoteEncryptingKey()); byte[] iv = token.getRemoteInitializationVector(); //initialize cipher for decryption purposes cipher.init(false, new ParametersWithIV(secret, iv)); //decrypt/* w w w .j a v a 2s.c o m*/ int cryptedBytes = cipher.processBytes(dataToDecrypt, inputOffset, inputLength, output, outputOffset); try { cryptedBytes += cipher.doFinal(output, outputOffset + cryptedBytes); return cryptedBytes; //TODO REMOVE print traces } catch (DataLengthException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (InvalidCipherTextException e) { // TODO Auto-generated catch block e.printStackTrace(); } throw new ServiceResultException(StatusCodes.Bad_InternalError, "Error in symmetric decrypt"); }
From source file:org.opcfoundation.ua.transport.tcp.impl.ChunkSymmEncryptSigner.java
License:Open Source License
private int symmetricEncrypt(SecurityToken token, byte[] dataToEncrypt, int inputOffset, int inputLength, byte[] output, int outputOffset) throws ServiceResultException { //Make RijndaelEngine for encryption RijndaelEngine engine = new RijndaelEngine(token.getEncryptionBlockSize() * 8); //check right instance for cipher try {//from w w w.j a v a2 s .co m //TODO should we check that mode is CBC? //blockCipher CBC BufferedBlockCipher cipher = new BufferedBlockCipher(new CBCBlockCipher(engine)); cipher.init(true, new ParametersWithIV(new KeyParameter(token.getLocalEncryptingKey()), token.getLocalInitializationVector())); //Check that input data is even with the encryption blocks if (dataToEncrypt.length % cipher.getBlockSize() != 0) { //ERROR LOGGER.error("Input data is not an even number of encryption blocks."); //throw new ServiceResultException(StatusCodes.Bad_InternalError,"Error in symmetric decrypt: Input data is not an even number of encryption blocks."); } int crypted = cipher.processBytes(dataToEncrypt, inputOffset, inputLength, output, outputOffset); //log.error("ChunkSymmEncrypter/encrypt: Processed bytes: "+crypted); crypted += cipher.doFinal(output, outputOffset + crypted); return crypted; } //TODO remoce print traces catch (DataLengthException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (InvalidCipherTextException e) { e.printStackTrace(); } LOGGER.error("EXCEPTION from symmetric exception!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); throw new ServiceResultException(StatusCodes.Bad_InternalError, "Error in symmetric encrypt"); }