RC4加密解密算法

/**
 * RC4加密解密工具类
 * 
 * @author hesongjun
 * 
 */
public class RC4Util {
    /**
     * 解密文件中的内容
     * 
     * @param filePath 需要解密的文件路径
     * @param key 解密的key
     * 
     * @return 解密后的内容
     * */
    public static String decodeStrFromFile(String filePath, String key) {
        String encodeContent = readFile(filePath);
        String decodeContent = rc4(encodeContent,key);
        return decodeContent;
    }
    
    /**
     * 解密内容
     * 
     * @param content 需要解密的内容
     * @param key 解密的key,为数字或者字母
     * 
     * @return 解密后的内容
     * */
    public static String decodeStr(String content, String key) {
        String decodeContent = rc4(content,key);
        return decodeContent;
    }

    /**
     * 加密内容至文件
     * 
     * @param content 需要加密的内容
     * @param filePath 内容加密后保存的文件路径
     * @param key 加密的key
     * 
     * @return 是否加密成功
     * */
    public static boolean encodeStrToFile(String content, String filePath, String key) {
        String encodeContent = rc4(content,key);
        boolean result =saveFile(encodeContent, filePath);
        return result;
    }
    
    /**
     * 加密内容
     * 
     * @param content 需要加密的内容
     * @param key 加密的key
     * 
     * @return 加密后的内容
     * */
    public static String encodeStr(String content, String key) {
        String encodeContent = rc4(content,key);
        return encodeContent;
    }

    //加密解密都用同一个方法
    private static String rc4(String aInput,String key) {
        int[] iS = new int[256];
        byte[] iK = new byte[256];

        for (int i = 0; i < 256; i++){          
            iS[i] = i;
        }
        int j = 1;
        for (short i = 0; i < 256; i++) {
            iK[i] = (byte) key.charAt((i % key.length()));
        }
        j = 0;
        for (int i = 0; i < 255; i++) {
            j = (j + iS[i] + iK[i]) % 256;
            int temp = iS[i];
            iS[i] = iS[j];
            iS[j] = temp;
        }
        int i = 0;
        j = 0;
        char[] iInputChar = aInput.toCharArray();
        char[] iOutputChar = new char[iInputChar.length];
        for (short x = 0; x < iInputChar.length; x++) {
            i = (i + 1) % 256;
            j = (j + iS[i]) % 256;
            int temp = iS[i];
            iS[i] = iS[j];
            iS[j] = temp;
            int t = (iS[i] + (iS[j] % 256)) % 256;
            int iY = iS[t];
            char iCY = (char) iY;
            iOutputChar[x] = (char) (iInputChar[x] ^ iCY);
        }
        return new String(iOutputChar);
    }

    /** 读取 */
    private static String readFile(String filePath) {
        String res = "";
        if (!new File(filePath).exists()) {
            return res;
        }
        String resStr = "";
        try {
            byte buf[] = new byte[1024];
            FileInputStream input = new FileInputStream(filePath);
            int count = input.read(buf);
            input.close();
            return new String(buf, 0, count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resStr;
    }

    /** 写入 */
    private static boolean saveFile(String content, String filePath) {
        try {
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            if (!file.getParentFile().exists()) {
                return false;
            }
            if (!new File(filePath).exists()) {
                file.createNewFile();
            }
            FileOutputStream outStream = new FileOutputStream(file);
            String utf8Str = new String(content.getBytes("UTF-8"), "UTF-8");
            outStream.write(utf8Str.getBytes());
            outStream.flush();
            outStream.getFD().sync();
            outStream.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容