packagecom.encode.sm4;importjava.math.BigInteger;public classUtil
{/*** 整形转换成网络传输的字节流(字节数组)型数据
*
*@paramnum 一个整型数据
*@return4个字节的自己数组*/
public static byte[] intToBytes(intnum)
{byte[] bytes = new byte[4];
bytes[0] = (byte) (0xff & (num >> 0));
bytes[1] = (byte) (0xff & (num >> 8));
bytes[2] = (byte) (0xff & (num >> 16));
bytes[3] = (byte) (0xff & (num >> 24));returnbytes;
}/*** 四个字节的字节数据转换成一个整形数据
*
*@parambytes 4个字节的字节数组
*@return一个整型数据*/
public static int byteToInt(byte[] bytes)
{int num = 0;inttemp;
temp= (0x000000ff & (bytes[0])) << 0;
num= num |temp;
temp= (0x000000ff & (bytes[1])) << 8;
num= num |temp;
temp= (0x000000ff & (bytes[2])) << 16;
num= num |temp;
temp= (0x000000ff & (bytes[3])) << 24;
num= num |temp;returnnum;
}/*** 长整形转换成网络传输的字节流(字节数组)型数据
*
*@paramnum 一个长整型数据
*@return4个字节的自己数组*/
public static byte[] longToBytes(longnum)
{byte[] bytes = new byte[8];for (int i = 0; i < 8; i++)
{
bytes[i]= (byte) (0xff & (num >> (i * 8)));
}returnbytes;
}/*** 大数字转换字节流(字节数组)型数据
*
*@paramn
*@return
*/
public static byte[] byteConvert32Bytes(BigInteger n)
{byte tmpd[] = (byte[])null;if(n == null)
{return null;
}if(n.toByteArray().length == 33)
{
tmpd= new byte[32];
System.arraycopy(n.toByteArray(),1, tmpd, 0, 32);
}else if(n.toByteArray().length == 32)
{
tmpd=n.toByteArray();
}else{
tmpd= new byte[32];for(int i = 0; i < 32 - n.toByteArray().length; i++)
{
tmpd[i]= 0;
}
System.arraycopy(n.toByteArray(),0, tmpd, 32 -n.toByteArray().length, n.toByteArray().length);
}returntmpd;
}/*** 换字节流(字节数组)型数据转大数字
*
*@paramb
*@return
*/
public static BigInteger byteConvertInteger(byte[] b)
{if (b[0] < 0)
{byte[] temp = new byte[b.length + 1];
temp[0] = 0;
System.arraycopy(b,0, temp, 1, b.length);return newBigInteger(temp);
}return newBigInteger(b);
}/*** 根据字节数组获得值(十六进制数字)
*
*@parambytes
*@return
*/
public static String getHexString(byte[] bytes)
{return getHexString(bytes, true);
}/*** 根据字节数组获得值(十六进制数字)
*
*@parambytes
*@paramupperCase
*@return
*/
public static String getHexString(byte[] bytes, booleanupperCase)
{
String ret= "";for (int i = 0; i < bytes.length; i++)
{
ret+= Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1);
}return upperCase ?ret.toUpperCase