1. 凱撒加密演算法
1.1 演算法邏輯
根據一個固定偏移值(offset), 將字母向一個方向偏移, 進行加密.
1.2 初步思路
- 獲取明文(
plaintext) - 獲取明文字串的單獨字符
- 進行字符值偏移
- 當偏移超出字母范圍時, 回到第一個字母處繼續偏移.
- 得到密文(
ciphertext)
1.3 初步編程
/*
凱撒密碼:
偏移量
A(65)~Z(90)
a(97)~z(122)
方法1: 但偏移量超過范圍時, 回傳到最扯訓圈
方法二:進行陣列偏移(加密)
方法三:進行陣列回位(解密)
*/
public class Task01_Caesar {
public static void main(String[] args) {
// 輸入明文
String plaintext = "I told it was a lie. ";
// 明文加密
String password = leadingPlaintext(plaintext, 10);
System.out.println(password);
// 密文解密
String plaintext1 = leadingPassword(password, 10);
System.out.println(plaintext1);
}
// 凱撒密碼加密
public static String leadingPlaintext(String plaintext, int leadingNum) {
String password = "";
// 將明文轉化成字符陣列
char[] charPassword = plaintext.toCharArray();
// 進行加密操作
int[] intPassword = new int[charPassword.length];
for (int i = 0; i < charPassword.length; i++) {
// 將字符陣列轉化成字符碼陣列
intPassword[i] = (int)charPassword[i];
// 字符碼陣列偏移&范圍限定
intPassword[i] = limitLetter(intPassword[i], intPassword[i]+leadingNum);
// 偏移字符碼陣列重新輸出為字符陣列
charPassword[i] = (char)intPassword[i];
// 將字符陣列轉化成字串
password = String.valueOf(charPassword);
}
return password;
}
// 凱撒密碼解密
public static String leadingPassword(String password, int leadingNum) {
String plaintext = "";
// 將密碼轉化成字符陣列
char[] charPassword = password.toCharArray();
// 進行解密操作
int[] intPassword = new int[charPassword.length];
for (int i = 0; i < charPassword.length; i++) {
// 將字符陣列轉化成字符碼陣列
intPassword[i] = (int)charPassword[i];
// 字符碼陣列偏移&范圍限定
intPassword[i] = limitLetter(intPassword[i], intPassword[i]-leadingNum);
// 偏移字符碼陣列重新輸出為字符陣列
charPassword[i] = (char)intPassword[i];
// 將字符陣列轉化成字串
plaintext = String.valueOf(charPassword);
}
return plaintext;
}
// 進行范圍限定
public static int limitArea(int num, int min, int max) {
int area = 26; // 限定范圍區間
while (num < min || num > max) {
if (num < min) {
num += area;
} else if (num > max) {
num -= area;
}
}
return num;
}
// 進行字母范圍限定
public static int limitLetter(int originNum, int leadingNum) {
if (originNum >=65 && originNum <= 90) {
leadingNum = limitArea(leadingNum, 65, 90);
} else if (originNum >= 97 && originNum <= 122) {
leadingNum = limitArea(leadingNum, 97, 122);
}
return leadingNum;
}
}
我的思路是:
- 首先將字串轉化為字符陣列
- 字符陣列可以轉化為整型陣列
- 對陣列進行偏移
- 對偏移的陣列進行校正
- 將陣列重新回傳為字串
然后我使用了4個方法, 第一個方法(leadingPlaintext)和第二個方法(leadingPassword)進行陣列偏移, 其中呼叫了第三, 四個方法進行偏移陣列校正.
1.4 查詢演算法
public class Task02_Caesar {
public static void main(String[] args) {
String plaintext = "I'm a robot. ";
String pwd = caesar(plaintext, 5);
String str = caesar(pwd, -5);
System.out.println(pwd);
System.out.println(str);
}
public static String caesar(String text, int offset) {
String cipher = "";
for (int i = 0; i < text.length(); i++) {
// 迭代字符
char c = text.charAt(i);
if (c >= 'A' && c <= 'Z') { // 若當前選中字符為大寫字母
c += (offset % 26);
if (c < 'A') {
c += 26;
} else if (c > 'Z') {
c -= 26;
}
} else if (c >= 'a' && c <= 'z') { // 若當前選中字符為小寫字母
c += (offset % 26);
if (c < 'a') {
c += 26;
} else if (c > 'z') {
c -= 26;
}
}
cipher += c;
}
return cipher;
}
}
1.5 思路重置
- 不需要將字串轉化為字符陣列, 可以通過
String.charAt()方法在for回圈里直接獲取單獨的字符. 不需要使用String.toCharArray()方法將字串轉化為字符陣列. - 因為字符char本質其實是數字, 所以可以直接使用char進行邏輯判斷, 不需要將其轉換為數字碼點再判斷.
- 當需要框定一個數的范圍, 進行A-B回圈時, 可以通過取余操作進行限定.
b = (b % 26)+1(限定范圍1~26的數字)
1.6 A-B回圈
/*
* description: 1~26回圈數輸出
*/
public class Task03_ABLoop {
public static void main(String[] args) {
for (int i = 0; i <= 100; i++) {
System.out.print(i%26+1+" ");
if (i%26+1 == 26) {
System.out.println();
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/522908.html
標籤:其他
上一篇:java基礎-注解
