Example usage for org.bouncycastle.crypto.engines RijndaelEngine RijndaelEngine

List of usage examples for org.bouncycastle.crypto.engines RijndaelEngine RijndaelEngine

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.engines RijndaelEngine RijndaelEngine.

Prototype

public RijndaelEngine(int blockBits) 

Source Link

Document

basic constructor - set the cipher up for a given blocksize

Usage

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 {/*from  w w  w.j  a v a 2s.c  om*/
        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:org.debux.webmotion.server.BouncyCastleTest.java

License:Open Source License

@Test
public void testEncryptRijndael()
        throws DataLengthException, IllegalStateException, InvalidCipherTextException {
    BlockCipher engine = new RijndaelEngine(256);
    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine),
            new ZeroBytePadding());

    byte[] keyBytes = "0123456789abcdef0123456789abcdef".getBytes();
    cipher.init(true, new KeyParameter(keyBytes));

    byte[] input = "value".getBytes();
    byte[] cipherText = new byte[cipher.getOutputSize(input.length)];

    int cipherLength = cipher.processBytes(input, 0, input.length, cipherText, 0);
    cipher.doFinal(cipherText, cipherLength);

    String result = new String(Base64.encode(cipherText));
    log.debug("result : " + result);
    AssertJUnit.assertNotNull(result);//from   w ww .  j  a v  a2s  .c  o m
}

From source file:org.debux.webmotion.server.BouncyCastleTest.java

License:Open Source License

@Test
public void testDecryptRijndael()
        throws DataLengthException, IllegalStateException, InvalidCipherTextException {
    BlockCipher engine = new RijndaelEngine(256);
    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine),
            new ZeroBytePadding());

    byte[] keyBytes = "0123456789abcdef0123456789abcdef".getBytes();
    cipher.init(false, new KeyParameter(keyBytes));

    byte[] output = Base64.decode("Ij7J7G33H5xE9K5vaTiEypPnjJPuDdZ0C9QyvcIj/ZI=".getBytes());
    byte[] cipherText = new byte[cipher.getOutputSize(output.length)];

    int cipherLength = cipher.processBytes(output, 0, output.length, cipherText, 0);
    int outputLength = cipher.doFinal(cipherText, cipherLength);
    outputLength += cipherLength;/*from w  w  w  . j a  v a  2s  . c om*/

    byte[] resultBytes = cipherText;
    if (outputLength != output.length) {
        resultBytes = new byte[outputLength];
        System.arraycopy(cipherText, 0, resultBytes, 0, outputLength);
    }

    String result = new String(resultBytes);
    log.debug("result : " + result);
    AssertJUnit.assertEquals("value", result);
}

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//ww w. j av 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 {/* w ww.  java2 s .  com*/
        //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");
}