package encode;
public class mainClass {
public static void main(String[] args) {
String str = "abc012";
String result, ori;
result = encode(str);
System.out.println(str + "를 암호화 : " + result);
ori = code(result);
System.out.println(result + "를 복호화 : " + ori);
}
// 암호화
static String encode(String src) {
// 암호표
char abcCode[] = { // a ~ z
'`', '~', '!', '@', '#',
'$', '%', '^', '&', '*',
'(', ')', '-', '_', '+',
'=', '|', '[', ']', '{',
'}', ';', ':', ',', '.', '/'
};
char numCode[] = { // 0 ~ 9
'q', 'w', 'e', 'r', 't',
'y', 'u', 'i', 'o', 'p'
};
String resultStr = "";
for (int i = 0; i < src.length(); i++) {
char ch = src.charAt(i);
int n = (int)ch;
// 알파벳의 경우 ( 97 ~ )
if(n >= 97 && n <= 122) {
n = n - 97;
resultStr = resultStr + abcCode[n];
}
// 숫자의 경우 ( 48 ~ )
else {
n = n - 48;
resultStr = resultStr + numCode[n];
}
}
return resultStr;
}
static String code(String src) {
// 암호표
char abcCode[] = { // a ~ z
'`', '~', '!', '@', '#',
'$', '%', '^', '&', '*',
'(', ')', '-', '_', '+',
'=', '|', '[', ']', '{',
'}', ';', ':', ',', '.', '/'
};
char numCode[] = { // 0 ~ 9
'q', 'w', 'e', 'r', 't',
'y', 'u', 'i', 'o', 'p'
};
String oriStr = "";
// 복호화
for (int i = 0; i < src.length(); i++) {
char ch = src.charAt(i);
int n = (int)ch;
int index = 0;
// 알파벳의 경우
if(n >= 97 && n <= 122) {
for (int j = 0; j < numCode.length; j++) {
if(ch == numCode[j]) {
index = j;
break;
}
}
index = index + 48;
oriStr = oriStr + (char)index;
}
// 그 외의 경우 (기호)
else {
for (int j = 0; j < abcCode.length; j++) {
if(ch == abcCode[j]) {
index = j;
break;
}
}
index = index + 97;
oriStr = oriStr + (char)index;
}
}
return oriStr;
}
}