Example usage for java.math BigInteger toByteArray

List of usage examples for java.math BigInteger toByteArray

Introduction

In this page you can find the example usage for java.math BigInteger toByteArray.

Prototype

public byte[] toByteArray() 

Source Link

Document

Returns a byte array containing the two's-complement representation of this BigInteger.

Usage

From source file:com.android.email.codec.binary.Base64.java

/**
 * Returns a byte-array representation of a <code>BigInteger</code>
 * without sign bit.//from   www  .  j  a  v  a  2  s .com
 *
 * @param bigInt <code>BigInteger</code> to be converted
 * @return a byte array representation of the BigInteger parameter
 */
static byte[] toIntegerBytes(BigInteger bigInt) {
    int bitlen = bigInt.bitLength();
    // round bitlen
    bitlen = ((bitlen + 7) >> 3) << 3;
    byte[] bigBytes = bigInt.toByteArray();

    if (((bigInt.bitLength() % 8) != 0) && (((bigInt.bitLength() / 8) + 1) == (bitlen / 8))) {
        return bigBytes;
    }

    // set up params for copying everything but sign bit
    int startSrc = 0;
    int len = bigBytes.length;

    // if bigInt is exactly byte-aligned, just skip signbit in copy
    if ((bigInt.bitLength() % 8) == 0) {
        startSrc = 1;
        len--;
    }

    int startDst = bitlen / 8 - len; // to pad w/ nulls as per spec
    byte[] resizedBytes = new byte[bitlen / 8];

    System.arraycopy(bigBytes, startSrc, resizedBytes, startDst, len);

    return resizedBytes;
}

From source file:com.coroptis.coidi.core.services.impl.ConvertorServiceImpl.java

@Override
public String convertToString(BigInteger b) {
    return convertToString(b.toByteArray());
}

From source file:com.og.elliptic.sdk.main.java

private static void elGamalString(WeierStrassCurve curve) {
    // Droulement de El Gamal
    // Cryptage/*from w ww .j a va  2s.  c  om*/
    PointGMP generateur = new PointGMP(curve.getGx(), curve.getGy(), curve);// le point gnrateur qui servira  calculer notre clef
    // secrte
    Random rnd = new Random();

    BigInteger clefSecrete = BigInteger.probablePrime(7, rnd);
    PointGMP publicKey = generateur.mult(clefSecrete);// on calcule notre clef secrte, qui nous servira  crypter les messages (h)

    BigInteger m = new BigInteger(Base64.decodeBase64("bonjour lesil"));

    BigInteger k = BigInteger.probablePrime(7, rnd);// on choisit un k random

    PointGMP C1 = generateur.mult(k);// on multiplie le gnrateur par k
    PointGMP tmp = publicKey.mult(k);
    BigInteger C2 = m.add(tmp.getX());// on multiplie notre clef secrte par k

    // Dcryptage
    PointGMP temp = C1.mult(clefSecrete);// on multiplie C1 par notre clef secrte
    BigInteger messageTraduit = C2.subtract(temp.getX());// et on obtient le message en ajoutant m2 avec l'oppos de temp
    messageTraduit = messageTraduit.mod(curve.getP());

    if (m == messageTraduit)
        System.out.println("El Gamal fonctionnel");
    else
        System.out.println("El Gamal non fonctionnel");

    System.out.println("Message a retrouver " + m + " " + Base64.encodeBase64String(m.toByteArray()) + "\n");
    System.out.println("Resultat El gamal " + messageTraduit + " "
            + Base64.encodeBase64String(messageTraduit.toByteArray()) + "\n");

}

From source file:co.rsk.blockchain.utils.BlockGenerator.java

