Example usage for org.bouncycastle.util Arrays concatenate

List of usage examples for org.bouncycastle.util Arrays concatenate

Introduction

In this page you can find the example usage for org.bouncycastle.util Arrays concatenate.

Prototype

public static int[] concatenate(int[] a, int[] b) 

Source Link

Usage

From source file:Algorithms.Asymmetric.blockElGamal.java

@Override
public CrypticObject encrypt(byte[] message) {

    context = new ClassPathXmlApplicationContext("spring.xml");
    algo = (CrypticAlgo) context.getBean("basicElGamal");
    CrypticObject tmp;//  w w  w. ja  v  a 2 s . c  o  m
    crypt = new CrypticObject();
    crypt.data = msgBytes;
    int sTime, eTime;

    beg = 0;
    length = message.length;
    sTime = eTime = 0;

    while (beg < length) {
        end = (beg + 19 >= length) ? (length) : (beg + 19);
        msgBytes = Arrays.copyOfRange(message, beg, end);
        tmp = algo.encrypt(msgBytes);

        crypt.time += tmp.time;
        crypt.data = Arrays.concatenate(crypt.data, tmp.data);
        beg += 19;

    }

    return crypt;
}

From source file:Algorithms.Asymmetric.blockElGamal.java

@Override
public CrypticObject decrypt(byte[] message) {
    CrypticObject tmp;//from ww  w.  jav  a 2  s  . c  o m
    crypt = new CrypticObject();
    int sTime, eTime;

    beg = 0;
    length = message.length;
    sTime = eTime = 0;

    while (beg < length) {
        end = (beg + 40 >= length) ? (length) : (beg + 40);
        msgBytes = Arrays.copyOfRange(message, beg, end);
        tmp = algo.decrypt(msgBytes);

        crypt.time += tmp.time;
        crypt.data = Arrays.concatenate(crypt.data, tmp.data);
        beg += 40;

    }

    return crypt;
}

From source file:Algorithms.Asymmetric.blockRSA.java

@Override
public CrypticObject encrypt(byte[] message) {

    context = new ClassPathXmlApplicationContext("spring.xml");
    algo = (CrypticAlgo) context.getBean("basicRSA");
    CrypticObject tmp;/*ww  w  . j av  a 2  s  . c  o  m*/
    crypt = new CrypticObject();
    crypt.data = msgBytes;
    int sTime, eTime;

    beg = 0;
    length = message.length;
    sTime = eTime = 0;

    while (beg < length) {
        end = (beg + 245 >= length) ? (length) : (beg + 245);
        msgBytes = Arrays.copyOfRange(message, beg, end);
        tmp = algo.encrypt(msgBytes);

        crypt.time += tmp.time;
        crypt.data = Arrays.concatenate(crypt.data, tmp.data);
        beg += 245;

    }

    return crypt;
}

From source file:Algorithms.Asymmetric.blockRSA.java

@Override
public CrypticObject decrypt(byte[] message) {
    CrypticObject tmp;//from w  w w. java2s .c  o m
    crypt = new CrypticObject();
    int sTime, eTime;

    beg = 0;
    length = message.length;
    sTime = eTime = 0;

    while (beg < length) {
        end = (beg + 256 >= length) ? (length) : (beg + 256);
        msgBytes = Arrays.copyOfRange(message, beg, end);
        tmp = algo.decrypt(msgBytes);

        crypt.time += tmp.time;
        crypt.data = Arrays.concatenate(crypt.data, tmp.data);
        beg += 256;

    }

    return crypt;
}

From source file:cc.agentx.wrapper.FakedHttpWrapper.java

License:Apache License

private byte[] warpInRequest(final byte[] bytes) {
    String encodedStr = encoder.encodeToString(bytes).replaceAll("=", "");

    // 20 percent of small data are faked by post method requests
    if (bytes.length > 512 || KeyHelper.generateRandomInteger(0, 10) < 2) {
        String header = HttpFaker.getRandomRequestHeader(Http.METHOD_POST, true);
        header = header.replaceAll(Matcher.quoteReplacement("$"), String.valueOf(encodedStr.length()));
        return Arrays.concatenate(header.getBytes(), encodedStr.getBytes());
    } else {//from  w w  w. ja v  a 2 s.c om
        String header = HttpFaker.getRandomRequestHeader(Http.METHOD_GET, true);
        header = header.replaceAll(Matcher.quoteReplacement("$"), encodedStr);
        return header.getBytes();
    }
}

