加密算法

来源:互联网 发布:网达软件最新消息 编辑:程序博客网 时间:2024/05/03 06:56
public class text {    static final byte[] bytes_a = new byte[]{66, 108, 111, 119, 102, 105, 115, 104};    static final byte[] bytes_key = new byte[]{104,101,108,108,111,50,48,49,55};    static final byte[] bytes_content = new byte[]{103,111,111,100,32,106,111,98,32,77,121,32,102,114,105,101,110,100};    public static void main(String[] args) {                SecretKeySpec se_key = new SecretKeySpec(bytes_key,new String(bytes_a));       byte[] target =  encrypt(se_key,bytes_content);        System.out.println(new String(target));        byte[] bytes = decrypt(se_key,target);        System.out.println(new String(bytes));    }    public static byte[] encrypt(Key key, byte[] text){        try {            Cipher cipher = Cipher.getInstance(new String(bytes_a));            cipher.init(Cipher.ENCRYPT_MODE,key);            return cipher.doFinal(text);        } catch (NoSuchAlgorithmException e) {            e.printStackTrace();        } catch (NoSuchPaddingException e) {            e.printStackTrace();        } catch (InvalidKeyException e) {            e.printStackTrace();        } catch (BadPaddingException e) {            e.printStackTrace();        } catch (IllegalBlockSizeException e) {            e.printStackTrace();        }        return null;    }    public static byte[] decrypt(Key key, byte[] text){        try {            Cipher cipher = Cipher.getInstance(new String(bytes_a));            cipher.init(Cipher.DECRYPT_MODE,key);            return cipher.doFinal(text);        } catch (NoSuchAlgorithmException e) {            e.printStackTrace();        } catch (NoSuchPaddingException e) {            e.printStackTrace();        } catch (InvalidKeyException e) {            e.printStackTrace();        } catch (BadPaddingException e) {            e.printStackTrace();        } catch (IllegalBlockSizeException e) {            e.printStackTrace();        }        return null;    }}

0 0
原创粉丝点击