wangy 发表于 2021-6-30 22:18:45

【Java】元宵节

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main
{
        public static void main(String[] args)
        {
                Main t6=new Main();
                String htmls= t6.getPageSource("http://bmob-cdn-11629.b0.upaiyun.com/2019/02/19/1483728040e51b2c80b9ddb4ad3c6ced.txt","utf8");
                        String decryStr = RC4Util.decryRC4(htmls, "happy");
                System.out.println(decryStr);
                        }
        public String getPageSource(String pageUrl,String encoding) {   
                StringBuffer sb = new StringBuffer();   
                try {   
                        URL url = new URL(pageUrl);   
                        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), encoding));   
                        String line;   
                        while((line = in.readLine())!=null){
                                sb.append(line);
                        }in.close();}
                catch (Exception ex) {   
                        System.err.println(ex);   
                }   
                return sb.toString();   
        }   
}
class RC4Util {
        public static String decryRC4(String data, String key) {
                if (data == null || key == null) {
                        return null;
                }
                return new String(RC4Base(HexString2Bytes(data), key), StandardCharsets.UTF_8);
        }
        private static byte[] initKey(String aKey) {
                byte[] bkey = aKey.getBytes();
                byte state[] = new byte;

                for (int i = 0; i < 256; i++) {
                        state = (byte) i;
                }
                int index1 = 0;
                int index2 = 0;
                if (bkey.length == 0) {
                        return null;
                }
                for (int i = 0; i < 256; i++) {
                        index2 = ((bkey & 0xff) + (state & 0xff) + index2) & 0xff;
                        byte tmp = state;
                        state = state;
                        state = tmp;
                        index1 = (index1 + 1) % bkey.length;
                }
                return state;
        }
        private static byte[] HexString2Bytes(String src) {
                int size = src.length();
                byte[] ret = new byte;
                byte[] tmp = src.getBytes(StandardCharsets.UTF_8);
                for (int i = 0; i < size / 2; i++) {
                        ret = uniteBytes(tmp, tmp);
                }
                return ret;
        }
        private static byte uniteBytes(byte src0, byte src1) {
                char _b0 = (char) Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
                _b0 = (char) (_b0 << 4);
                char _b1 = (char) Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
                return (byte) (_b0 ^ _b1);
        }
        private static byte[] RC4Base(byte[] input, String mKkey) {
                int x = 0;
                int y = 0;
                byte key[] = initKey(mKkey);
                int xorIndex;
                byte[] result = new byte;
                for (int i = 0; i < input.length; i++) {
                        x = (x + 1) & 0xff;
                        y = ((key & 0xff) + y) & 0xff;
                        byte tmp = key;
                        key = key;
                        key = tmp;
                        xorIndex = ((key & 0xff) + (key & 0xff)) & 0xff;
                        result = (byte) (input ^ key);
                }
                return result;
        }
}
页: [1]
查看完整版本: 【Java】元宵节