異或
package c_001_introductory;
/*
* @program source-analysis
* @description
* @author Rudolph Browne
* @create 18-11-10
*/
public class T_01_Xor {
private static int XOR = 133388;
public static String xor(String input) { // => "abc" -> {'a','b','c'}
char[] chars = input.toCharArray(); // => 這里之所以轉(zhuǎn)成char,是因?yàn)閏har可以和int相互轉(zhuǎn)換
for (int i=0; i < chars.length; i++) {
chars[i] = (char) (chars[i] ^ XOR);
}
return new String(chars); // => {'a','b','c'} -> "abc"
}
public static void main(String[] args){
String password = "accat@133388_li2";
System.out.println("原密碼:" + password);
password = xor(password);
System.out.println("加密后:" + password);
password = xor(password);
System.out.println("解密后:" + password);
}
}
/**
* XOR 異或加解密
* 異或一次是加密,異或兩次解密,可逆
* xorCode = 3
*
* x -> 120 -> 1111000
*
* ‘x’ ^ xorCode 加密
*
* 01111000
* ^ 00000011
* ----------
* 01111011
*
* 'x' ^ xorCode 解密
*
* 01111011
* ^ 00000011
* ----------
* 01111000
*/
MD5
package c_001_introductory;
import org.springframework.util.DigestUtils;
/*
* @program source-analysis
* @description
* @author Rudolph Browne
* @create 18-11-10
*/
public class T_02_Md5 {
// => 為什么要用byte[], byte相當(dāng)于二進(jìn)制格式,所有數(shù)據(jù)都可以傳入該方法,只要事先轉(zhuǎn)成byte[]形式
// => Byte 十六進(jìn)制 5D -> 計(jì)算機(jī)底層其實(shí)就是二進(jìn)制數(shù)據(jù), 01011110
public static String md5(byte[] input) {
return DigestUtils.md5DigestAsHex(input);
}
public static void main(String[] args){
String password = "accat@133388_li2";
System.out.println("原密碼:" + password);
String passCode = md5(password.getBytes());
System.out.println("密碼序列:" + passCode);
}
}
/**
* 使用Apache提供的依賴包,能夠直接使用MD5加密,不可逆
* 引入maven依賴 Apache Commons Codec
*
* MD5存在著專門的破解網(wǎng)站如cmd5, 專門存儲(chǔ)大量簡(jiǎn)密碼的md5串, 然后盲掃反解密碼
* 所以又有一種干擾的措施, 加鹽, 鹽是串隨機(jī)的串, 你怎么爽怎么來(lái).
*
* MD5("fgekhsl2h4545ksnlser3" + password)
*
*/
Sha
package c_001_introductory;
import org.apache.commons.codec.digest.DigestUtils;
/*
* @program source-analysis
* @description
* @author Rudolph Browne
* @create 18-11-10
*/
public class T_03_Sha {
public static String sha(byte[] input) {
return DigestUtils.sha256Hex(input);
}
public static void main(String[] args){
String password = "accat@133388_li2";
System.out.println("原密碼:" + password);
String passCode = sha(password.getBytes());
System.out.println("密碼序列:" + passCode);
}
}
Base64
package c_001_introductory;
import java.util.Base64;
/*
* @program source-analysis
* @description
* @author Rudolph Browne
* @create 18-11-10
*/
public class T_04_Base64 {
public static String encode(byte[] input) {
String result = null;
// try {
// Class clazz = Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
// Method method = clazz.getMethod("encode", byte[].class);
// result = (String) method.invoke(null, input);
// // => 當(dāng)method調(diào)用的是static方法時(shí),可以不指定調(diào)用對(duì)象
// // Person person = new Person(); method.invoke(null, null);
//
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// } catch (NoSuchMethodException e) {
// e.printStackTrace();
// } catch (IllegalAccessException e) {
// e.printStackTrace();
// } catch (InvocationTargetException e) {
// e.printStackTrace();
// }
result = Base64.getEncoder().encodeToString(input);
return result;
}
public static byte[] decode(String input) {
byte[] result = new byte[0];
// try {
// Class clazz = Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
// Method method = clazz.getMethod("decode", String.class);
// result = (byte[]) method.invoke(null, input);
//
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// } catch (NoSuchMethodException e) {
// e.printStackTrace();
// } catch (IllegalAccessException e) {
// e.printStackTrace();
// } catch (InvocationTargetException e) {
// e.printStackTrace();
// }
result = Base64.getDecoder().decode(input.getBytes());
return result;
}
public static void main(String[] args){
String password = "accat@133388_li2";
System.out.println("原密碼:" + password);
password = encode(password.getBytes());
System.out.println("編碼后:" + password);
password = new String(decode(password));
System.out.println("解碼后:" + password);
}
}
綜合運(yùn)用
package c_001_introductory;
import lombok.extern.slf4j.Slf4j;
import java.time.LocalTime;
import java.util.Scanner;
/*
* @program source-analysis
* @description 模仿注冊(cè)登錄加解密
* @author Rudolph Browne
* @create 18-11-10
*/
@Slf4j
public class T_05_RegisterAndLogin {
static String username;
static String password;
static String gender;
static Scanner inputScanner = new Scanner(System.in);
public static boolean register() {
boolean result = false;
try {
System.out.println("請(qǐng)輸入用戶名:");
username = inputScanner.next();
System.out.println("請(qǐng)輸入密碼(不小于8位):");
password = inputScanner.next();
System.out.println("請(qǐng)輸入性別(男/女):");
String gender1 = inputScanner.next();
gender = gender1.equals("男") ? "先生" : gender1.equals("女") ? "小姐" : "xx";
//if (男) 先生 if (女) 小姐 else xx
// 加密 + check
password = password.substring(1, 3) + (password.length() + 2)
+ password.substring(5,7) + username.substring(0,2)
+ username.substring(1,3);
log.info("substring and length - {}", password);
password = T_01_Xor.xor(password);
log.info("xor - {}", password);
password = T_02_Md5.md5(password.getBytes());
log.info("md5 - {}", password);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static boolean login() {
boolean result = false;
try {
System.out.println("請(qǐng)輸入用戶名:");
String username1 = inputScanner.next();
System.out.println("請(qǐng)輸入密碼:");
String password1 = inputScanner.next();
password1 = password1.substring(1, 3) + (password1.length() + 2)
+ password1.substring(5,7) + username1.substring(0, 2)
+ username1.substring(1,3);
password1 = T_01_Xor.xor(password1);
password1 = T_02_Md5.md5(password1.getBytes());
if (username.equals(username1) && password.equals(password1)) {
int hour = LocalTime.now().getHour();
String greetings = hour < 3 ? "凌晨好." : hour < 11 ? "早上好." :
hour < 14 ? "中午好." : hour < 17 ? "下午好." :
hour < 19 ? "傍晚好." : "晚上好";
result = true;
System.out.println(username + gender + "," + greetings);
} else {
System.out.println("登錄失敗");
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args){
register();
login();
}
}