-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCaesar.java
More file actions
71 lines (58 loc) · 1.56 KB
/
Caesar.java
File metadata and controls
71 lines (58 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
*
* 凯撒密码 - 简单的替换加密
*
* 问题:将字母表中的每个字母移动固定位数
*
* 核心思想:
* - 字母替换
* - 循环移位
* - 保持大小写
*
* 时间复杂度: O(n)
* 空间复杂度: O(n)
*/
public class Caesar {
/*
*
* 凯撒加密
*/
public static String encrypt(String text, int shift) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (Character.isUpperCase(c)) {
c = (char) ((c - 'A' + shift) % 26 + 'A');
} else if (Character.isLowerCase(c)) {
c = (char) ((c - 'a' + shift) % 26 + 'a');
}
result.append(c);
}
return result.toString();
}
/*
*
* 凯撒解密
*/
public static String decrypt(String text, int shift) {
return encrypt(text, 26 - (shift % 26));
}
/*
*
* 主函数
*/
public static void main(String[] args) {
System.out.println("=== 凯撒密码 ===");
String text = "Hello, World!";
int shift = 3;
System.out.println("明文: " + text);
System.out.println("移位数: " + shift);
// 加密
String encrypted = encrypt(text, shift);
System.out.println("加密后: " + encrypted);
// 解密
String decrypted = decrypt(encrypted, shift);
System.out.println("解密后: " + decrypted);
System.out.println("验证: " + text.equals(decrypted));
}
}