欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

一个简单的加密工具,性能貌似不行,待优化

程序员文章站 2023-04-04 19:04:22
一个简单的加密工具,性能貌似不行,待优化 java package com.kxvz.common.crypt; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; ......

一个简单的加密工具,性能貌似不行,待优化

package com.kxvz.common.crypt;

import javax.crypto.cipher;
import javax.crypto.keygenerator;
import javax.crypto.secretkey;
import java.io.bytearrayoutputstream;
import java.security.securerandom;
import java.util.arraylist;
import java.util.list;
import java.util.random;
import java.util.zip.deflater;
import java.util.zip.inflater;

public class xe {
    private static final string hex_normal = "0123456789abcdef";
    private static final string hex_tmp = "0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
    private static final int hexlen = 100;
    private static final list<string> hexs = new arraylist<>(hexlen);
    static {
        gen();
    }
    /**
     * 加密 bin->en->hex->zip
     *
     * @param bytes
     * @return
     */
    public static byte[] en(byte[] bytes, byte[] pass) throws exception {
        long t1 = system.currenttimemillis();
        byte[] en = encrypt(bytes, pass);
        long t2 = system.currenttimemillis();
        string hex = tohex(en);
        long t3 = system.currenttimemillis();
        byte[] res = compress(hex.getbytes());
        long t4 = system.currenttimemillis();
        system.out.println("en time : encrypt: " + (t2 - t1) + " : tohex : " + (t3 - t2) + " : compress: " + (t4 - t3));
        return res;
    }
    /**
     * 解密 zip->hex->de->bin
     *
     * @param bytes
     * @return
     */
    public static byte[] de(byte[] bytes, byte[] pass) throws exception {
        byte[] unzip = uncompress(bytes);
        byte[] hex = hexto(new string(unzip));
        return decrypt(hex, pass);
    }

    public static string enandhex(byte[] bytes, byte[] pass) throws exception {
        byte[] res = en(bytes, pass);
        return tohex(res);
    }

    public static byte[] dewithhex(string content, byte[] pass) throws exception {
        byte[] hex = hexto(content);
        return de(hex, pass);
    }

    private static string gethexsource(int index) {
        return hexs.get(index);
    }

    private static int toint(byte[] bytes) {
        if (bytes == null || bytes.length != 2) {
            throw new runtimeexception("length bytes length error");
        }
        int c1 = (int) bytes[0];
        int c2 = (int) bytes[1];
        return integer.valueof(c1 + "" + (c2 < 10 ? "0" + c2 : c2));
    }

    private static byte[] tobytes(int num) {
        if (num > 10000 || num < 0) {
            throw new runtimeexception("number mast in 0~9999");
        }

        if (num < 100) {
            return new byte[]{0x00, (byte) num};
        } else {
            string tmp = string.valueof(num);
            int a1 = integer.valueof(tmp.substring(0, tmp.length() - 2));
            int a2 = integer.valueof(tmp.substring(tmp.length() - 2));
            return new byte[]{(byte) a1, (byte) a2};
        }
    }

    private static string tohex(byte[] bytes) {
        int index = new random().nextint(hexlen);
        string hextmp = gethexsource(index);
        string result = "";

        byte[] lenbytes = tobytes(index);
        string hex = string.valueof(hex_normal.charat((lenbytes[0] & 0xf0) >> 4));
        hex += string.valueof(hex_normal.charat(lenbytes[0] & 0x0f));
        result += hex;

        hex = string.valueof(hex_normal.charat((lenbytes[1] & 0xf0) >> 4));
        hex += string.valueof(hex_normal.charat(lenbytes[1] & 0x0f));
        result += hex;

        for (int i = 0; i < bytes.length; i++) {
            hex = string.valueof(hextmp.charat((bytes[i] & 0xf0) >> 4));
            hex += string.valueof(hextmp.charat(bytes[i] & 0x0f));
            result += hex;
        }
        return result;
    }

