不能将凯撒密码移动超过 1
Cant shift Caesar Cipher by more than 1
我是 Java 的新手,我必须编写一个凯撒密码程序。这是我的代码。我遇到的问题是我不能将它移动超过 1。例如,如果我写 'help me' 并输入 2 的移动值,它仍然只移动 1 --> 'ifmq nf'
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str;
String key;
int keyLength;
System.out.println("Enter message:");
str=sc.nextLine();
System.out.println("Enter encryption key:");
key=sc.next();
keyLength=key.length();
//This for loop is repeated use of 'Enrypt' and 'Decrypt' options
for(;;)
{
System.out.println("1.Encrypt\n2.Decrypt\n3.Exit...");
int choice=sc.nextInt();
switch(choice)
{
case 1:
/*send input string keyLength to encrypt() method to encrypt it returns 'Encrypted' string*/
System.out.println("Encrypted message..."+encrypt(str,keyLength));
break;
case 2:
//send retrived string from encrypt() method and keyLength to decrypt() method it returns 'Decrypted' string
System.out.println("Decryptedmessage..."+decrypt(encrypt(str,keyLength),keyLength));
break;
case 3:
//exit from the program
System.exit(0);
break;
default:
System.out.println("Invalid option..");
}
}
}
public static String encrypt(String str,int keyLength)
{
String encrypted="";
for(int i=0;i<str.length();i++)
{
//stores ascii value of character in the string at index 'i'
int c=str.charAt(i);
//encryption logic for uppercase letters
if(Character.isUpperCase(c))
{
c=c+(keyLength%26);
//if c value exceeds the ascii value of 'Z' reduce it by subtracting 26(no.of alphabets) to keep in boundaries of ascii values of 'A' and 'Z'
if(c>'Z')
c=c-26;
}
//encryption logic for lowercase letters
else if(Character.isLowerCase(c))
{
c=c+(keyLength%26);
//if c value exceeds the ascii value of 'z' reduce it by subtracting 26(no.of alphabets) to keep in boundaries of ascii values of 'a' and 'z'
if(c>'z')
c=c-26;
}
//concatinate the encrypted characters/strings
encrypted=encrypted+(char) c;
}
return encrypted;
}
public static String decrypt(String str,int keyLength)
{
String decrypted="";
for(int i=0;i<str.length();i++)
{
//stores ascii value of character in the string at index 'i'
int c=str.charAt(i);
//decryption logic for uppercase letters
if(Character.isUpperCase(c))
{
c=c-(keyLength%26);
//if c value deceed the ascii value of 'A' increase it by adding 26(no.of alphabets) to keep in boundaries of ascii values of 'A' and 'Z'
if(c<'A')
c=c+26;
}
//decryption logic for uppercase letters
else if(Character.isLowerCase(c))
{
c=c-(keyLength%26);
//if c value deceed the ascii value of 'A' increase it by adding 26(no.of alphabets) to keep in boundaries of ascii values of 'A' and 'Z'
if(c<'a')
c=c+26;
}
//concatinate the decrypted characters/strings
decrypted=decrypted+(char) c;
}
return decrypted;
}
正如 Scary Wombat 和 MadProgrammer 所说,在第 12 行你有 keyLength=key.length();
。这个returns你输入的String
的length。所以如果你输入2,keyLength
就是1。如果你输入11, keyLength
是 2.
我会完全摆脱 keyLength
,将 key
设为 int
而不是 String
,然后设置 key=sc.nextInt();
这将允许 1 为 1,2 为 2,依此类推。
记得将 keyLength
的所有实例更改为 key
我是 Java 的新手,我必须编写一个凯撒密码程序。这是我的代码。我遇到的问题是我不能将它移动超过 1。例如,如果我写 'help me' 并输入 2 的移动值,它仍然只移动 1 --> 'ifmq nf'
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str;
String key;
int keyLength;
System.out.println("Enter message:");
str=sc.nextLine();
System.out.println("Enter encryption key:");
key=sc.next();
keyLength=key.length();
//This for loop is repeated use of 'Enrypt' and 'Decrypt' options
for(;;)
{
System.out.println("1.Encrypt\n2.Decrypt\n3.Exit...");
int choice=sc.nextInt();
switch(choice)
{
case 1:
/*send input string keyLength to encrypt() method to encrypt it returns 'Encrypted' string*/
System.out.println("Encrypted message..."+encrypt(str,keyLength));
break;
case 2:
//send retrived string from encrypt() method and keyLength to decrypt() method it returns 'Decrypted' string
System.out.println("Decryptedmessage..."+decrypt(encrypt(str,keyLength),keyLength));
break;
case 3:
//exit from the program
System.exit(0);
break;
default:
System.out.println("Invalid option..");
}
}
}
public static String encrypt(String str,int keyLength)
{
String encrypted="";
for(int i=0;i<str.length();i++)
{
//stores ascii value of character in the string at index 'i'
int c=str.charAt(i);
//encryption logic for uppercase letters
if(Character.isUpperCase(c))
{
c=c+(keyLength%26);
//if c value exceeds the ascii value of 'Z' reduce it by subtracting 26(no.of alphabets) to keep in boundaries of ascii values of 'A' and 'Z'
if(c>'Z')
c=c-26;
}
//encryption logic for lowercase letters
else if(Character.isLowerCase(c))
{
c=c+(keyLength%26);
//if c value exceeds the ascii value of 'z' reduce it by subtracting 26(no.of alphabets) to keep in boundaries of ascii values of 'a' and 'z'
if(c>'z')
c=c-26;
}
//concatinate the encrypted characters/strings
encrypted=encrypted+(char) c;
}
return encrypted;
}
public static String decrypt(String str,int keyLength)
{
String decrypted="";
for(int i=0;i<str.length();i++)
{
//stores ascii value of character in the string at index 'i'
int c=str.charAt(i);
//decryption logic for uppercase letters
if(Character.isUpperCase(c))
{
c=c-(keyLength%26);
//if c value deceed the ascii value of 'A' increase it by adding 26(no.of alphabets) to keep in boundaries of ascii values of 'A' and 'Z'
if(c<'A')
c=c+26;
}
//decryption logic for uppercase letters
else if(Character.isLowerCase(c))
{
c=c-(keyLength%26);
//if c value deceed the ascii value of 'A' increase it by adding 26(no.of alphabets) to keep in boundaries of ascii values of 'A' and 'Z'
if(c<'a')
c=c+26;
}
//concatinate the decrypted characters/strings
decrypted=decrypted+(char) c;
}
return decrypted;
}
正如 Scary Wombat 和 MadProgrammer 所说,在第 12 行你有 keyLength=key.length();
。这个returns你输入的String
的length。所以如果你输入2,keyLength
就是1。如果你输入11, keyLength
是 2.
我会完全摆脱 keyLength
,将 key
设为 int
而不是 String
,然后设置 key=sc.nextInt();
这将允许 1 为 1,2 为 2,依此类推。
记得将 keyLength
的所有实例更改为 key