public static Block createChildBlock(Block parent, List<Transaction> txs, List<BlockHeader> uncles,
        long difficulty, BigInteger minGasPrice) {
    if (txs == null)
        txs = new ArrayList<>();
    if (uncles == null)
        uncles = new ArrayList<>();

    Bloom logBloom = new Bloom();
    byte[] bidiff = BigInteger.valueOf(difficulty).toByteArray();
    byte[] unclesListHash = HashUtil.sha3(BlockHeader.getUnclesEncodedEx(uncles));

    BlockHeader newHeader = new BlockHeader(parent.getHash(), unclesListHash, parent.getCoinbase(),
            ByteUtils.clone(new Bloom().getData()), new byte[] { 1 }, parent.getNumber() + 1,
            parent.getGasLimit(), 0, parent.getTimestamp() + ++count, new byte[] {}, new byte[] {},
            new byte[] {}, new byte[] {}, (minGasPrice != null) ? minGasPrice.toByteArray() : null,
            CollectionUtils.size(uncles));

    if (difficulty == 0)
        newHeader.setDifficulty(newHeader.calcDifficulty(parent.getHeader()).toByteArray());
    else//  ww  w  .  jav a2s.  c  o m
        newHeader.setDifficulty(BigInteger.valueOf(difficulty).toByteArray());

    newHeader.setTransactionsRoot(Block.getTxTrie(txs).getHash());

    newHeader.setStateRoot(parent.getStateRoot());

    Block newBlock = new Block(newHeader, txs, uncles);

    return newBlock;
}

From source file:org.rifidi.emulator.tags.impl.C0G1Tag.java

public C0G1Tag(BigInteger id) {
    super();
    this.id = id.toByteArray();
}

From source file:com.l2jfree.tools.math.ScrambledKeyPair.java

private byte[] scrambleModulus(BigInteger modulus) {
    byte[] retScrambledModulus = modulus.toByteArray();

    if (retScrambledModulus.length == 0x81 && retScrambledModulus[0] == 0x00) {
        byte[] temp = new byte[0x80];
        System.arraycopy(retScrambledModulus, 1, temp, 0, 0x80);
        retScrambledModulus = temp;//from  ww w .  jav a  2 s .c  om
    }
    // step 1 : 0x4d-0x50 <-> 0x00-0x04
    for (int i = 0; i < 4; i++) {
        byte temp = retScrambledModulus[0x00 + i];
        retScrambledModulus[0x00 + i] = retScrambledModulus[0x4d + i];
        retScrambledModulus[0x4d + i] = temp;
    }
    // step 2 : xor first 0x40 bytes with last 0x40 bytes
    for (int i = 0; i < 0x40; i++) {
        retScrambledModulus[i] = (byte) (retScrambledModulus[i] ^ retScrambledModulus[0x40 + i]);
    }
    // step 3 : xor bytes 0x0d-0x10 with bytes 0x34-0x38
    for (int i = 0; i < 4; i++) {
        retScrambledModulus[0x0d + i] = (byte) (retScrambledModulus[0x0d + i] ^ retScrambledModulus[0x34 + i]);
    }
    // step 4 : xor last 0x40 bytes with first 0x40 bytes
    for (int i = 0; i < 0x40; i++) {
        retScrambledModulus[0x40 + i] = (byte) (retScrambledModulus[0x40 + i] ^ retScrambledModulus[i]);
    }
    if (_log.isDebugEnabled())
        _log.debug("Modulus was scrambled");

    return retScrambledModulus;
}

From source file:org.calrissian.mango.types.encoders.lexi.BigIntegerEncoder.java

@Override
public String encode(BigInteger value) {
    checkNotNull(value, "Null values are not allowed");

    byte[] bytes = value.toByteArray();
    int length = bytes.length;

    //Length is always positive so use it to encode the actual sign of the big int.
    if (value.signum() < 0)
        length = -length;/*  w w w  .jav a 2  s .co  m*/

    return integerEncoder.encode(length) + new String(encodeHex(bytes));
}

From source file:cl.niclabs.tscrypto.common.datatypes.BigIntegerBase64TypeAdapter.java

@Override
public JsonElement serialize(BigInteger src, Type typeOfSrc, JsonSerializationContext context) {
    return new JsonPrimitive(new String(Base64.encodeBase64(src.toByteArray())));
}

From source file:org.rifidi.edge.readerplugin.awid.awid2010.communication.messages.Gen2ReadBlockDataResponse.java

