【APP逆向07】字符串与字节的转换

  • 1.逆向的时候,很多时候字符串都是通过字节来表示的
import java.util.Arrays;

public class Hello {

    public static void main(String[] args) {
        // 1.字节数组(转换为字符串) [字节,字节,字节]
        byte[] dataList = {97, 105, 100, 61, 50, 52, 54, 51, 56, 55, 53, 55, 49, 38, 97, 117, 116, 111, 95, 112, 108, 97, 121, 61, 48, 38, 99, 105, 100, 61, 50, 56, 57, 48, 48, 56, 52, 52, 49, 38, 100, 105, 100, 61, 75, 82, 69, 104, 69, 83, 77, 85, 74, 104, 56, 116, 70, 67, 69, 86, 97, 82, 86, 112, 69, 50, 116, 97, 80, 81, 107, 55, 87, 67, 104, 67, 74, 103, 38, 101, 112, 105, 100, 61, 48, 38, 102, 116, 105, 109, 101, 61, 49, 54, 50, 55, 49, 48, 48, 57, 51, 55, 38, 108, 118, 61, 48, 38, 109, 105, 100, 61, 48, 38, 112, 97, 114, 116, 61, 49, 38, 115, 105, 100, 61, 48, 38, 115, 116, 105, 109, 101, 61, 49, 54, 50, 55, 49, 48, 52, 51, 55, 50, 38, 115, 117, 98, 95, 116, 121, 112, 101, 61, 48, 38, 116, 121, 112, 101, 61, 51};
        
        String dataString = new String(dataList);
        System.out.println("字符串是:" + dataString);

        // 2.字符串->字节数组
        try {
            // Python中的  name.encode("gbk")
            String name = "周杰伦hbg";
            byte[] v1 = name.getBytes("GBK");
            System.out.println(Arrays.toString(v1)); // [-50, -28, -59, -26, -58, -21]
            
            // Python中的  name.encode("utf-8")
            byte[] v2 = name.getBytes("UTF-8");
            System.out.println(Arrays.toString(v2)); // [-26, -83, -90, -26, -78, -101, -23, -67, -112]
        } catch (Exception e) {

        }
    }
}
  • 2.Java中字符串的定义与常见方法
    • 2.1:定义
import java.io.UnsupportedEncodingException;

public class Hello {

    public static void main(String[] args) throws UnsupportedEncodingException {
        String v1 = "周杰伦hbg";
        String v2 = new String("周杰伦hbg");

        String v4 = new String(new byte[]{-26, -83, -90, -26, -78, -101, -23, -67, -112});
        String v5 = new String(new byte[]{-50, -28, -59, -26, -58, -21}, "GBK");
        
        String v6 = new String(  new char[]{'周', '杰', '伦'}   ) ;
    }
}
  • 2.2:常见方法
public class Hello {

    public static void main(String[] args) {
        String origin = "xwwdf是个大DB";

        char v1 = origin.charAt(5); // 指定字符
        int len = origin.length();  // 长度
        for (int i = 0; i < len; i++) {
            char item = origin.charAt(i);
        }

        String v2 = origin.trim(); // 去除空白
        String v3 = origin.toLowerCase(); // 小写
        String v4 = origin.toUpperCase(); // 大写
        String[] v5 = origin.split("是"); // 分割
        String v6 = origin.replace("D", "S"); // 替换
        String v7 = origin.substring(2, 6);  // 子字符串=切片 [2:6]
        boolean v8 = origin.equals("alex是个大SB"); // 
        
        boolean v9 = origin.contains("el");
        boolean v10 = origin.startsWith("a");
        String v11 = origin.concat("哈哈哈");
    }
}
  • 3.某个app逆向,在Java代码中得到一个字节数组 [-50,-28,-59,-26,-58,-21],请通过Python代码将这个字节数组转换成字符串?
    • 在Java中的字节范围:-128127;Python中字节的范围:0255
    • Java 0 1 2 3 4 .. 127 -128 -127 -126 -3 -2 -1
    • Python 0 1 2 3 4 .. 127 128 129 130 ...253 254 255
data_list = [-26, -83, -90, -26, -78, -101, -23, -67, -112]

num_list = bytearray()
for i in data_list:
    if i < 0:
        i = i + 256
    num_list.append(i)

text = num_list.decode('utf-8')
print(text)
  • 4.字符串拼接
import java.io.UnsupportedEncodingException;

public class Hello {

    public static void main(String[] args) {
        // "name=xwl&age=18"
        StringBuilder sb = new StringBuilder();  // StringBuffer线程安全
        sb.append("name");
        sb.append("=");
        sb.append("xwl");
        sb.append("&");
        sb.append("age");
        sb.append("=");
        sb.append("18");
        
        String dataString = sb.toString();
        
        System.out.println(dataString);
    }
}
  • 5.逆向小案例截图
posted @ 2024-01-17 22:26  Tony_xiao  阅读(52)  评论(0编辑  收藏  举报