Example usage for org.bouncycastle.crypto AsymmetricBlockCipher getOutputBlockSize

List of usage examples for org.bouncycastle.crypto AsymmetricBlockCipher getOutputBlockSize

Introduction

In this page you can find the example usage for org.bouncycastle.crypto AsymmetricBlockCipher getOutputBlockSize.

Prototype

public int getOutputBlockSize();

Source Link

Document

returns the maximum size of the block produced by this cipher.

Usage

From source file:dorkbox.util.crypto.CryptoRSA.java

License:Apache License

/**
 * RSA encrypts data with a specified key.
 *
 * @param logger/*from ww  w. j  a v  a 2s  .c o  m*/
 *                 may be null, if no log output is necessary
 *
 * @return empty byte[] if error
 */
public static byte[] encrypt(AsymmetricBlockCipher rsaEngine, RSAKeyParameters rsaPublicKey, byte[] bytes,
        Logger logger) {
    rsaEngine.init(true, rsaPublicKey);

    try {
        int inputBlockSize = rsaEngine.getInputBlockSize();
        if (inputBlockSize < bytes.length) {
            int outSize = rsaEngine.getOutputBlockSize();
            //noinspection NumericCastThatLosesPrecision
            int realsize = (int) Math.round(bytes.length / (outSize * 1.0D) + 0.5);
            ByteBuffer buffer = ByteBuffer.allocateDirect(outSize * realsize);

            int position = 0;

            while (position < bytes.length) {
                int size = Math.min(inputBlockSize, bytes.length - position);

                byte[] block = rsaEngine.processBlock(bytes, position, size);
                buffer.put(block, 0, block.length);

                position += size;
            }

            return buffer.array();

        } else {
            return rsaEngine.processBlock(bytes, 0, bytes.length);
        }
    } catch (Exception e) {
        if (logger != null) {
            logger.error("Unable to perform RSA cipher.", e);
        }
        return new byte[0];
    }
}

From source file:dorkbox.util.crypto.CryptoRSA.java

License:Apache License

/**
 * RSA decrypt data with a specified key.
 *
 * @param logger/*from w ww.j  a  v  a  2 s. co  m*/
 *                 may be null, if no log output is necessary
 *
 * @return empty byte[] if error
 */
public static byte[] decrypt(AsymmetricBlockCipher rsaEngine, RSAPrivateCrtKeyParameters rsaPrivateKey,
        byte[] bytes, Logger logger) {
    rsaEngine.init(false, rsaPrivateKey);

    try {
        int inputBlockSize = rsaEngine.getInputBlockSize();
        if (inputBlockSize < bytes.length) {
            int outSize = rsaEngine.getOutputBlockSize();
            //noinspection NumericCastThatLosesPrecision
            int realsize = (int) Math.round(bytes.length / (outSize * 1.0D) + 0.5);
            ByteArrayOutputStream buffer = new ByteArrayOutputStream(outSize * realsize);

            int position = 0;

            while (position < bytes.length) {
                int size = Math.min(inputBlockSize, bytes.length - position);

                byte[] block = rsaEngine.processBlock(bytes, position, size);
                buffer.write(block, 0, block.length);

                position += size;
            }

            return buffer.toByteArray();
        } else {
            return rsaEngine.processBlock(bytes, 0, bytes.length);
        }
    } catch (Exception e) {
        if (logger != null) {
            logger.error("Unable to perform RSA cipher.", e);
        }
        return new byte[0];
    }
}

From source file:org.opcfoundation.ua.transport.security.BcCryptoProvider.java

License:Open Source License

@Override
public int decryptAsymm(PrivateKey decryptingKey, SecurityAlgorithm algorithm, byte[] dataToDecrypt,
        byte[] output, int outputOffset) throws ServiceResultException {

    java.security.interfaces.RSAPrivateCrtKey rsaPrivateKey = (java.security.interfaces.RSAPrivateCrtKey) decryptingKey;
    RSAPrivateKey privateKey = new RSAPrivateKey(rsaPrivateKey.getModulus(), rsaPrivateKey.getPublicExponent(),
            rsaPrivateKey.getPrivateExponent(), rsaPrivateKey.getPrimeP(), rsaPrivateKey.getPrimeQ(),
            rsaPrivateKey.getPrimeExponentP(), rsaPrivateKey.getPrimeExponentQ(),
            rsaPrivateKey.getCrtCoefficient());

    AsymmetricBlockCipher cipher = getAsymmetricCipher(algorithm, privateKey);

    try {/*from  w ww .j av  a2  s  .com*/

        int len = 0;
        int inputBlockSize = cipher.getInputBlockSize();
        int outputBlockSize = cipher.getOutputBlockSize();
        logger.debug("Decrypt: inputBlockSize={}, outputBlockSize={}, dataToDecrypt.length={}", inputBlockSize,
                outputBlockSize, dataToDecrypt.length);
        for (int i = 0; i < dataToDecrypt.length; i += inputBlockSize) {
            int size = Math.min(dataToDecrypt.length - i, inputBlockSize);
            byte[] tmp = cipher.processBlock(dataToDecrypt, i, size);
            System.arraycopy(tmp, 0, output, outputOffset + len, tmp.length);
            len += tmp.length;
        }
        return len;

    } catch (CryptoException e) {
        throw new ServiceResultException(StatusCodes.Bad_InternalError, e);
    }

}

From source file:org.opcfoundation.ua.transport.security.BcCryptoProvider.java

License:Open Source License

public void encryptAsymm(PublicKey encryptingCertificate, SecurityAlgorithm algorithm, byte[] dataToEncrypt,
        byte[] output, int outputOffset) throws ServiceResultException {

    try {/*from   ww w .j av  a2  s  . c om*/
        java.security.interfaces.RSAPublicKey encryptingCertificateRSA = (java.security.interfaces.RSAPublicKey) encryptingCertificate;
        RSAPublicKey publicKey = new RSAPublicKey(encryptingCertificateRSA.getModulus(),
                encryptingCertificateRSA.getPublicExponent());
        AsymmetricBlockCipher cipher = getAsymmetricCipher(algorithm, publicKey);

        int len = 0;
        int inputBlockSize = cipher.getInputBlockSize();
        int outputBlockSize = cipher.getOutputBlockSize();
        logger.debug("Encrypt: inputBlockSize={}, outputBlockSize={}, dataToEncrypt.length={}", inputBlockSize,
                outputBlockSize, dataToEncrypt.length);
        for (int i = 0; i < dataToEncrypt.length; i += inputBlockSize) {
            int size = Math.min(dataToEncrypt.length - i, inputBlockSize);
            byte[] tmp = cipher.processBlock(dataToEncrypt, i, size);
            System.arraycopy(tmp, 0, output, outputOffset + len, tmp.length);
            len += tmp.length;
        }

    } catch (InvalidCipherTextException e) {
        throw new ServiceResultException(StatusCodes.Bad_InternalError, e);
    }

}