• JAVA BASE64 加密解密实现代码


    1、代码

    package com.zhaochao.utill;
    import java.io.UnsupportedEncodingException;
    import java.lang.reflect.Method;
    
    public class Base64Utill {
    	public static String encode(String code) throws UnsupportedEncodingException, Exception {
    		return encodeBase64(code.getBytes());
    	}
    
    	public static String decode(String code) throws UnsupportedEncodingException, Exception {
    		return new String(decodeBase64(code));
    	}
    
    	public static String encodeBase64(byte[] input) throws Exception {
    		Class clazz = Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
    		Method mainMethod = clazz.getMethod("encode", byte[].class);
    		mainMethod.setAccessible(true);
    		Object retObj = mainMethod.invoke(null, new Object[] { input });
    		return (String) retObj;
    	}
    
    	public static byte[] decodeBase64(String input) throws Exception {
    		Class clazz = Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
    		Method mainMethod = clazz.getMethod("decode", String.class);
    		mainMethod.setAccessible(true);
    		Object retObj = mainMethod.invoke(null, input);
    		return (byte[]) retObj;
    	}
    
    	public static void main(String[] rags) throws UnsupportedEncodingException, Exception {
    		String str = "abadjfajerjaqwrasdf";
    		String code = Base64Utill.encode(str);
    		System.err.println("加密前:" + str);
    		System.err.println("加密后:" + code);
    		System.err.println("解密后:" + Base64Utill.decode(code));
    
    	}
    
    }
    

    2、输出结果

    加密前:abadjfajerjaqwrasdf
    加密后:YWJhZGpmYWplcmphcXdyYXNkZg==
    解密后:abadjfajerjaqwrasdf
    


  • 相关阅读:
    转 c#中 base64字符串与普通字符串互转
    Dapper丶DapperExtention,以及AbpDapper之间的关系,
    Dapper优秀资料
    软件工程 BUAAMOOC项目Postmortem结果
    M1阶段个人总结
    Fifteen scrum meeting 2015-11-21
    Fourteenth scrum meeting
    第一阶段工作展示
    Alpha版本测试文档
    BUAAMOOC-Alpha版本发布说明
  • 原文地址:https://www.cnblogs.com/whzhaochao/p/5023417.html
Copyright © 2020-2023  润新知