package Base64;
import javax.swing.*;
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 = Base64.getEncoder();
final static Base64.Decoder decoder = Base64.getDecoder();
/**
* 给字符串加密
* @param text
* @return
*/
public static String encode(String text) {
byte[] textByte = new byte[0];
try {
textByte = text.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String encodedText = encoder.encodeToString(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.println(Base64Converter.encode(username));
System.out.println(Base64Converter.encode(password));
// 解密
System.out.println("
==== [解密后] 用户名/密码 =====");
System.out.println(Base64Converter.decode(Base64Converter.encode(username)));
System.out.println(Base64Converter.decode(Base64Converter.encode(password)));
}
}
package Base64;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class JFrameDemo {
public void CreateJFrame() {
JFrame jf = new JFrame("这是一个JFrame窗体"); // 实例化一个JFrame对象
jf.setVisible(true); // 设置窗体可视
jf.setSize(50, 35); // 设置窗体大小
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 设置窗体关闭方式
}
public static void main(String[] args) {
new JFrameDemo().CreateJFrame(); // 调用CreateJFrame()方法
}
}