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:Main.java

public static byte[] getBigIntegerBytes(BigInteger value) {
    byte[] bytes = value.toByteArray();
    if (bytes.length > 256) {
        byte[] correctedAuth = new byte[256];
        System.arraycopy(bytes, 1, correctedAuth, 0, 256);
        return correctedAuth;
    } else if (bytes.length < 256) {
        byte[] correctedAuth = new byte[256];
        System.arraycopy(bytes, 0, correctedAuth, 256 - bytes.length, bytes.length);
        for (int a = 0; a < 256 - bytes.length; a++) {
            correctedAuth[a] = 0;/*from  w  ww  .  jav a  2s .c om*/
        }
        return correctedAuth;
    }
    return bytes;
}

From source file:Main.java

public static void writeBigInteger(ByteArrayOutputStream baos, BigInteger bi) {
    writeData(baos, bi.toByteArray());
}

From source file:Main.java

public static byte[] toBytes(long num) {
    BigInteger a = BigInteger.valueOf(num);
    byte[] temp = a.toByteArray();
    byte[] tr = new byte[temp.length];

    for (int i = 0; i < tr.length; i++) {
        tr[i] = temp[temp.length - 1 - i];
    }//from w  ww  .  ja  v a 2s.  c om
    return tr;
}

From source file:Main.java

public static byte[] getRSAHackdData(byte[] dataWithHash) {
    BigInteger modulus = new BigInteger(
            "C150023E2F70DB7985DED064759CFECF0AF328E69A41DAF4D6F01B538135A6F91F8F8B2A0EC9BA9720CE352EFCF6C5680FFC424BD634864902DE0B4BD6D49F4E580230E3AE97D95C8B19442B3C0A10D8F5633FECEDD6926A7F6DAB0DDB7D457F9EA81B8465FCD6FFFEED114011DF91C059CAEDAF97625F6C96ECC74725556934EF781D866B34F011FCE4D835A090196E9A5F0E4449AF7EB697DDB9076494CA5F81104A305B6DD27665722C46B60E5DF680FB16B210607EF217652E60236C255F6A28315F4083A96791D7214BF64C1DF4FD0DB1944FB26A2A57031B32EEE64AD15A8BA68885CDE74A5BFC920F6ABF59BA5C75506373E7130F9042DA922179251F",
            16);//from www . j a va 2  s . c o  m
    BigInteger pubExp = new BigInteger("010001", 16);

    BigInteger r = new BigInteger(dataWithHash);

    BigInteger s = r.modPow(pubExp, modulus);
    byte[] temp = s.toByteArray();
    return Arrays.copyOfRange(temp, temp.length - 256, temp.length);
}

From source file:Main.java

/**
 * Transforms a number String to a byte array
 * @param in number String//from   w ww  .j  a va2 s .c o m
 * @return
 */
public static byte[] NumStringToBytes(String in) {
    BigInteger num = new BigInteger(in);
    byte[] bytes = num.toByteArray();
    if (bytes.length > 0) {
        if (bytes[0] == 0) {
            byte[] cuttedByte = new byte[bytes.length - 1];
            System.arraycopy(bytes, 1, cuttedByte, 0, bytes.length - 1);
            return cuttedByte;
        }

    }
    return num.toByteArray();
}

From source file:Main.java

public static byte[] getPrivateKeyBytes(BigInteger privateKey) {
    byte[] privateKeyPlainNumber = privateKey.toByteArray();
    int plainNumbersOffs = privateKeyPlainNumber[0] == 0 ? 1 : 0;
    byte[] privateKeyBytes = new byte[32];
    System.arraycopy(privateKeyPlainNumber, plainNumbersOffs, privateKeyBytes,
            privateKeyBytes.length - (privateKeyPlainNumber.length - plainNumbersOffs),
            privateKeyPlainNumber.length - plainNumbersOffs);
    return privateKeyBytes;
}

From source file:Hex.java

/**
 * Decodar hex till binrt/*from  w  w  w.j  ava2  s.c  o  m*/
 *
 *@param dataStr Strng innehllande hex-representation av data
 *@return byte[] innhllande binr representation av data
 **/
public static byte[] decode(String dataStr) {

    if ((dataStr.length() & 0x01) == 0x01)
        dataStr = new String(dataStr + "0");
    BigInteger cI = new BigInteger(dataStr, 16);
    byte[] data = cI.toByteArray();

    return data;
}

From source file:org.gluu.oxeleven.util.Base64Util.java

public static String base64UrlEncodeUnsignedBigInt(BigInteger bigInteger) {
    byte[] array = bigInteger.toByteArray();
    if (array[0] == 0) {
        byte[] tmp = new byte[array.length - 1];
        System.arraycopy(array, 1, tmp, 0, tmp.length);
        array = tmp;/*  ww w.  j a  v  a 2s.c  om*/
    }

    return base64UrlEncode(array);
}

From source file:Main.java

/**
 *  @param bi non-negative//from   ww w.j a  v a  2 s.co m
 *  @return array of exactly len bytes
 */
public static byte[] rectify(BigInteger bi, int len) throws InvalidKeyException {
    byte[] b = bi.toByteArray();
    if (b.length == len) {
        // just right
        return b;
    }
    if (b.length > len + 1)
        throw new InvalidKeyException("key too big (" + b.length + ") max is " + (len + 1));
    byte[] rv = new byte[len];
    if (b.length == 0)
        return rv;
    if ((b[0] & 0x80) != 0)
        throw new InvalidKeyException("negative");
    if (b.length > len) {
        // leading 0 byte
        if (b[0] != 0)
            throw new InvalidKeyException("key too big (" + b.length + ") max is " + len);
        System.arraycopy(b, 1, rv, 0, len);
    } else {
        // smaller
        System.arraycopy(b, 0, rv, len - b.length, b.length);
    }
    return rv;
}

From source file:ru.jts_dev.authserver.config.KeyGenerationConfig.java

static byte[] scrambleModulus(BigInteger modulus) {
    byte[] scrambledMod = modulus.toByteArray();

    if (scrambledMod.length == 0x81 && scrambledMod[0] == 0x00) {
        byte[] temp = new byte[0x80];
        System.arraycopy(scrambledMod, 1, temp, 0, 0x80);
        scrambledMod = temp;/*  w  w w  . j a  v  a  2s  .c o  m*/
    }
    // step 1 : 0x4d-0x50 <-> 0x00-0x04
    for (int i = 0; i < 4; i++) {
        byte temp = scrambledMod[i];
        scrambledMod[i] = scrambledMod[0x4d + i];
        scrambledMod[0x4d + i] = temp;
    }
    // step 2 : xor first 0x40 bytes with  last 0x40 bytes
    for (int i = 0; i < 0x40; i++) {
        scrambledMod[i] ^= scrambledMod[0x40 + i];
    }
    // step 3 : xor bytes 0x0d-0x10 with bytes 0x34-0x38
    for (int i = 0; i < 4; i++) {
        scrambledMod[0x0d + i] ^= scrambledMod[0x34 + i];
    }
    // step 4 : xor last 0x40 bytes with  first 0x40 bytes
    for (int i = 0; i < 0x40; i++) {
        scrambledMod[0x40 + i] ^= scrambledMod[i];
    }

    return scrambledMod;
}