微慑信息网

[JAVA]16进制数据与class文件的相互转换测试

16进制数据与class文件的相互转换

mainc.java


import java.io.*;

public class mainc {
    public static void main(String[] args) {
        //以下将16进制转换为class文件

        String mstr="CAFEBABE0000003100680A0013001A08001B0A001C001D0A001C001E0A001F00200A002100220A001F00230A001F002409000C00250700260A001F00270700280A000C002907002A07002B0A000E002C0A000F002D07002E07002F0100063C696E69743E010003282956010004436F646501000161010028285B4C6A6176612F6C616E672F4F626A6563743B4C6A6176612F6C616E672F537472696E673B295A01000A457863657074696F6E730C001400150100086031696439682E790700300C003100320C003300340700350C003600370700380C0039003A0C003B003C0C003D003E0C003F00400100106A6176612F6C616E672F4F626A6563740C004100420100116A6176612F6C616E672F426F6F6C65616E0C004300440100136A6176612F6C616E672F5468726F7761626C650100136A6176612F6C616E672F457863657074696F6E0C004500460C00140047010008627572702F7178630100156A6176612F6C616E672F436C6173734C6F6164657201000F6A6176612F6C616E672F436C617373010007666F724E616D65010025284C6A6176612F6C616E672F537472696E673B294C6A6176612F6C616E672F436C6173733B0100126765744465636C617265644D6574686F647301001D28295B4C6A6176612F6C616E672F7265666C6563742F4D6574686F643B0100186A6176612F6C616E672F7265666C6563742F4D6574686F6401000C6765744D6F6469666965727301000328294901001A6A6176612F6C616E672F7265666C6563742F4D6F64696669657201000869735374617469630100042849295A010011676574506172616D65746572547970657301001428295B4C6A6176612F6C616E672F436C6173733B01000D67657452657475726E5479706501001328294C6A6176612F6C616E672F436C6173733B010004545950450100114C6A6176612F6C616E672F436C6173733B010006696E766F6B65010039284C6A6176612F6C616E672F4F626A6563743B5B4C6A6176612F6C616E672F4F626A6563743B294C6A6176612F6C616E672F4F626A6563743B01000C626F6F6C65616E56616C756501000328295A01000A6765744D65737361676501001428294C6A6176612F6C616E672F537472696E673B010015284C6A6176612F6C616E672F537472696E673B29560100085A4B4D352E332E3307004A010008627572702F6D7A630C004C004D0100016C01000149090049004B070050010008627572702F6566630C0052004D0100014B09004F00510100017A0100124C6A6176612F6C616E672F537472696E673B0100083C636C696E69743E0700580100106A6176612F6C616E672F537472696E670700580C005B005C01000B746F43686172417272617901000428295B430A0059005A0700580C00140060010005285B4329560A005E005F0700580C00640046010006696E7465726E0A006200630C00540055090012006600200012001300000001001A0054005500000003000000140015000100160000001100010001000000052AB70001B10000000000080017001800020016000000B00008000800000094B200533607B2004E3606B20067B80003B600044D2CBE3E03360415041DA200632C1504323A05150615079A00539A004E1905B60005B8000615069A004C99003B190515069A000DB60007BE05A0002C1905B6000815069A001BB20009A6001C19050105BD000A59032A5359042B53B6000BC0000CB6000DAC840401150699FF9D0315079AFF99AC4DBB000F592CB60010B70011BF0002000A00770087000E007800860087000E00000019000000040001000E0008005600150001001600000087000800020000007B1202A80009B30067A700724BB6005D59BE5F033C5F5A04A3004E591B5C341B0870AA00000000003200000000000000030000001F00000023000000280000002D05A700141044A7000F101BA7000A1014A7000510178292558401015F5A9A00085C5FA7FFBA5F5A1BA3FFB2BB00575A5FB70061B600655F57A900B1000000000000";
        String filepath="source.class";
        String result = mfunction.ChangeToClass(mstr,filepath);
        System.out.println(result);

        //以下读取class转换为16进制
        /*
        String filepath="qxc.class";
        String result = mfunction.ChangeToVaule(filepath);
        System.out.println(result);
        */
    }
}

 

mfunction.java

import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class mfunction {

    public static String ChangeToVaule(String filename) {
        //将class转换为16进制字符:
        String res = "";
        try {
            FileInputStream flos = new FileInputStream(filename);

            byte[] bytes = new byte[9999];
            int temp;
            while ((temp = flos.read(bytes))  != -1) {
                System.out.println(temp);
                String data = HexBin.encode(bytes);
                data=data.substring(0,temp*2);
                res=data;
            }

        }
        catch (Exception e) {
            System.out.println(e);
            res="ERR";
            }
            return  res;
    }



    public static String ChangeToClass(String classstr, String filepath) {
        //将16进制转换为class文件:
        String res;
        try{
            byte[] data= HexBin.decode(classstr);
            FileOutputStream fos =new FileOutputStream(filepath);
            fos.write(data);
            fos.close();
            //return data;
            res="make ok";
        }
        catch(Exception e){
            System.out.println(e);
            res="make error";
        }
    return res;
    }
}

 

 

 

 

 

拓展阅读(点评/知识):

 while ((temp = flos.read(bytes))  != -1) {
                System.out.println(temp);
                String data = HexBin.encode(bytes);
                data=data.substring(0,temp*2);
                res=data;
            }

以上使用while读取大文件使用较多,当读取小文件时,可使用:

        try {
            FileInputStream flos = new FileInputStream(filename);

            byte[] bytes = new byte[flos.available()];
            flos.read(bytes);
            System.out.println(HexBin.encode(bytes));


        }

 

本文标题:[JAVA]16进制数据与class文件的相互转换测试
本文链接:
(转载请附上本文链接)
http://vulsee.com/archives/vulsee_2020/0107_9794.html
转载请附本站链接,未经允许不得转载,,谢谢:微慑信息网-VulSee.com » [JAVA]16进制数据与class文件的相互转换测试
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

微慑信息网 专注工匠精神

访问我们联系我们