几种常用的加密方法

public static void main(String[] args) throws Exception {
        String md5Str = encodeToMD532("hello word");
        //32位,小写
        String md532Lower = md5Str.toLowerCase();
        //32位,大写
        String md532Upper = md532Lower.toUpperCase();
        //16位,小写
        String md516Lower = md532Lower.substring(8, 24);
        //16位,大写
        String md516Upper = md532Lower.substring(8, 24).toUpperCase();
    }

    private static String encodeToMD532(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(text.getBytes());
            byte[] digest = md.digest();
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b & 0xff));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    private static String encodeSHA256(String msg) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-256");
            byte[] digest = md.digest(msg.getBytes());
            StringBuilder hexString = new StringBuilder();
            for (byte b : digest) {
                hexString.append(String.format("%02x", b));
            }
            return hexString.toString();
        } catch (Exception e) {
            // 处理算法不可用的情况
            throw new RuntimeException(e);
        }
    }

  private static String encodeBase64(String msg) {
        return Base64.getEncoder().encodeToString(msg.getBytes());
    }
posted @   Dyaqi  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示