    private static byte[] hexto(string hex) {
        byte indexfirsthigh = (byte) ((hex_normal.indexof(hex.charat(0))) << 4);
        byte indexfirstlow = (byte) hex_normal.indexof(hex.charat(1));
        byte indexfirst = (byte) (indexfirsthigh | indexfirstlow);

        byte indexlasthigh = (byte) ((hex_normal.indexof(hex.charat(2))) << 4);
        byte indexlastlow = (byte) hex_normal.indexof(hex.charat(3));
        byte indexlast = (byte) (indexlasthigh | indexlastlow);

        byte[] lenbytes = new byte[]{indexfirst, indexlast};
        int index = toint(lenbytes);
        string hextmp = gethexsource(index);
        string content = hex.substring(4);
        int len = content.length() >> 1;
        byte[] result = new byte[len];
        byte high = 0;
        byte low = 0;
        for (int i = 0; i < len; i++) {
            high = (byte) ((hextmp.indexof(content.charat(2 * i))) << 4);
            low = (byte) hextmp.indexof(content.charat(2 * i + 1));
            result[i] = (byte) (high | low);
        }
        return result;
    }

    private static byte[] encrypt(byte[] bytes, byte[] pass) throws exception {
        securerandom securerandom = securerandom.getinstance("sha1prng");
        securerandom.setseed(pass);
        keygenerator generator = keygenerator.getinstance("aes");
        generator.init(securerandom);
        secretkey secretkey = generator.generatekey();
        cipher cipher = cipher.getinstance("aes");
        cipher.init(cipher.encrypt_mode, secretkey);
        return cipher.dofinal(bytes);
    }

    private static byte[] decrypt(byte[] bytes, byte[] pass) throws exception {
        securerandom securerandom = securerandom.getinstance("sha1prng");
        securerandom.setseed(pass);
        keygenerator generator = keygenerator.getinstance("aes");
        generator.init(securerandom);
        secretkey secretkey = generator.generatekey();
        cipher cipher = cipher.getinstance("aes");
        cipher.init(cipher.decrypt_mode, secretkey);
        return cipher.dofinal(bytes);
    }

    private static byte[] compress(byte[] inputbyte) throws exception {
        int len = 0;
        deflater defl = new deflater();
        defl.setinput(inputbyte);
        defl.finish();
        bytearrayoutputstream bos = new bytearrayoutputstream();
        byte[] outputbyte = new byte[1024];
        try {
            while (!defl.finished()) {
                len = defl.deflate(outputbyte);
                bos.write(outputbyte, 0, len);
            }
            defl.end();
        } catch (exception e) {
            throw e;
        } finally {
            bos.close();
        }
        return bos.tobytearray();
    }

    private static byte[] uncompress(byte[] inputbyte) throws exception {
        int len = 0;
        inflater infl = new inflater();
        infl.setinput(inputbyte);
        bytearrayoutputstream bos = new bytearrayoutputstream();
        byte[] outbyte = new byte[1024];
        try {
            while (!infl.finished()) {
                len = infl.inflate(outbyte);
                if (len == 0) {
                    break;
                }
                bos.write(outbyte, 0, len);
            }
            infl.end();
        } catch (exception e) {
            throw e;
        } finally {
            bos.close();
        }
        return bos.tobytearray();
    }

    private static void gen() {
        while (hexs.size() < hexlen) {
            string hex = genhextmp();
            if (!hexs.contains(hex)) {
                hexs.add(hex);
            }
        }
    }

    private static string genhextmp() {
        random rnd = new random();
        int len = hex_tmp.length();
        list<character> tmp = new arraylist<>(17);
        string hex = "";
        while (tmp.size() < 16) {
            int index = rnd.nextint(len);
            char c = hex_tmp.charat(index);
            if (!tmp.contains(c)) {
                tmp.add(c);
                hex += string.valueof(c);
            }
        }
        tmp.clear();
        return hex;
    }
}

作者公众号:loyomer

简洁:kxvz的生活记账本。技术,生活,随笔,文摘...

一个简单的加密工具,性能貌似不行,待优化

欢迎大家关注...