RC4加密解密算法工具类-Java语言实现

摘要 RC4加密解密算法是工具类是大名鼎鼎的 RSA三人组中的头号人物Ron Rivest设计的,可以有效抵御暴力搜索密钥的攻击。鉴于此,提供一个由Java语言实现的工具类。

前言

  RC4加密算法是大名鼎鼎的 RSA三人组中的头号人物Ron Rivest在1987年设计的密钥长度可变的流加密算法簇。之所以称其为簇,是因为其核心部分的S-box长度可为任意长度,但一般为256字节。RC4算法是一种对称加密算法,所谓对称加密,就是加密和解密的过程是一样的。

  RC4算法的特点是算法简单,执行速度快。并且密钥长度是可变的,可变范围为1-256字节(8-2048比特),在现在技术支持的前提下,当密钥长度为128bit时,用暴力法搜索密钥已经不太可行,所以能够预见RC4的密钥范围仍然能够在今后相当长的时间里抵御暴力搜索密钥的攻击。实际上,现在也没有找到对于128bit密钥长度的RC4加密算法的有效攻击方法。

算法实现


package com.eg.wiener.utils;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

/**
 * RC4加解密算法工具类
 *
 * @author Wiener
 * @date 2020/11/17
 */
public class RC4Util {
    private static String CHARSET = "UTF-8";

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

    /**
     * 解密
     * @param data
     * @param key
     * @return
     */
    public static String decry_RC4(String data, String key) {
        if (data == null || key == null) {
            return null;
        }
        return new String(RC4Base(HexString2Bytes(data), key));
    }

    /**
     * 加密,返回字节流
     * @param data
     * @param key
     * @return
     */
    public static byte[] encry_RC4_byte(String data, String key) {
        if (data == null || key == null) {
            return null;
        }
        byte b_data[] = data.getBytes();
        return RC4Base(b_data, key);
    }

    /**
     * 加密,返回密文
     * @param data 明文
     * @param key 密钥
     * @return 密文
     */
    public static String encry_RC4_string(String data, String key) {
        if (data == null || key == null) {
            return null;
        }
        return toHexString(asString(encry_RC4_byte(data, key)));
    }

    private static String asString(byte[] buf) {
        StringBuffer strbuf = new StringBuffer(buf.length);
        for (int i = 0; i < buf.length; i++) {
            strbuf.append((char) buf[i]);
        }
        return strbuf.toString();
    }

    private static byte[] initKey(String aKey) {
        byte[] b_key = null;
        try {
            b_key = aKey.getBytes(CHARSET);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        byte state[] = new byte[256];

        for (int i = 0; i < 256; i++) {
            state[i] = (byte) i;
        }
        int index1 = 0;
        int index2 = 0;
        if (b_key == null || b_key.length == 0) {
            return null;
        }
        for (int i = 0; i < 256; i++) {
            index2 = ((b_key[index1] & 0xff) + (state[i] & 0xff) + index2) & 0xff;
            byte tmp = state[i];
            state[i] = state[index2];
            state[index2] = tmp;
            index1 = (index1 + 1) % b_key.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;// 0x表示十六进制
    }

    private static byte[] HexString2Bytes(String src) {
        int size = src.length();
        byte[] ret = new byte[size / 2];
        byte[] tmp = src.getBytes();
        for (int i = 0; i < size / 2; i++) {
            ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
        }
        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();
        byte ret = (byte) (_b0 ^ _b1);
        return ret;
    }

    private static byte[] RC4Base(byte[] input, String mKkey) {
        int x = 0;
        int y = 0;
        byte key[] = initKey(mKkey);
        int xorIndex;
        byte[] result = new byte[input.length];

        for (int i = 0; i < input.length; i++) {
            x = (x + 1) & 0xff;
            y = ((key[x] & 0xff) + y) & 0xff;
            byte tmp = key[x];
            key[x] = key[y];
            key[y] = tmp;
            xorIndex = ((key[x] & 0xff) + (key[y] & 0xff)) & 0xff;
            result[i] = (byte) (input[i] ^ key[xorIndex]);
        }
        return result;
    }

    public static void main(String[] args) {
        String key = "myKey";
        String encryStr = encry_RC4_string(String.format("%03d", 12333201), key);
        System.out.println(encryStr);
        System.out.println(decry_RC4(encryStr, key));
    }

    /**
     * 生成6位密文
     * @param data 明文,可以为用户id等
     * @param key 密钥
     * @return
     */
    public static String m4Rc4Encrypt(int data, String key) {
        if (null == key) {
            key = UUID.randomUUID().toString();
        }
        return encry_RC4_string(String.format("%03d", data), key);
//        String.format("%03d", i) 主要实现位数不够前面补零。如果一个数字不超过3位,则会在其前面补零以到达规定的3位数,其中0被填充到缺省位,3代表规定位数  d代表是整型。
    }
}

结束语

  欢迎点赞阅读,一同学习交流RC4加密解密算法;若有疑问,请在文章下方留下你的神评妙论!


  读后有收获,小礼物走一走,请作者喝咖啡。

赞赏支持

原文地址:https://www.cnblogs.com/east7/p/14423212.html