Java 加密和解密文本时出现问题
我的代码有问题。我编写代码是为了做两件事:加密文本和解密文本。我的代码加密正确,但解密方法不起作用。我做了一些更改来更正解密方法,但结果是加密和解密都不起作用。我试图更正它,但我不能。帮助我纠正方法并使其正确 头等舱Java 加密和解密文本时出现问题,java,encryption,Java,Encryption,我的代码有问题。我编写代码是为了做两件事:加密文本和解密文本。我的代码加密正确,但解密方法不起作用。我做了一些更改来更正解密方法,但结果是加密和解密都不起作用。我试图更正它,但我不能。帮助我纠正方法并使其正确 头等舱 package so4717814; public class text { public static final int AlphaSize = 26; public static final char[] alpha = { // 'a', 'b',
package so4717814;
public class text {
public static final int AlphaSize = 26;
public static final char[] alpha = { //
'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', //
};
protected char[] encrypt = new char[AlphaSize];
protected char[] decrypt = new char[AlphaSize];
public text() {
for (int i = 0; i < AlphaSize; i++)
encrypt[i] = alpha[(i + 3) % AlphaSize];
for (int i = 0; i < AlphaSize; i++)
decrypt[encrypt[i] - 'a'] = alpha[i];
}
public String Encryption(String secret) {
char[] mess = secret.toCharArray();
for (int i = 0; i < mess.length; i++)
if (Character.isUpperCase(mess[i]))
mess[i] = encrypt[mess[i] - 'a'];
return new String(mess);
}
public String decryption(String secret) {
char[] mess = secret.toCharArray();
for (int i = 0; i < mess.length; i++)
if (Character.isUpperCase(mess[i]))
mess[i] = decrypt[mess[i] - 'a'];
return new String(mess);
}
}
我认为没有任何变化的原因是加密循环只对大写字母应用加密转换。如果值是小写字母、数字、标点符号等,则永远不会更改mess数组中的字符。要解决此问题,请尝试更改加密逻辑以处理所有类型的字符,而不仅仅是大写字符。请使用4个空格缩进的代码正确格式化您的问题!这样读很糟糕。什么东西实际上“不起作用”?我把空格放进去了。我希望易于阅读。当我选择加密或解密文本时,没有任何变化。句子按原样打印。为什么正确格式化代码如此困难?只需将其导入Eclipse,按Ctrl-Shift-F,您就完成了。谢谢templatetypedef先生。我把大写字母改为小写字母,这样就可以正确地处理小写字母了,现在就足够了。非常感谢你的比赛。
package so4717814;
import java.util.Scanner;
public class TextApp {
public static void main(String[] args) {
text T1 = new text();
System.out.println();
System.out.print("\n ~WELCOME~\n");
Scanner scanner = new Scanner(System.in);
System.out.printf("\nPlease choose one:\n\n1-%s\n\n2-%s\n", "Encrypt Message", "Decrypt message");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("\nEnter your Message :\n ");
String secret = scanner.next();
System.out.printf("\n The Encryption is :\n");
secret = T1.Encryption(secret);
System.out.println();
System.out.println(secret);
System.out.println();
break;
case 2:
System.out.println("\nEnter your message :\n ");
String message = scanner.next();
System.out.printf("\n The Decryption is :\n");
message = T1.decryption(message);
System.out.println();
System.out.println(message);
System.out.println();
break;
}
System.out.println("\n\tThank you for useing my program\t\n\t\t ;)\n\n");
}
}