From source file:cc.agentx.wrapper.FakedHttpWrapper.java

License:Apache License

private byte[] wrapInResponse(final byte[] bytes) {
    String header = HttpFaker.getRandomResponseHeader(Http.RESPONSE_200, true);
    header = header.replaceAll(Matcher.quoteReplacement("$"), String.valueOf(bytes.length));
    return Arrays.concatenate(header.getBytes(), bytes);
}

From source file:co.rsk.mine.MinerServerImpl.java

License:Open Source License

public static byte[] compressCoinbase(byte[] bitcoinMergedMiningCoinbaseTransactionSerialized,
        boolean lastOccurrence) {
    List<Byte> coinBaseTransactionSerializedAsList = java.util.Arrays
            .asList(ArrayUtils.toObject(bitcoinMergedMiningCoinbaseTransactionSerialized));
    List<Byte> tagAsList = java.util.Arrays.asList(ArrayUtils.toObject(RskMiningConstants.RSK_TAG));

    int rskTagPosition;
    if (lastOccurrence) {
        rskTagPosition = Collections.lastIndexOfSubList(coinBaseTransactionSerializedAsList, tagAsList);
    } else {/* w w  w .  jav a2 s.  c o  m*/
        rskTagPosition = Collections.indexOfSubList(coinBaseTransactionSerializedAsList, tagAsList);
    }

    int remainingByteCount = bitcoinMergedMiningCoinbaseTransactionSerialized.length - rskTagPosition
            - RskMiningConstants.RSK_TAG.length - RskMiningConstants.BLOCK_HEADER_HASH_SIZE;
    if (remainingByteCount > RskMiningConstants.MAX_BYTES_AFTER_MERGED_MINING_HASH) {
        throw new IllegalArgumentException("More than 128 bytes after RSK tag");
    }
    int sha256Blocks = rskTagPosition / 64;
    int bytesToHash = sha256Blocks * 64;
    SHA256Digest digest = new SHA256Digest();
    digest.update(bitcoinMergedMiningCoinbaseTransactionSerialized, 0, bytesToHash);
    byte[] hashedContent = digest.getEncodedState();
    byte[] trimmedHashedContent = new byte[RskMiningConstants.MIDSTATE_SIZE_TRIMMED];
    System.arraycopy(hashedContent, 8, trimmedHashedContent, 0, RskMiningConstants.MIDSTATE_SIZE_TRIMMED);
    byte[] unHashedContent = new byte[bitcoinMergedMiningCoinbaseTransactionSerialized.length - bytesToHash];
    System.arraycopy(bitcoinMergedMiningCoinbaseTransactionSerialized, bytesToHash, unHashedContent, 0,
            unHashedContent.length);
    return Arrays.concatenate(trimmedHashedContent, unHashedContent);
}

From source file:co.rsk.mine.MinerUtils.java

License:Open Source License

