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

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

Introduction

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

Prototype

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

Source Link

Usage

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

License:Open Source License

/**
 * decrypt data with asymmetric key. create asymmetrically encrypted data:<br>
 * <ul>// w  w w.ja v a  2s.co  m
 * <li>OAEP padding [42 bytes] (RSA-encrypted)</li>
 * <li>Symmetric key [16 bytes]</li>
 * <li>First part of data [70 bytes]</li>
 * <li>Second part of data [x-70 bytes] (Symmetrically encrypted)</li>
 * </ul>
 * encrypt and store in result
 *
 * @param priv key to use for decryption
 * @param data to be decrypted, needs currently to be at least 70 bytes long
 * @return raw data
 */
public static byte[] asymDecrypt(final RSAPrivateKey priv, final 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;

        // init OAEP
        final OAEPEncoding oaep = new OAEPEncoding(new RSAEngine());
        oaep.init(false, new RSAKeyParameters(true, priv.getModulus(), priv.getPrivateExponent()));
        // apply RSA+OAEP
        encryptedBytes = oaep.getInputBlockSize();
        final byte[] oaepInput = new byte[encryptedBytes];
        System.arraycopy(data, 0, oaepInput, 0, encryptedBytes);
        final byte[] part1 = oaep.decodeBlock(oaepInput, 0, encryptedBytes);

        // extract symmetric key
        final byte[] symmetricKey = new byte[16];
        System.arraycopy(part1, 0, symmetricKey, 0, 16);
        // init AES
        final AESCounterMode aes = new AESCounterMode(symmetricKey);
        // apply AES
        final byte[] aesInput = new byte[data.length - encryptedBytes];
        System.arraycopy(data, encryptedBytes, aesInput, 0, aesInput.length);
        final byte[] part2 = aes.processStream(aesInput);

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

        return result;

    } catch (final InvalidCipherTextException e) {
        logger.error("Encryption.asymDecrypt(): can't decrypt cipher text:" + e.getMessage());
        throw new TorException("Encryption.asymDecrypt(): InvalidCipherTextException:" + e.getMessage());
    }
}

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

License:Open Source License

/**
 * decrypt data with asymmetric key. create asymmetrically encrypted data:<br>
 * <ul>// w w  w.  ja  va 2s .  c o  m
 * <li>OAEP padding [42 bytes] (RSA-encrypted)</li>
 * <li>Symmetric key [16 bytes]</li>
 * <li>First part of data [70 bytes]</li>
 * <li>Second part of data [x-70 bytes] (Symmetrically encrypted)</li>
 * </ul>
 * encrypt and store in result
 *
 * @param priv key to use for decryption
 * @param data to be decrypted, needs currently to be at least 70 bytes long
 * @return raw data
 */
public static byte[] asymDecrypt(final RSAPrivateKey priv, final 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;

        // init OAEP
        final OAEPEncoding oaep = new OAEPEncoding(new RSAEngine());
        oaep.init(false, new RSAKeyParameters(true, priv.getModulus(), priv.getPrivateExponent()));
        // apply RSA+OAEP
        encryptedBytes = oaep.getInputBlockSize();
        final byte[] oaepInput = new byte[encryptedBytes];
        System.arraycopy(data, 0, oaepInput, 0, encryptedBytes);
        final byte[] part1 = oaep.decodeBlock(oaepInput, 0, encryptedBytes);

        // extract symmetric key
        final byte[] symmetricKey = new byte[16];
        System.arraycopy(part1, 0, symmetricKey, 0, 16);
        // init AES
        final AESCounterMode aes = new AESCounterMode(symmetricKey);
        // apply AES
        final byte[] aesInput = new byte[data.length - encryptedBytes];
        System.arraycopy(data, encryptedBytes, aesInput, 0, aesInput.length);
        final byte[] part2 = aes.processStream(aesInput);

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

        return result;

    } catch (final InvalidCipherTextException e) {
        LOG.error("Encryption.asymDecrypt(): can't decrypt cipher text:" + e.getMessage());
        throw new TorException("Encryption.asymDecrypt(): InvalidCipherTextException:" + e.getMessage());
    }
}

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

License:Open Source License

/**
 * decrypt data with asymmetric key. create asymmetrically encrypted data:<br>
 * <ul>/*from   w  w w .j a  v  a  2s . co m*/
 * <li>OAEP padding [42 bytes] (RSA-encrypted)
 * <li>Symmetric key [16 bytes]
 * <li>First part of data [70 bytes]
 * <li>Second part of data [x-70 bytes] (Symmetrically encrypted)
 * <ul>
 * encrypt and store in result
 * 
 * @param priv
 *            key to use for decryption
 * @param data
 *            to be decrypted, needs currently to be at least 70 bytes long
 * @return raw data
 */
public static byte[] asymDecrypt(RSAPrivateKey priv, 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;

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

        // extract symmetric key
        byte[] symmetricKey = new byte[16];
        System.arraycopy(part1, 0, symmetricKey, 0, 16);
        // init 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 - 16 + part2.length];
        System.arraycopy(part1, 16, result, 0, part1.length - 16);
        System.arraycopy(part2, 0, result, part1.length - 16, part2.length);

        return result;

    } catch (InvalidCipherTextException e) {
        log.severe("CommonEncryption.asymDecrypt(): can't decrypt cipher text:" + e.getMessage());
        throw new TorException("CommonEncryption.asymDecrypt(): InvalidCipherTextException:" + e.getMessage());
    }
}

From source file:TorJava.Common.Encryption.java

License:Open Source License

/**
 * encrypt data with asymmetric key. create asymmetrically encrypted data:<br>
 * <ul>//from  w  ww.  j a  v  a2 s . c  om
 * <li>OAEP padding [42 bytes] (RSA-encrypted)
 * <li>Symmetric key [16 bytes]
 * <li>First part of data [70 bytes]
 * <li>Second part of data [x-70 bytes] (Symmetrically encrypted)
 * <ul>
 * encrypt and store in result
 * 
 * @param priv
 *            key to use for decryption
 * @param data
 *            to be decrypted, needs currently to be at least 70 bytes long
 * @return raw data
 */
public static byte[] asym_decrypt(RSAKeyParameters priv, 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(false, priv);
        // 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.decodeBlock(oaep_input, 0, encrypted_bytes);

        // extract symmetric key
        byte[] symmetric_key = new byte[16];
        System.arraycopy(part1, 0, symmetric_key, 0, 16);
        // init AES
        AESCounterMode aes = new AESCounterMode(true, symmetric_key);
        // 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 - 16 + part2.length];
        System.arraycopy(part1, 16, result, 0, part1.length - 16);
        System.arraycopy(part2, 0, result, part1.length - 16, part2.length);

        return result;

    } catch (InvalidCipherTextException e) {
        Logger.logCell(Logger.ERROR,
                "CommonEncryption.asym_decrypt(): can't decrypt cipher text:" + e.getMessage());
        throw new TorException("CommonEncryption.asym_decrypt(): InvalidCipherTextException:" + e.getMessage());
    }

}