Here you can find the source of writeBigInteger(ByteBuffer bb, BigInteger bigInteger, int length)
private static void writeBigInteger(ByteBuffer bb, BigInteger bigInteger, int length) throws IOException
//package com.java2s; /*// ww w .j a v a 2 s . c om * Copyright 2004 - 2013 Wayne Grant * 2013 - 2018 Kai Kramer * * This file is part of KeyStore Explorer. * * KeyStore Explorer is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * KeyStore Explorer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with KeyStore Explorer. If not, see <http://www.gnu.org/licenses/>. */ import java.io.IOException; import java.math.BigInteger; import java.nio.ByteBuffer; public class Main { private static void writeBigInteger(ByteBuffer bb, BigInteger bigInteger, int length) throws IOException { // Get big-endian two's compliment representation of big integer byte[] bigInt = bigInteger.toByteArray(); // Remove leading zero bytes int skipZeroPos = 0; for (int i = 0; i < bigInt.length; i++) { if (bigInt[i] != 0) { break; } skipZeroPos++; } byte[] tmp = new byte[bigInt.length - skipZeroPos]; System.arraycopy(bigInt, skipZeroPos, tmp, 0, tmp.length); bigInt = tmp; // Convert to little-endian reverseBytes(bigInt); // Pad out byte array with zeros int padByteLength = length - bigInt.length; if (padByteLength > 0) { tmp = new byte[length]; System.arraycopy(bigInt, 0, tmp, 0, bigInt.length); bigInt = tmp; } bb.put(bigInt); } private static void reverseBytes(byte[] bytes) { int halfWay = bytes.length / 2; for (int i = 0; i < halfWay; i++) { byte b = bytes[i]; bytes[i] = bytes[bytes.length - 1 - i]; bytes[bytes.length - 1 - i] = b; } } }