sm4 前后端 加密_国密SM4,javaScript加密 java解密

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值