public static co.rsk.bitcoinj.core.BtcTransaction getBitcoinMergedMiningCoinbaseTransactionWithTwoTags(
        co.rsk.bitcoinj.core.NetworkParameters params, byte[] blockHashForMergedMining1,
        byte[] blockHashForMergedMining2) {
    co.rsk.bitcoinj.core.BtcTransaction coinbaseTransaction = new co.rsk.bitcoinj.core.BtcTransaction(params);
    //Add a random number of random bytes before the RSK tag
    SecureRandom random = new SecureRandom();
    byte[] prefix = new byte[random.nextInt(1000)];
    random.nextBytes(prefix);//from   ww  w.java  2 s . c o m

    byte[] bytes0 = Arrays.concatenate(RskMiningConstants.RSK_TAG, blockHashForMergedMining1);
    // addsecond tag
    byte[] bytes1 = Arrays.concatenate(bytes0, RskMiningConstants.RSK_TAG, blockHashForMergedMining2);

    co.rsk.bitcoinj.core.TransactionInput ti = new co.rsk.bitcoinj.core.TransactionInput(params,
            coinbaseTransaction, prefix);
    coinbaseTransaction.addInput(ti);
    ByteArrayOutputStream scriptPubKeyBytes = new ByteArrayOutputStream();
    co.rsk.bitcoinj.core.BtcECKey key = new co.rsk.bitcoinj.core.BtcECKey();
    try {
        co.rsk.bitcoinj.script.Script.writeBytes(scriptPubKeyBytes, key.getPubKey());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    scriptPubKeyBytes.write(co.rsk.bitcoinj.script.ScriptOpCodes.OP_CHECKSIG);
    coinbaseTransaction.addOutput(new co.rsk.bitcoinj.core.TransactionOutput(params, coinbaseTransaction,
            co.rsk.bitcoinj.core.Coin.valueOf(50, 0), scriptPubKeyBytes.toByteArray()));
    // add opreturn output with two tags
    ByteArrayOutputStream output2Bytes = new ByteArrayOutputStream();
    output2Bytes.write(co.rsk.bitcoinj.script.ScriptOpCodes.OP_RETURN);

    try {
        co.rsk.bitcoinj.script.Script.writeBytes(output2Bytes, bytes1);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    coinbaseTransaction.addOutput(new co.rsk.bitcoinj.core.TransactionOutput(params, coinbaseTransaction,
            co.rsk.bitcoinj.core.Coin.valueOf(1), output2Bytes.toByteArray()));

    return coinbaseTransaction;
}

From source file:com.bitbreeds.webrtc.dtls.WebrtcDtlsServer.java

License:Open Source License

protected int[] getCipherSuites() {
    return Arrays.concatenate(super.getCipherSuites(),
            new int[] { CipherSuite.DRAFT_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 });
}

From source file:com.eucalyptus.auth.DatabaseAccountProxy.java

License:Open Source License

@Override
public ServerCertificate addServerCertificate(String certName, String certBody, String certChain,
        String certPath, String pk) throws AuthException {
    if (!ServerCertificateEntity.isCertificateNameValid(certName))
        throw new AuthException(AuthException.INVALID_SERVER_CERT_NAME);
    if (!ServerCertificateEntity.isCertificatePathValid(certPath))
        throw new AuthException(AuthException.INVALID_SERVER_CERT_PATH);

    String encPk = null;/* w w  w.  jav  a2 s  . c o  m*/
    String sessionKey = null;
    try {
        // generate symmetric key
        final MessageDigest digest = Digest.SHA256.get();
        final byte[] salt = new byte[32];
        Crypto.getSecureRandomSupplier().get().nextBytes(salt);
        digest.update(this.lookupAdmin().getPassword().getBytes(Charsets.UTF_8));
        digest.update(salt);
        final SecretKey symmKey = new SecretKeySpec(digest.digest(), "AES");

        // encrypt the server pk
        Cipher cipher = Ciphers.AES_GCM.get();
        final byte[] iv = new byte[32];
        Crypto.getSecureRandomSupplier().get().nextBytes(iv);
        cipher.init(Cipher.ENCRYPT_MODE, symmKey, new IvParameterSpec(iv));
        final byte[] cipherText = cipher.doFinal(pk.getBytes());
        encPk = new String(Base64.encode(Arrays.concatenate(iv, cipherText)));

        final PublicKey euarePublicKey = SystemCredentials.lookup(Euare.class).getCertificate().getPublicKey();
        cipher = Ciphers.RSA_PKCS1.get();
        cipher.init(Cipher.WRAP_MODE, euarePublicKey);
        byte[] wrappedKeyBytes = cipher.wrap(symmKey);
        sessionKey = new String(Base64.encode(wrappedKeyBytes));
    } catch (final Exception e) {
        LOG.error("Failed to encrypt key", e);
        throw Exceptions.toUndeclared(e);
    }

    try {
        final ServerCertificate found = lookupServerCertificate(certName);
        if (found != null)
            throw new AuthException(AuthException.SERVER_CERT_ALREADY_EXISTS);
    } catch (final NoSuchElementException | AuthException ex) {
        ;
    } catch (final Exception ex) {
        throw ex;
    }

    final String certId = Crypto.generateAlphanumericId(21, "ASC");
    final EntityTransaction db = Entities.get(ServerCertificateEntity.class);
    ServerCertificateEntity entity = null;
    try {
        final UserFullName accountAdmin = UserFullName.getInstance(this.lookupAdmin());
        entity = new ServerCertificateEntity(accountAdmin, certName);
        entity.setCertBody(certBody);
        entity.setCertChain(certChain);
        entity.setCertPath(certPath);
        entity.setPrivateKey(encPk);
        entity.setSessionKey(sessionKey);
        entity.setCertId(certId);
        Entities.persist(entity);
        db.commit();
    } catch (final Exception ex) {
        LOG.error("Failed to persist server certificate entity", ex);
        throw Exceptions.toUndeclared(ex);
    } finally {
        if (db.isActive())
            db.rollback();
    }

    return ServerCertificates.ToServerCertificate.INSTANCE.apply(entity);
}