Java tutorial
//package com.java2s; import java.math.BigInteger; public class Main { private static byte[] getPadded(BigInteger n, int length) { byte[] bs = n.toByteArray(); if (bs.length < length) { byte[] tmp = new byte[length]; System.arraycopy(bs, 0, tmp, length - bs.length, bs.length); bs = tmp; } else if (bs.length > length) { //BigInteger bigger (probably due to 0 padding) byte[] tmp = new byte[length]; System.arraycopy(bs, bs.length - length, tmp, 0, length); bs = tmp; } return bs; } }