Base加密方法很方便,在保存一些特别数据时可以直接调用方法进行加密和解密的操作,我会发一篇专门介绍base64原理的文章,有兴趣的可以先去查看。还有php和python的使用代码我都会贴在文章最后。
下面是使用java进行base64加密与解密的源码,使用时在java项目中新建一个类,然后将代码挂上,在外部加密或解密直接实例化类,再调用响应函数即可,记住值的传递与接收
Base64加密
1 2 3 4 5 6 7 8 9 10 11 |
public static String encodeBase64(String cleartext) { // TODO Auto-generated method stub try { cleartext = new String(Base64.encodeBase64(cleartext.getBytes("UTF-8"))); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return cleartext; } |
Base解密
1 2 3 4 5 6 7 8 9 10 11 |
public static String decodeBase64(String ciphertext){ try { ciphertext = new String(Base64.decodeBase64(ciphertext.getBytes()), "UTF-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ciphertext; } |