美文网首页
Java Encryption and Decryption

Java Encryption and Decryption

作者: 阿武_Accat | 来源:发表于2019-02-10 23:41 被阅读0次

    异或

    package c_001_introductory;
    
    /*
     * @program source-analysis
     * @description
     * @author Rudolph Browne
     * @create 18-11-10
     */
    public class T_01_Xor {
    
        private static int XOR = 133388;
    
        public static String xor(String input) {    // => "abc" -> {'a','b','c'}
            char[] chars = input.toCharArray();     // => 这里之所以转成char,是因为char可以和int相互转换
            for (int i=0; i < chars.length; i++) {
                chars[i] = (char) (chars[i] ^ XOR);
            }
            return new String(chars);               // => {'a','b','c'} -> "abc"
        }
    
    
        public static void main(String[] args){
              String password = "accat@133388_li2";
              System.out.println("原密码:" + password);
              password = xor(password);
              System.out.println("加密后:" + password);
              password = xor(password);
              System.out.println("解密后:" + password);
        }
    }
    
    /**
     * XOR 异或加解密
     * 异或一次是加密,异或两次解密,可逆
     * xorCode = 3
     *
     * x -> 120 -> 1111000
     *
     * ‘x’ ^ xorCode 加密
     *
     *   01111000
     * ^ 00000011
     * ----------
     *   01111011
     *
     * 'x' ^ xorCode 解密
     *
     *   01111011
     * ^ 00000011
     * ----------
     *   01111000
     */
    

    MD5

    package c_001_introductory;
    
    import org.springframework.util.DigestUtils;
    
    /*
     * @program source-analysis
     * @description
     * @author Rudolph Browne
     * @create 18-11-10
     */
    public class T_02_Md5 {
    
        // => 为什么要用byte[], byte相当于二进制格式,所有数据都可以传入该方法,只要事先转成byte[]形式
        // => Byte 十六进制 5D -> 计算机底层其实就是二进制数据, 01011110
        public static String md5(byte[] input) {
    
            return DigestUtils.md5DigestAsHex(input);
        }
    
        public static void main(String[] args){
            String password = "accat@133388_li2";
            System.out.println("原密码:" + password);
            String passCode = md5(password.getBytes());
            System.out.println("密码序列:" + passCode);
        }
    }
    
    /**
     * 使用Apache提供的依赖包,能够直接使用MD5加密,不可逆
     * 引入maven依赖 Apache Commons Codec
     *
     * MD5存在着专门的破解网站如cmd5, 专门存储大量简密码的md5串, 然后盲扫反解密码
     * 所以又有一种干扰的措施, 加盐, 盐是串随机的串, 你怎么爽怎么来.
     *
     * MD5("fgekhsl2h4545ksnlser3" + password)
     *
     */
    

    Sha

    package c_001_introductory;
    
    import org.apache.commons.codec.digest.DigestUtils;
    
    /*
     * @program source-analysis
     * @description
     * @author Rudolph Browne
     * @create 18-11-10
     */
    public class T_03_Sha {
    
        public static String sha(byte[] input) {
            return DigestUtils.sha256Hex(input);
        }
    
        public static void main(String[] args){
            String password = "accat@133388_li2";
            System.out.println("原密码:" + password);
            String passCode = sha(password.getBytes());
            System.out.println("密码序列:" + passCode);
        }
    }
    

    Base64

    package c_001_introductory;
    
    import java.util.Base64;
    
    /*
     * @program source-analysis
     * @description
     * @author Rudolph Browne
     * @create 18-11-10
     */
    public class T_04_Base64 {
    
        public static String encode(byte[] input) {
    
            String result = null;
    
    //        try {
    //            Class clazz = Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
    //            Method method = clazz.getMethod("encode", byte[].class);
    //            result = (String) method.invoke(null, input);
    //            // => 当method调用的是static方法时,可以不指定调用对象
    //            // Person person = new Person(); method.invoke(null, null);
    //
    //        } catch (ClassNotFoundException e) {
    //            e.printStackTrace();
    //        } catch (NoSuchMethodException e) {
    //            e.printStackTrace();
    //        } catch (IllegalAccessException e) {
    //            e.printStackTrace();
    //        } catch (InvocationTargetException e) {
    //            e.printStackTrace();
    //        }
    
            result = Base64.getEncoder().encodeToString(input);
    
            return result;
        }
    
        public static byte[] decode(String input) {
            byte[] result = new byte[0];
    //        try {
    //            Class clazz = Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
    //            Method method = clazz.getMethod("decode", String.class);
    //            result = (byte[]) method.invoke(null, input);
    //
    //        } catch (ClassNotFoundException e) {
    //            e.printStackTrace();
    //        } catch (NoSuchMethodException e) {
    //            e.printStackTrace();
    //        } catch (IllegalAccessException e) {
    //            e.printStackTrace();
    //        } catch (InvocationTargetException e) {
    //            e.printStackTrace();
    //        }
            result = Base64.getDecoder().decode(input.getBytes());
            return result;
        }
    
        public static void main(String[] args){
            String password = "accat@133388_li2";
            System.out.println("原密码:" + password);
            password = encode(password.getBytes());
            System.out.println("编码后:" + password);
            password = new String(decode(password));
            System.out.println("解码后:" + password);
        }
    }
    

    综合运用

    package c_001_introductory;
    
    import lombok.extern.slf4j.Slf4j;
    
    import java.time.LocalTime;
    import java.util.Scanner;
    
    /*
     * @program source-analysis
     * @description 模仿注册登录加解密
     * @author Rudolph Browne
     * @create 18-11-10
     */
    @Slf4j
    public class T_05_RegisterAndLogin {
    
        static String username;
        static String password;
        static String gender;
        static Scanner inputScanner = new Scanner(System.in);
    
        public static boolean register() {
            boolean result = false;
    
            try {
                System.out.println("请输入用户名:");
                username = inputScanner.next();
                System.out.println("请输入密码(不小于8位):");
                password = inputScanner.next();
                System.out.println("请输入性别(男/女):");
                String gender1 = inputScanner.next();
                gender = gender1.equals("男") ? "先生" : gender1.equals("女") ? "小姐" : "xx";
                //if (男) 先生 if (女) 小姐 else xx
                // 加密 + check
                password = password.substring(1, 3) + (password.length() + 2)
                        + password.substring(5,7) + username.substring(0,2)
                        + username.substring(1,3);
                log.info("substring and length - {}", password);
                password = T_01_Xor.xor(password);
                log.info("xor - {}", password);
                password = T_02_Md5.md5(password.getBytes());
                log.info("md5 - {}", password);
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return result;
        }
    
        public static boolean login() {
            boolean result = false;
    
            try {
                System.out.println("请输入用户名:");
                String username1 = inputScanner.next();
                System.out.println("请输入密码:");
                String password1 = inputScanner.next();
                password1 = password1.substring(1, 3) + (password1.length() + 2)
                        + password1.substring(5,7) + username1.substring(0, 2)
                        + username1.substring(1,3);
                password1 = T_01_Xor.xor(password1);
                password1 = T_02_Md5.md5(password1.getBytes());
                if (username.equals(username1) && password.equals(password1)) {
                    int hour = LocalTime.now().getHour();
                    String greetings = hour < 3 ? "凌晨好." : hour < 11 ? "早上好." :
                                            hour < 14 ? "中午好." : hour < 17 ? "下午好." :
                                            hour < 19 ? "傍晚好." : "晚上好";
                    result = true;
                    System.out.println(username + gender + "," + greetings);
                } else {
                    System.out.println("登录失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
        
        public static void main(String[] args){
              register();
              login();
        }
    }
    

    相关文章

      网友评论

          本文标题:Java Encryption and Decryption

          本文链接:https://www.haomeiwen.com/subject/cdlfeqtx.html