import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class Test{
public static void main(String[] Args){
Test ts= new Test();
ts.interphace();
}
public void interphace(){
char option = 0 ;
Test ts= new Test();
System.out.println("Type E for encryption and D for decryption (In caps)");
try {
option = (char)System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch(option){
case 'E':
ts.encrypt();
break;
case 'D':
ts.decrypt();
break;
default:
System.out.println("Option Invalid");
}
}
public void encrypt(){
//Gives the pcode
Test ts= new Test();
System.out.println("Encrypt method initiated");
System.out.println("Please enter the String to be encrypted");
Scanner sc = new Scanner(System.in);
String toben = sc.next();
System.out.println("Your String: "+ toben);
Random pcodegen = new Random();
int pcode = pcodegen.nextInt(64);
System.out.println("Your pcode is: "+pcode);
char unen[]=toben.toCharArray();
int unenint[]= new int[unen.length];
char en[]= new char[unen.length];
for(int i = 0;i<=unen.length;i++){
unenint[i]=ts.CharToInt(unen[i]);
}
for(int i = 0;i<=unen.length;i++){
if(unenint[i]>64)
unenint[i]-=pcode;
else unenint[i]+=pcode;
}
for(int i = 0;i<=unen.length;i++){
en[i]=ts.IntToChar(unenint[i]);
}
String encrypted = new String(en);
System.out.println("Encrypted String: "+encrypted);
}
public void decrypt(){
//Dont forget to enter the pcode
}
public int CharToInt(char ch){
int s = (int)ch;
return s;
}
public char IntToChar(int i){
char ch = (char)i;
return ch;
}
}
会发生什么是在选择该选项后,加密方法被激活。之后它会要求你输入字符串。 将数组unen中的字符转换为unenint(所以我可以用ASCII形式操作它们)时会出现问题,它会给我那个错误。 我知道代码不是很好,因为我是新手。 请紧急给我解决方案。 在此先感谢。如何解决此错误:java.lang.ArrayIndexOutOfBoundsException:4
在所有的在加密方法循环,改变从'<='的条件到'<' – 2013-02-20 06:23:03