Java实现简单的加密,解密实例

2021/4/12 12:27:08

本文主要是介绍Java实现简单的加密,解密实例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一 概述

此例子用于简单的加密解密操作

二 自定义规则的MD5加密解密

    // MD5加密32位
    public static String MD5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];

        byte[] md5Bytes = md5.digest(byteArray);

        StringBuffer hexValue = new StringBuffer();

        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }

        return hexValue.toString();
    }

加密

    // 可逆的加密算法
    public static String encode(String inStr) {
        char[] a = inStr.toCharArray();
        for (int i = 0; i < a.length; i++) {
            a[i] = (char) (a[i] ^ 'p');
        }
        String s = new String(a);
        return s;
    }

解密

    // 加密后解密
    public static String decode(String inStr) {
        char[] a = inStr.toCharArray();
        for (int i = 0; i < a.length; i++) {
            a[i] = (char) (a[i] ^ 'p');
        }
        String k = new String(a);
        return k;
    }

三 基于JDK的AES加密解密

加密

    //加密密钥
    public static final String secret = "QINGSHANKEJIWAYINANDYINGE";

    /**
     * @Author zhoucheng
     * @MethodName AESJDKEncode
     * @Param [message, Key]
     * @Date 18:22 2021/4/8
     * @return: byte[]
     * @Version 1.0
     * @Description AES加密
     **/
    public static byte[] AESJDKEncode(String message, String Key) {

        try {
            KeyGenerator keyGeneratorEncode = KeyGenerator.getInstance("AES");

            keyGeneratorEncode.init(256, new SecureRandom(Key.getBytes(StandardCharsets.UTF_8)));
            SecretKey secretKey = keyGeneratorEncode.generateKey();
            byte[] encode = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(encode, "AES");

            //创建密码器
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = cipher.doFinal(message.getBytes(StandardCharsets.UTF_8));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

解密

    /**
     * @Author zhoucheng
     * @MethodName AESJDKDecode
     * @Param [message, Key]
     * @Date 18:22 2021/4/8
     * @return: byte[]
     * @Version 1.0
     * @Description AES解密
     **/
    public static byte[] AESJDKDecode(byte[] message, String Key) {

        try {
            KeyGenerator keyGeneratorDecode = KeyGenerator.getInstance("AES");

            keyGeneratorDecode.init(256, new SecureRandom(Key.getBytes(StandardCharsets.UTF_8)));
            SecretKey secretKey = keyGeneratorDecode.generateKey();
            byte[] decode = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(decode, "AES");
            //创建密码器
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] result = cipher.doFinal(message);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

byte数组转换成字符串输出

    /**
     * @Author zhoucheng
     * @MethodName convertByteToHexString
     * @Param [bytes]
     * @Date 19:11 2021/4/8
     * @return: java.lang.String
     * @Version 1.0
     * @Description 将byte数组转化为16进制字符串
     **/
    public static String convertByteToHexString(byte[] bytes) {

        String result = "";

        for (int i = 0; i < bytes.length; i++) {
            int temp = bytes[i] & 0xff;
            String tempHex = Integer.toHexString(temp);
            if (tempHex.length() < 2) {
                result += "0" + tempHex;
            }else {
                result += tempHex;
            }
        }
        return result;
    }

一些加密方式的自我简单实现。



这篇关于Java实现简单的加密,解密实例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程