Java语⾔实现Base64加密解密
Java语⾔实现 Base64 加密 & 解密
Base64是⽹络上最常见的⽤于传输8Bit字节码的编码⽅式之⼀,Base64就是⼀种基于64个可打印字符来表⽰⼆进制数据的⽅法。Base64编码是从⼆进制到字符的过程,可⽤于在HTTP环境下传递较长的标识信息。
采⽤Base64编码具有不可读性,需要解码后才能阅读。
Base64由于以上优点被⼴泛应⽤于计算机的各个领域。
本⽂讲解如何使⽤Java语⾔实现Base64的加密和解密。(基于 JDK 1.8 的新增功能 Base64 特性)
初始版本
代码如下:
import java.io.UnsupportedEncodingException;
import java.util.Base64;
/**
* @author Miracle Luna
* @version 1.0
* @date 2019/7/3 18:55
*/
public class Base64Converter {
final static Base64.Encoder encoder = Encoder();
final static Base64.Decoder decoder = Decoder();
/**
* 给字符串加密
* @param text
* @return
*/
public static String encode(String text) {
byte[] textByte = new byte[0];
try {
textByte = Bytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String encodedText = deToString(textByte);
return encodedText;
}
/**
* 将加密后的字符串进⾏解密
* @param encodedText
* @return
*/
public static String decode(String encodedText) {
String text = null;
try {
text = new String(decoder.decode(encodedText), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return text;
}
public static void main(String[] args) throws UnsupportedEncodingException {
String username = "Miracle Luna";
String password = "p@sSW0rd";
// 加密
System.out.println("==== [加密后] ⽤户名/密码 =====");
System.out.de(username));
System.out.de(password));
/
/ 解密
System.out.println("\n==== [解密后] ⽤户名/密码 =====");
System.out.println(Base64Converter.de(username))); System.out.println(Base64Converter.de(password))); }
}
运⾏结果如下:
==== [加密后] ⽤户名/密码 =====
TWlyYWNsZSBMdW5h
cEBzU1cwcmQ=
==== [解密后] ⽤户名/密码 =====
Miracle Luna
p@sSW0rd
改进版本(推荐)
代码如下:
import java.nio.charset.StandardCharsets;
import java.util.Base64;
/**
* @author Miracle Luna
* @version 1.0
* @date 2019/7/3 18:55
*/
public class Base64Util {
final static Base64.Encoder encoder = Encoder();
final static Base64.Decoder decoder = Decoder();
/**
* 给字符串加密
* @param text
* @return
*/
public static String encode(String text) {
// byte[] textByte = Bytes(StandardCharsets.UTF_8);
// String encodedText = deToString(textByte);
// return encodedText;
java源代码加密Bytes(StandardCharsets.UTF_8));
}
/**
* 将加密后的字符串进⾏解密
* @param encodedText
* @return
*/
public static String decode(String encodedText) {
return new String(decoder.decode(encodedText), StandardCharsets.UTF_8);
}
public static void main(String[] args) {
String username = "Miracle Luna";
String password = "p@sSW0rd";
// 加密
System.out.println("==== [加密后] ⽤户名/密码 =====");
System.out.de(username));
System.out.de(password));
// 解密
System.out.println("\n==== [解密后] ⽤户名/密码 =====");
System.out.println(Base64Util.de(username)));
System.out.println(Base64Util.de(password)));
}
}
运⾏结果如下:
==== [加密后] ⽤户名/密码 =====
TWlyYWNsZSBMdW5h
cEBzU1cwcmQ=
==== [解密后] ⽤户名/密码 =====
Miracle Luna
p@sSW0rd
PS:
改进版本使⽤了 StandardCharsets.UTF_8 代替了 "UTF-8"。
所以,没有抛出初始版本的 UnsupportedEncodingException 异常。StandardCharsets.java 源码如下:
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.nio.charset;
/**
* Constant definitions for the standard {@link Charset Charsets}. These
* charsets are guaranteed to be available on every implementation of the Java
* platform.
*
* @see <a href="Charset#standard">Standard Charsets</a>
* @since 1.7
*/
public final class StandardCharsets {
private StandardCharsets() {
throw new AssertionError("No java.nio.charset.StandardCharsets instances for you!"); }
/**
* Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the
* Unicode character set
*/
public static final Charset US_ASCII = Charset.forName("US-ASCII");
/**
* ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
*/
public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
/
**
* Eight-bit UCS Transformation Format
*/
public static final Charset UTF_8 = Charset.forName("UTF-8");
/**
* Sixteen-bit UCS Transformation Format, big-endian byte order
*/
public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
/**
* Sixteen-bit UCS Transformation Format, little-endian byte order
*/
public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
/**
* Sixteen-bit UCS Transformation Format, byte order identified by an * optional byte-order mark
*/
public static final Charset UTF_16 = Charset.forName("UTF-16");
}
推荐⼀个在线 Base64 加密&解密的⽹页:
对 p@sSW0rd 进⾏加密,效果如下:
加密前:
加密后:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论