package com.inet.ailink.receiver.common.utils; import java.io.UnsupportedEncodingException; import java.util.Base64; //import com.google.common.primitives.Bytes; public class Base64Util { final static Base64.Decoder decoder = Base64.getDecoder(); final static Base64.Encoder encoder = Base64.getEncoder(); public Base64Util(){} /** * 解码 * @param encoderStr * @return * @throws UnsupportedEncodingException */ public static String decoderString(String base64Text) throws UnsupportedEncodingException{ String result = ""; if(base64Text != null && !base64Text.isEmpty() && !base64Text.equals("")){ byte[] textByte = decoder.decode(base64Text); result = bytes_String16(textByte); } return result; } /** * byte[]转16进制字符串 * @param b * @return */ public static String bytes_String16(byte[] b) { StringBuilder sb = new StringBuilder(); for(int i=0;i= 0 && ascii <= 255) { length++; } else { length += 2; } } return length; } private static String intToHex(int n) { StringBuffer s = new StringBuffer(); String a; char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; while(n != 0){ s = s.append(b[n%16]); n = n/16; } a = s.reverse().toString(); return a; } public static byte[] hexStrToByteArray(String str) { if (str == null) { return null; } if (str.length() == 0) { return new byte[0]; } byte[] byteArray = new byte[str.length() / 2]; for (int i = 0; i < byteArray.length; i++){ String subStr = str.substring(2 * i, 2 * i + 2); byteArray[i] = ((byte)Integer.parseInt(subStr, 16)); } return byteArray; } public static void main(String args[]) throws UnsupportedEncodingException { //01 01 16 b7 28 32 18 4a 88 00 0e 00 00 00 00 42 4d 10 01 0a 00 13 05 07 e9 byte[] old =new byte[]{(byte)0x01 ,(byte)0x01 ,(byte)0x16 ,(byte)0xb7 ,(byte)0x28 ,(byte)0x32 ,(byte)0x18 ,(byte)0x4a ,(byte)0x88 ,(byte)0x00 ,(byte)0x0e ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x42 ,(byte)0x4d ,(byte)0x10 ,(byte)0x01 ,(byte)0x0a ,(byte)0x00 ,(byte)0x13 ,(byte)0x05 ,(byte)0x07 ,(byte)0xe9}; //原始数据 System.out.println( "原始数据:"); for(byte i : old) System.out.print(i + " "); System.out.println(); //tea加密 byte[] teaDscrypt =TeaUtils.encrypt(old,null); System.out.println( "tea加密:"); for(byte i : teaDscrypt) System.out.print(i + " "); System.out.println(); //base编码 String base64Text = encoderString(teaDscrypt); System.out.println("base64编码后的数据长度:"+getLength(base64Text)+":"+base64Text); //base解码 String base64TextEn= decoderString(base64Text); System.out.println("base64解码后的数据长度:"+getLength(base64TextEn)+":"+base64TextEn); /*//转称byte数组(每两位一个16进制byte) List base64TeaByteOldList = new ArrayList(); for(int i=0;i