Example usage for org.bouncycastle.crypto.encodings OAEPEncoding encodeBlock

List of usage examples for org.bouncycastle.crypto.encodings OAEPEncoding encodeBlock

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.encodings OAEPEncoding encodeBlock.

Prototype

public byte[] encodeBlock(byte[] in, int inOff, int inLen) throws InvalidCipherTextException 

Source Link

Usage

From source file:org.silvertunnel.netlib.layer.tor.util.Encryption.java

License:Open Source License

/**
 * encrypt data with asymmetric key. create asymmetricla encrypted data:<br>
 * <ul>//from   w w  w.ja v a2 s.com
 * <li>OAEP padding [42 bytes] (RSA-encrypted)
 * <li>Symmetric key [16 bytes]                   FIXME: we assume that we ALWAYS need this 
 * <li>First part of data [70 bytes]
 * <li>Second part of data [x-70 bytes] (Symmetrically encrypted)
 * <ul>
 * encrypt and store in result
 * 
 * @param pub
 * @param symmetricKey    AES key  
 * @param data
 *            to be encrypted, needs currently to be at least 70 bytes long
 * @return the first half of the key exchange, ready to be send to the other
 *         partner
 */
public static byte[] asymEncrypt(RSAPublicKey pub, byte[] symmetricKey, byte[] data) throws TorException {
    if (data == null) {
        throw new NullPointerException("can't encrypt NULL data");
    }
    if (data.length < 70) {
        throw new TorException("input array too short");
    }

    try {
        int encryptedBytes = 0;

        // initialize OAEP
        OAEPEncoding oaep = new OAEPEncoding(new RSAEngine());
        oaep.init(true, new RSAKeyParameters(false, pub.getModulus(), pub.getPublicExponent()));
        // apply RSA+OAEP
        encryptedBytes = oaep.getInputBlockSize();
        byte[] oaepInput = new byte[encryptedBytes];
        System.arraycopy(data, 0, oaepInput, 0, encryptedBytes);
        byte[] part1 = oaep.encodeBlock(oaepInput, 0, encryptedBytes);

        // initialize AES
        AESCounterMode aes = new AESCounterMode(true, symmetricKey);
        // apply AES
        byte[] aesInput = new byte[data.length - encryptedBytes];
        System.arraycopy(data, encryptedBytes, aesInput, 0, aesInput.length);
        byte part2[] = aes.processStream(aesInput);

        // replace unencrypted data
        byte[] result = new byte[part1.length + part2.length];
        System.arraycopy(part1, 0, result, 0, part1.length);
        System.arraycopy(part2, 0, result, part1.length, part2.length);

        return result;
    } catch (InvalidCipherTextException e) {
        log.severe("Node.asymEncrypt(): can't encrypt cipher text:" + e.getMessage());
        throw new TorException("InvalidCipherTextException:" + e.getMessage());
    }
}

From source file:TorJava.Node.java

License:Open Source License

/**
 * encrypt data with asymmetric key. create asymmetricla encrypted data:<br>
 * <ul>/*from w  w  w  .j  a va  2s.c  om*/
 * <li>OAEP padding [42 bytes] (RSA-encrypted)
 * <li>Symmetric key [16 bytes]                   FIXME: we assume that we ALWAYS need this 
 * <li>First part of data [70 bytes]
 * <li>Second part of data [x-70 bytes] (Symmetrically encrypted)
 * <ul>
 * encrypt and store in result
 * 
 * @param data
 *            to be encrypted, needs currently to be at least 70 bytes long
 * @return the first half of the key exchange, ready to be send to the other
 *         partner
 */
byte[] asym_encrypt(byte[] data) throws TorException {
    if (data == null)
        throw new NullPointerException("can't encrypt NULL data");
    if (data.length < 70)
        throw new TorException("input array too short");

    try {
        int encrypted_bytes = 0;

        // init OAEP
        OAEPEncoding oaep = new OAEPEncoding(new RSAEngine());
        oaep.init(true,
                new RSAKeyParameters(false, server.onionKey.getModulus(), server.onionKey.getPublicExponent()));
        // apply RSA+OAEP
        encrypted_bytes = oaep.getInputBlockSize();
        byte[] oaep_input = new byte[encrypted_bytes];
        System.arraycopy(data, 0, oaep_input, 0, encrypted_bytes);
        byte[] part1 = oaep.encodeBlock(oaep_input, 0, encrypted_bytes);

        // init AES
        AESCounterMode aes = new AESCounterMode(true, symmetric_key_for_create);
        // apply AES
        byte[] aes_input = new byte[data.length - encrypted_bytes];
        System.arraycopy(data, encrypted_bytes, aes_input, 0, aes_input.length);
        byte part2[] = aes.processStream(aes_input);

        // replace unencrypted data
        byte[] result = new byte[part1.length + part2.length];
        System.arraycopy(part1, 0, result, 0, part1.length);
        System.arraycopy(part2, 0, result, part1.length, part2.length);

        return result;
    } catch (InvalidCipherTextException e) {
        Logger.logCell(Logger.ERROR, "Node.asym_encrypt(): can't encrypt cipher text:" + e.getMessage());
        throw new TorException("InvalidCipherTextException:" + e.getMessage());
    }
}