-
Notifications
You must be signed in to change notification settings - Fork 2
/
CaesarCipher.java
50 lines (49 loc) · 1.65 KB
/
CaesarCipher.java
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
/**
* @file Class realize Caesar encryption metods.
* @class caesarCipher
* @author Full_droper <full_droper@pm.me>
* @version 0.0.1
*/
public class CaesarCipher {
public static void main(String[] args) {
// String en = encode("Hello World",2);
// String de = decode(en,2);
// System.out.println("Hello World -> "+ en + " -> " + de);
}
/**
* Encode input data by Caesar Cipher.
* @name encode
* @param {string} rawData - raw data for encode.
* @param {number} key - (default 1) count of shift.
* @return {string} The encoded by Caesar Cipher data
*/
public String encode(String rawData,int key) {
char[] data = new char[rawData.length()];
for (int i = 0; i < rawData.length(); i++) {
data[i] = rawData.charAt(i);
}
if(key < 1) key = 1;
for (int i = 0 ; i < data.length; i++) {
data[i] = (char) (( (int) data[i] ) + key);
}
return String.valueOf(data);
}
/**
* Decode input data by Caesar Cipher.
* @name decode
* @param {string} rawData - raw data for decode.
* @param {number} key - (default 1) count of shift.
* @return {string} The decoded by Caesar Cipher data
*/
public String decode(String rawData,int key) {
char[] data = new char[rawData.length()];
for (int i = 0; i < rawData.length(); i++) {
data[i] = rawData.charAt(i);
}
if(key < 1) key = 1;
for (int i = 0 ; i < data.length; i++) {
data[i] = (char) (( (int) data[i] ) - key);
}
return String.valueOf(data);
}
}