@Override
public TagReadEvent getTagReadEvent() {
    EPCGeneration2Event gen2Event = new EPCGeneration2Event();
    byte[] shiftedmessage = new byte[super.rawmessage.length];
    BigInteger bi = new BigInteger(super.rawmessage);
    BigInteger bi2 = bi.shiftLeft(1);

    shiftedmessage = bi2.toByteArray();
    if (memorybank == 0) {
        logger.debug("Reserved Memory Bank" + new String(Hex.encodeHex(shiftedmessage)));
        //TODO: put bytes in reserved mem bank
    } else if (memorybank == 1) {
        logger.debug("EPC Memory Bank" + new String(Hex.encodeHex(shiftedmessage)));
        //TODO: what if we don't have 96 bits?
        gen2Event.setEPCMemory(new BigInteger(Arrays.copyOfRange(shiftedmessage, 7, 19)), 12 * 8);
    } else if (memorybank == 2) {
        logger.debug("TID Memory Bank " + new String(Hex.encodeHex(shiftedmessage)));

        // TODO: this is not quite correct since it assumes that we have 96
        // bits/*from   w w  w . j  a v  a  2s .  c  om*/
        gen2Event.setTIDMemory(new BigInteger(Arrays.copyOfRange(shiftedmessage, 3, 17)), 12 * 8);
    } else if (memorybank == 3) {
        logger.debug("User Memory Bank" + new String(Hex.encodeHex(shiftedmessage)));
        // TODO: insert into user memory
    }

    TagReadEvent tre;
    if (super.rawmessage.length == 22) {
        logger.debug("AntennaID " + super.rawmessage[19]);
        tre = new TagReadEvent(readerID, gen2Event, super.rawmessage[19], timestamp);
    } else {
        logger.warn("No Antenna byte, assuming default value of " + 1);
        tre = new TagReadEvent(readerID, gen2Event, 1, timestamp);
    }
    return tre;
}

From source file:org.rifidi.edge.adapter.awid.awid2010.communication.messages.Gen2ReadBlockDataResponse.java

@Override
public TagReadEvent getTagReadEvent() {
    EPCGeneration2Event gen2Event = new EPCGeneration2Event();
    byte[] shiftedmessage = new byte[super.rawmessage.length];
    BigInteger bi = new BigInteger(super.rawmessage);
    BigInteger bi2 = bi.shiftLeft(1);

    shiftedmessage = bi2.toByteArray();
    if (memorybank == 0) {
        logger.debug("Reserved Memory Bank" + new String(Hex.encodeHex(shiftedmessage)));
        //TODO: put bytes in reserved mem bank
    } else if (memorybank == 1) {
        logger.debug("EPC Memory Bank" + new String(Hex.encodeHex(shiftedmessage)));
        //TODO: what if we don't have 96 bits?
        BigInteger epc = new BigInteger(Arrays.copyOfRange(shiftedmessage, 7, 19));
        gen2Event.setEPCMemory(epc, epc.toString(16), 12 * 8);
    } else if (memorybank == 2) {
        logger.debug("TID Memory Bank " + new String(Hex.encodeHex(shiftedmessage)));

        // TODO: this is not quite correct since it assumes that we have 96
        // bits/*from w w  w .  j  av a2s  . c o m*/
        gen2Event.setTIDMemory(new BigInteger(Arrays.copyOfRange(shiftedmessage, 3, 17)), 12 * 8);
    } else if (memorybank == 3) {
        logger.debug("User Memory Bank" + new String(Hex.encodeHex(shiftedmessage)));
        // TODO: insert into user memory
    }

    TagReadEvent tre;
    if (super.rawmessage.length == 22) {
        logger.debug("AntennaID " + super.rawmessage[19]);
        tre = new TagReadEvent(readerID, gen2Event, super.rawmessage[19], timestamp);
    } else {
        logger.warn("No Antenna byte, assuming default value of " + 1);
        tre = new TagReadEvent(readerID, gen2Event, 1, timestamp);
    }
    return tre;
}