wangy 发表于 2021-6-30 22:21:04

【Java】java实现RC4加解密

RC4属于对称可逆加密(就是加密解密的密钥一样)
使用方法:
try
                {
                        String encryStr = RC4Util.encryRC4String("测试", "123456", "UTF-8");
        System.out.println("加密后得到得字符串:" + encryStr);
                }
                catch (UnsupportedEncodingException e)
                {}
               
                String decryStr = RC4Util.decryRC4(encryStr, "123456");
                System.out.println("解密后得到得字符串:"+decryStr);
               
                工具类:
class RC4Util {
        public static String encryRC4String(String data, String key, String chartSet) throws UnsupportedEncodingException {
                if (data == null || key == null) {
                        return null;
                }
                return toHexString(asString(encryRC4Byte(data, key, chartSet)));
        }

        public static byte[] encryRC4Byte(String data, String key, String chartSet) throws UnsupportedEncodingException {
                if (data == null || key == null) {
                        return null;
                }
                if (chartSet == null || chartSet.isEmpty()) {
                        byte bData[] = data.getBytes();
                        return RC4Base(bData, key);
                } else {
                        byte bData[] = data.getBytes(chartSet);
                        return RC4Base(bData, key);
                }

        }

        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);
        }

        public static String decryRC4(byte[] data, String key) {
                if (data == null || key == null) {
                        return null;
                }
                return asString(RC4Base(data, key));
        }

        private static String asString(byte[] buf) {
                StringBuffer strbuf = new StringBuffer(buf.length);
                for (byte b : buf) {
                        strbuf.append((char) b);
                }
                return strbuf.toString();
        }

        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 String toHexString(String s) {
                String str = "";
                for (int i = 0; i < s.length(); i++) {
                        int ch = (int) s.charAt(i);
                        String s4 = Integer.toHexString(ch & 0xFF);
                        if (s4.length() == 1) {
                                s4 = '0' + s4;
                        }
                        str = str + s4;
                }
                return str;
        }

        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】java实现RC4加解密