文档管理系统基于加密算法对文件进行加密
2021-09-19

        企业文档管理系统的文档数据统一存储在服务端,如果对服务器的安全存储要求高,一般会对统一管理的文档采用一定的加密算法进行加密,基于加密算法对文件进行加密以保证文件在服务器的安全存储,考虑到多种加密算法的安全程度及计算性能,目前常用的加密算法为RC4等对称性算法。RC4算法的Java实现方法如下:

       /**

     * 二进制流解密

     * @param data

     * @param key 密钥

     * @return

     */

    public static String decryRC4(byte[] data, String key) {

        if (data == null || StringUtils.isBlank(key)) {

            return null;

        }

        return asString(RC4Base(data, key));

    }


    /**

     * 二进制流转二进制并且返回二进制

     * @param data

     * @param key

     * @return

     */

    public static byte[] decryRC4ToByte(byte[] data,String key){

        if (data == null || StringUtils.isBlank(key)) {

            return null;

        }

        return RC4Base(data,key);

    }


    /**

     * 字符串解密

     * @param data

     * @param key 密钥

     * @return

     */

    public static String decryRC4(String data, String key) {

        if (data == null || StringUtils.isBlank(key)) {

            return null;

        }

        return new String(RC4Base(hexString2Bytes(data), key));

    }


    /**

     * 字符串加密,返回二进制流

     * @param data

     * @param key

     * @return

     */

    public static byte[] encryRC4Byte(String data, String key) {

        if (data == null || StringUtils.isBlank(key)) {

            return null;

        }

        byte b_data[] = data.getBytes();

        return RC4Base(b_data, key);

    }


    /**

     * 字符串加密,返回字符串

     * @param data

     * @param key

     * @return

     */

    public static String encryRC4String(String data, String key) {

        if (data == null || StringUtils.isBlank(key)) {

            return null;

        }

        return toHexString(asString(encryRC4Byte(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 = aKey.getBytes();

        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;

    }