这里是Python和爬虫的世界 Spide|

app逆向java转python代码

djy.java

public class djy {
    public static void main(String[] args) {
        C0445 c0445 = new C0445();
        String str1 = "9C0E7BB121F0";
        String str2 = "kele";
        String result = c0445.m2291RC4(str1, str2);
        System.out.println(result);
    }
}

C0445.java

public class C0445 {

    public static String m2291RC4(String str, String str2) {
        String str3 = "";
        if (!(str == null || str2 == null)) {
            try {
                byte[] hexString2Bytes = HexString2Bytes(str);
                byte[] rc4base=RC4Base(hexString2Bytes, str2);
                return new String(rc4base, "GBK");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return str3;
    }

    public static byte[] HexString2Bytes(String str) {
        try {
            int length = str.length();
            byte[] bArr = new byte[(length / 2)];
            byte[] bytes = str.getBytes("GBK");
            for (int i = 0; i < length / 2; i++) {
                int i2 = i * 2;
                bArr[i] = uniteBytes(bytes[i2], bytes[i2 + 1]);
            }
            return bArr;
        } catch (Exception e) {
            e.printStackTrace();
            return new byte[0];
        }
    }

    private static byte uniteBytes(byte b, byte b2) {
        StringBuilder sb = new StringBuilder();
        String str = "0x";
        sb.append(str);
        sb.append(new String(new byte[]{b}));
        char byteValue = (char) ((Byte.decode(sb.toString()).byteValue()) << 4);
        StringBuilder sb2 = new StringBuilder();
        sb2.append(str);
        sb2.append(new String(new byte[]{b2}));
        return (byte) (byteValue ^ (Byte.decode(sb2.toString()).byteValue()));
    }

    private static byte[] initKey(String str) {
        try {
            byte[] bytes = str.getBytes("GBK");
            byte[] bArr = new byte[256];
            for (int i = 0; i < 256; i++) {
                bArr[i] = (byte) i;
            }
            if (bytes != null) {
                if (bytes.length != 0) {
                    int i2 = 0;
                    int i3 = 0;
                    for (int i4 = 0; i4 < 256; i4++) {
                        i3 = ((bytes[i2] & 255) + (bArr[i4] & 255) + i3) & 255;
                        byte b = bArr[i4];
                        bArr[i4] = bArr[i3];
                        bArr[i3] = b;
                        i2 = (i2 + 1) % bytes.length;
                    }
                    return bArr;
                }
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static byte[] RC4Base(byte[] bArr, String str) {
        byte[] initKey = initKey(str);
        byte[] bArr2 = new byte[bArr.length];
        int i = 0;
        int b = 0;
        for (int i2 = 0; i2 < bArr.length; i2++) {
            i = (i + 1) & 255;
            b = (((initKey[i] & 255) + b) & 255);
            byte b2 = initKey[i];
            initKey[i] = initKey[b];
            initKey[b] = b2;
            int i3 = ((initKey[i] & 255) + (initKey[b] & 255)) & 255;
            byte i3i2=  (byte) (initKey[i3] ^ bArr[i2]);
            bArr2[i2] =i3i2;
        }
        return bArr2;
    }
}

转为python之后的代码

# -*- coding: utf-8 -*-
def pb2jb(byte_arr):
    """
    python字节码转java字节码
    :param byte_arr:
    :return:
    """
    return [int(i) - 256 if int(i) > 127 else int(i) for i in byte_arr]


def uniteBytes(b, b2):
    sb = int(chr(b), 16)
    byteValue = sb << 4
    sb2 = int(chr(b2), 16)
    return byteValue ^ sb2


def strToBytes(s):
    _bytes = bytearray()
    _bytes.extend(s)
    _bytes = pb2jb(_bytes)
    return _bytes


def InitKey(str1):
    _bytes = strToBytes(str1)
    bArr = bytearray(256)
    for i in range(0, 256):
        bArr[i] = i
    i2 = 0
    i3 = 0
    for i4 in range(0, 256):
        i3 = ((_bytes[i2] & 255) + (bArr[i4] & 255) + i3) & 255
        b = bArr[i4]
        bArr[i4] = bArr[i3]
        bArr[i3] = b
        i2 = (i2 + 1) % len(_bytes)
    return pb2jb(bArr)


def jb2pd(byte_arr):
    """java 字节码转python 字节码"""
    return [int(i) + 256 if int(i) < 0 else int(i) for i in byte_arr]


def jb2str(byte_arr, encoding="gbk"):
    """
    java字节数组转string
    """
    s = ""
    for i in jb2pd(byte_arr):
        s += chr(i)
    return s.decode(encoding)


def RC4Base(bArr, str2):
    initKey = InitKey(str2)
    bArr2 = [0 for _ in range(len(bArr))]
    i = 0
    b = 0
    for i2, value in enumerate(bArr):
        i = (i + 1) & 255
        b = (((initKey[i] & 255) + b) & 255)
        b2 = initKey[i]
        initKey[i] = initKey[b]
        initKey[b] = b2
        i3 = ((initKey[i] & 255) + (initKey[b] & 255)) & 255
        ib = initKey[i3] ^ value
        bArr2[i2] = ib
    return pb2jb(bArr2)


def HexString2Bytes(_str):
    length = len(_str)
    bArr = bytearray(int(length / 2))
    _bytes = strToBytes(_str)
    for i in range(0, length / 2):
        i2 = i * 2
        bArr[i] = uniteBytes(_bytes[i2], _bytes[i2 + 1])
    return pb2jb(bArr)


class RC4helper(object):
    def decode(self, unicode_str1, str2="kele"):
        str1 = str(unicode_str1)
        hexString2Bytes = HexString2Bytes(str1)
        rc4base = RC4Base(hexString2Bytes, str2)
        return jb2str(rc4base)

本文作者:一起来学python

本文链接:https://www.cnblogs.com/c-x-a/p/15274809.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   公众号python学习开发  阅读(213)  评论(0编辑  收藏  举报
历史上的今天:
2019-09-15 C语言中的指针笔记
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起