123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
-
- 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<b.length;i++) {
- sb.append(String.format("%02x", b[i]));
- }
- return sb.toString();
- }
-
-
- /**
- * 编码
- * @param encoderStr
- * @return
- * @throws UnsupportedEncodingException
- */
- public static String encoderString(String base64Text) throws UnsupportedEncodingException{
- String result = "";
- if(base64Text != null && !base64Text.isEmpty() && !base64Text.equals("")){
- byte[] textByte = base64Text.getBytes("UTF-8");
- result = encoder.encodeToString(textByte);
- }
- return result;
- }
-
- /**
- * 编码
- * @param encoderStr
- * @return
- * @throws UnsupportedEncodingException
- */
- public static String encoderString(byte[] textByte) throws UnsupportedEncodingException{
- return encoder.encodeToString(textByte);
- }
-
- public static int getLength(String s) {
- int length = 0;
- for (int i = 0; i < s.length(); i++) {
- int ascii = Character.codePointAt(s, i);
- if (ascii >= 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<String> base64TeaByteOldList = new ArrayList<String>();
- for(int i=0;i<base64TextEn.length();i++){
- if(i%2 == 0){
- base64TeaByteOldList.add("0x"+base64TextEn.charAt(i)+base64TextEn.charAt(i+1));
- }
- }
- //解码后转换得到的list
- System.out.println("解码后转换得到的list:");
- for(String byte16:base64TeaByteOldList){
- System.out.print(byte16+" ");
- }
- System.out.println();*/
-
- //解码数据转换为byte数组
- byte[] base64TeaByteOld = hexStrToByteArray(base64TextEn);
- System.out.println( "解码后得到的tea加密的byte数组:");
- for(byte i : base64TeaByteOld)
- System.out.print(i + " ");
- System.out.println();
-
- byte[] base64TeaByte =TeaUtils.decrypt(base64TeaByteOld,null);
-
- System.out.println( "tea解密:");
- for(byte i : base64TeaByte)
- System.out.print(i + " ");
- System.out.println();
- }
- }
|