1、对输入的字符串进行加解密,并输出。
2加密方法为:
当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B;字母Z时则替换为a;
当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0;
其他字符不做变化。
3、解密方法为加密的逆过程。
接口描述:
实现接口,每个接口实现1个基本操作:
void Encrypt (char aucPassword[], char aucResult[]):在该函数中实现字符串加密并输出
说明:
1、字符串以\0结尾。
2、字符串最长100个字符。
int unEncrypt (char result[], char password[]):在该函数中实现字符串解密并输出
说明:
1、字符串以\0结尾。
2、字符串最长100个字符。
public class StrEnAndDecryption {
public static final String str1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
public static final String str2 = "BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza1234567890";
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String strEncrypt = sc.nextLine();//待加密字符串
String strUnEncrypt = sc.nextLine();//待解密字符串
char[] aucPassword = strEncrypt.toCharArray();
char[] password = strUnEncrypt.toCharArray();
Encrypt(aucPassword);
unEncrypt(password);
}
}
//字符串加密
public static void Encrypt(char aucPassword[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i< aucPassword.length;i++) {
for (int j =0; j< str1.length();j++){
char c = str1.charAt(j);
if (c == aucPassword[i]){
sb.append(str2.charAt(j));
}
}
}
System.out.println(sb.toString());
}
//字符串解密
public static void unEncrypt(char password[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < password.length; i++){
for (int j =0; j<str2.length(); j++){
char c = str2.charAt(j);
if (c == password[i]) {
sb.append(str1.charAt(i));
}
}
}
System.out.println(sb.toString());
}
}