当前位置: 首页>>代码示例>>Java>>正文


Java OAEPEncoding.getInputBlockSize方法代码示例

本文整理汇总了Java中org.bouncycastle.crypto.encodings.OAEPEncoding.getInputBlockSize方法的典型用法代码示例。如果您正苦于以下问题:Java OAEPEncoding.getInputBlockSize方法的具体用法?Java OAEPEncoding.getInputBlockSize怎么用?Java OAEPEncoding.getInputBlockSize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.bouncycastle.crypto.encodings.OAEPEncoding的用法示例。


在下文中一共展示了OAEPEncoding.getInputBlockSize方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: asymEncrypt

import org.bouncycastle.crypto.encodings.OAEPEncoding; //导入方法依赖的package包/类
/**
 * encrypt data with asymmetric key. create asymmetricla encrypted data:<br>
 * <ul>
 * <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());
    }
}
 
开发者ID:sirvaliance,项目名称:netlib,代码行数:56,代码来源:Encryption.java

示例2: asymDecrypt

import org.bouncycastle.crypto.encodings.OAEPEncoding; //导入方法依赖的package包/类
/**
 * decrypt data with asymmetric key. create asymmetrically encrypted data:<br>
 * <ul>
 * <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());
    }
}
 
开发者ID:rovemonteux,项目名称:silvertunnel-monteux,代码行数:58,代码来源:Encryption.java

示例3: asymDecrypt

import org.bouncycastle.crypto.encodings.OAEPEncoding; //导入方法依赖的package包/类
/**
 * decrypt data with asymmetric key. create asymmetrically encrypted data:<br>
 * <ul>
 * <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());
    }
}
 
开发者ID:sirvaliance,项目名称:netlib,代码行数:61,代码来源:Encryption.java


注:本文中的org.bouncycastle.crypto.encodings.OAEPEncoding.getInputBlockSize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。