• JAVA-3DES对称加解密工具(不依赖第三方库)


    
    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    import java.nio.charset.StandardCharsets;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    public class EncryptUtil {
    
        // 密钥
        public static final String ENCRYPT_KEY = "ABCD1234";
    
        // 定义加密算法,可用DES,DESede,Blowfish
        private static final String ALGORITHM_DESDE = "DESede";
    
        public static void main(String[] args) {
            // 加密
            String encPassword = desEdeEncoder("123456", EncryptUtil.ENCRYPT_KEY);
            System.out.println(encPassword);
            // 解密
            String desPassword = desEdeDecoder(encPassword, EncryptUtil.ENCRYPT_KEY);
            System.out.println(desPassword);
        }
    
    
        public static boolean isEmpty(String str) {
            return null == str || str.trim().length() == 0;
        }
    
        /**
         * 3DES加密
         *
         * @param src
         * @param key
         * @return
         */
        public static String desEdeEncoder(String src, String key) {
            byte[] bytes = null;
            try {
                SecretKeySpec secretKey = new SecretKeySpec(build3DesKey(key), ALGORITHM_DESDE);
                Cipher cipher = Cipher.getInstance(ALGORITHM_DESDE);
                cipher.init(Cipher.ENCRYPT_MODE, secretKey);
                bytes = cipher.doFinal(src.getBytes(StandardCharsets.UTF_8));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return byte2HexStr(bytes);
        }
    
        /**
         * 3DES 解密
         *
         * @param dest
         * @param key
         * @return
         */
        public static String desEdeDecoder(String dest, String key) {
            String desStr;
            try {
                SecretKeySpec secretKey = new SecretKeySpec(build3DesKey(key), ALGORITHM_DESDE);
                Cipher cipher = Cipher.getInstance(ALGORITHM_DESDE);
                cipher.init(Cipher.DECRYPT_MODE, secretKey);
                byte[] bytes = cipher.doFinal(str2ByteArray(dest));
                desStr = new String(bytes, StandardCharsets.UTF_8);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return desStr;
        }
    
    
        public static String md5Digest(String src) throws NoSuchAlgorithmException {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(src.getBytes(StandardCharsets.UTF_8));
            return byte2HexStr(bytes);
        }
    
        private static byte[] build3DesKey(String keyStr) {
            byte[] bytes = new byte[24];
            byte[] temp = keyStr.getBytes(StandardCharsets.UTF_8);
            System.arraycopy(temp, 0, bytes, 0, Math.min(bytes.length, temp.length));
            return bytes;
        }
    
        private static byte[] str2ByteArray(String str) {
            int byteArrayLength = str.length() / 2;
            byte[] bytes = new byte[byteArrayLength];
            for (int i = 0; i < byteArrayLength; i++) {
                byte b0 = (byte) Integer.valueOf(str.substring(i * 2, i * 2 + 2), 16).intValue();
                bytes[i] = b0;
            }
            return bytes;
        }
    
        private static String byte2HexStr(byte[] bytes) {
            StringBuilder sbf = new StringBuilder();
            for (byte aByte : bytes) {
                String s = Integer.toHexString(aByte & 0xFF);
                if (s.length() == 1) {
                    sbf.append("0");
                }
                sbf.append(s.toUpperCase());
            }
            return sbf.toString();
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
  • 相关阅读:
    图论——强连通分量缩点+拓扑排序
    Qt简易闹钟
    Linux内核设计与实现(六)| 内存管理
    Prometheus插件安装(cadvisor)
    纯血鸿蒙APP实战开发——Navigation页面跳转对象传递案例
    今天步行数5000多
    刷题记录(NC235611 牛牛国的战争,NC23803 DongDong认亲戚,NC235622 叠积木)
    Minio设置文件永久访问和下载
    快速上手Django(七) -Django之登录cookie和session
    雅思成绩单上的这个符号, CEFR 究竟是什么意思
  • 原文地址:https://blog.csdn.net/piaoyunlive/article/details/132833895