Skip to content
This repository was archived by the owner on Apr 6, 2025. It is now read-only.

Commit 0dc7254

Browse files
committed
feat: Caesar Cipher algorithm
0 parents  commit 0dc7254

File tree

12 files changed

+511
-0
lines changed

12 files changed

+511
-0
lines changed

.gitignore

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Created by https://www.toptal.com/developers/gitignore/api/eclipse
2+
# Edit at https://www.toptal.com/developers/gitignore?templates=eclipse
3+
4+
### Eclipse ###
5+
.metadata
6+
bin/
7+
tmp/
8+
*.tmp
9+
*.bak
10+
*.swp
11+
*~.nib
12+
local.properties
13+
.settings/
14+
.loadpath
15+
.recommenders
16+
17+
# External tool builders
18+
.externalToolBuilders/
19+
20+
# Locally stored "Eclipse launch configurations"
21+
*.launch
22+
23+
# PyDev specific (Python IDE for Eclipse)
24+
*.pydevproject
25+
26+
# CDT-specific (C/C++ Development Tooling)
27+
.cproject
28+
29+
# CDT- autotools
30+
.autotools
31+
32+
# Java annotation processor (APT)
33+
.factorypath
34+
35+
# PDT-specific (PHP Development Tools)
36+
.buildpath
37+
38+
# sbteclipse plugin
39+
.target
40+
41+
# Tern plugin
42+
.tern-project
43+
44+
# TeXlipse plugin
45+
.texlipse
46+
47+
# STS (Spring Tool Suite)
48+
.springBeans
49+
50+
# Code Recommenders
51+
.recommenders/
52+
53+
# Annotation Processing
54+
.apt_generated/
55+
.apt_generated_test/
56+
57+
# Scala IDE specific (Scala & Java development for Eclipse)
58+
.cache-main
59+
.scala_dependencies
60+
.worksheet
61+
62+
# Uncomment this line if you wish to ignore the project description file.
63+
# Typically, this file would be tracked if it contains build/dependency configurations:
64+
#.project
65+
66+
### Eclipse Patch ###
67+
# Spring Boot Tooling
68+
.sts4-cache/
69+
70+
# End of https://www.toptal.com/developers/gitignore/api/eclipse

EncryptionAlgorithms/.classpath

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
4+
<attributes>
5+
<attribute name="module" value="true"/>
6+
</attributes>
7+
</classpathentry>
8+
<classpathentry kind="src" path="src"/>
9+
<classpathentry kind="lib" path="src/lib/gui325.jar"/>
10+
<classpathentry kind="output" path="bin"/>
11+
</classpath>

EncryptionAlgorithms/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>EncryptionAlgorithms</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package algorithms;
2+
3+
import java.util.Arrays;
4+
5+
public class CaesarCipher {
6+
private final String[] lowerAlpha = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K","L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
7+
private String originString;
8+
private int key;
9+
10+
public CaesarCipher(String originString, int key) {
11+
super();
12+
setOriginString(originString);
13+
setKey(key);
14+
}
15+
public CaesarCipher() {
16+
super();
17+
}
18+
public String getOriginString() {
19+
return originString;
20+
}
21+
public void setOriginString(String originString) {
22+
this.originString = originString;
23+
}
24+
public int getKey() {
25+
return key;
26+
}
27+
public void setKey(int key) {
28+
this.key = key;
29+
}
30+
public String[] getLowerAlpha() {
31+
return lowerAlpha;
32+
}
33+
34+
public static String encrypt(String origin, int key) {
35+
String encryptContent = "";
36+
37+
for (int i = 0; i < origin.length(); i++) {
38+
char temp = origin.charAt(i);
39+
int charEncrypt = temp + key % 26;
40+
if (temp >= 65 && temp <= 90) {
41+
encryptContent += calcChar(temp, key, 65, 90);
42+
}else if (temp >= 97 && temp <= 122) {
43+
encryptContent += calcChar(temp, key, 97, 122);
44+
} else {
45+
encryptContent += temp;
46+
}
47+
}
48+
return encryptContent;
49+
}
50+
51+
public static String decrypt(String origin, int key) {
52+
String decryptContent = "";
53+
54+
for (int i = 0; i < origin.length(); i++) {
55+
char temp = origin.charAt(i);
56+
int charEncrypt = temp - key % 26;
57+
58+
if (temp >= 65 && temp <= 90) {
59+
decryptContent += calcCharDecrypt(temp, key, 65, 90);
60+
}else if (temp >= 97 && temp <= 122) {
61+
decryptContent += calcCharDecrypt(temp, key, 97, 122);
62+
} else {
63+
decryptContent += temp;
64+
}
65+
}
66+
return decryptContent;
67+
}
68+
69+
public String encrypt() {
70+
String encryptContent = "";
71+
72+
for (int i = 0; i < originString.length(); i++) {
73+
char temp = originString.charAt(i);
74+
int charEncrypt = temp + key % 26;
75+
if (temp >= 65 && temp <= 90) {
76+
encryptContent += calcChar(temp, key, 65, 90);
77+
}else if (temp >= 97 && temp <= 122) {
78+
encryptContent += calcChar(temp, key, 97, 122);
79+
} else {
80+
encryptContent += temp;
81+
}
82+
}
83+
return encryptContent;
84+
}
85+
86+
public String decrypt() {
87+
String decryptContent = "";
88+
89+
for (int i = 0; i < originString.length(); i++) {
90+
char temp = originString.charAt(i);
91+
int charEncrypt = temp - key % 26;
92+
93+
if (temp >= 65 && temp <= 90) {
94+
decryptContent += calcCharDecrypt(temp, key, 65, 90);
95+
}else if (temp >= 97 && temp <= 122) {
96+
decryptContent += calcCharDecrypt(temp, key, 97, 122);
97+
} else {
98+
decryptContent += temp;
99+
}
100+
}
101+
return decryptContent;
102+
}
103+
104+
public static char calcChar (int charOrigin, int indexKey, int start, int end) {
105+
int charEncrypt = charOrigin + indexKey % 26;
106+
107+
if (charEncrypt <= end) {
108+
return (char) (charEncrypt);
109+
}
110+
return (char) (start + (charEncrypt - end - 1));
111+
}
112+
113+
public static char calcCharDecrypt (int charOrigin, int indexKey, int start, int end) {
114+
int charDecrypt = charOrigin - indexKey % 26;
115+
116+
if (charDecrypt >= start) {
117+
return (char) (charDecrypt);
118+
}
119+
return (char) (end - start + charDecrypt + 1 );
120+
}
121+
122+
123+
@Override
124+
public String toString() {
125+
return "CaesarCipher";
126+
}
127+
128+
129+
130+
131+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package application;
2+
3+
import java.util.Random;
4+
5+
import javax.swing.UIManager;
6+
import javax.swing.UnsupportedLookAndFeelException;
7+
8+
import com.formdev.flatlaf.themes.FlatMacLightLaf;
9+
10+
import gui.SplashLoading;
11+
12+
public class MainApplication {
13+
public static void main(String[] args) {
14+
try {
15+
16+
UIManager.setLookAndFeel(new FlatMacLightLaf());
17+
18+
SplashLoading spl = new SplashLoading();
19+
spl.setLocationRelativeTo(null);
20+
spl.setVisible(true);
21+
22+
spl.processBarUpdate(0, "Đang khởi động");
23+
24+
for (int i = 0; i < 101; i++) {
25+
Thread.sleep(random(10,50));
26+
spl.processBarUpdate(i);
27+
}
28+
spl.dispose();
29+
30+
} catch (Exception e) {
31+
// TODO Auto-generated catch block
32+
e.printStackTrace();
33+
}
34+
35+
}
36+
37+
public static int random(int min, int max) {
38+
39+
// Create an instance of Random
40+
Random random = new Random();
41+
42+
// Generate a random number within the specified range
43+
return random.nextInt((max - min) + 1) + min;
44+
45+
// Print the random number
46+
}
47+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package application;
2+
3+
import java.util.Random;
4+
5+
import javax.swing.UIManager;
6+
import javax.swing.UnsupportedLookAndFeelException;
7+
8+
import com.formdev.flatlaf.themes.FlatMacLightLaf;
9+
10+
import gui.GUI_MainApp;
11+
import gui.SplashLoading;
12+
13+
public class MainApplicationTest {
14+
public static void main(String[] args) {
15+
try {
16+
17+
UIManager.setLookAndFeel(new FlatMacLightLaf());
18+
19+
new GUI_MainApp().setVisible(true);
20+
21+
} catch (Exception e) {
22+
// TODO Auto-generated catch block
23+
e.printStackTrace();
24+
}
25+
26+
}
27+
28+
public static int random(int min, int max) {
29+
30+
// Create an instance of Random
31+
Random random = new Random();
32+
33+
// Generate a random number within the specified range
34+
return random.nextInt((max - min) + 1) + min;
35+
36+
// Print the random number
37+
}
38+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package gui;
2+
3+
import java.awt.GraphicsConfiguration;
4+
import java.awt.HeadlessException;
5+
6+
import javax.swing.JFrame;
7+
8+
public class GUI_MainApp extends JFrame{
9+
10+
public GUI_MainApp() {
11+
super();
12+
setSize(700,500);
13+
setDefaultCloseOperation(EXIT_ON_CLOSE);
14+
setLocationRelativeTo(null);
15+
16+
setTitle("Các thuật toán mã hoá | (C) 2024 Dương Thái Bảo - DHKTPM17B");
17+
18+
19+
}
20+
21+
22+
23+
}

0 commit comments

Comments
 (0)