我怎样才能让我的 encode/decoder 只使用某些 ASCII 字符

How can I make it so my encode/decoder only using certain ASCII characters

做一个项目,我需要对输入的密码进行编码和解码。我的问题是,我怎样才能让我的encode/decoder只使用ASCII字符33-122?

我会把我的代码放进去,但我认为这没什么区别

import java.util.Scanner;

public class Password
{
   public static void main(String[] args)
   {
      Scanner kb = new Scanner(System.in);
      Scanner kb1 = new Scanner(System.in);
      
      String password;
      String encryptedPassword;
      int encryptionNumber;
      int passwordLength;
      char cypher;
      char decypher;
      
      System.out.println("Enter password. Has to be 8 characters or longer");
      password = kb.nextLine();
      
      passwordLength = password.length();
      while(passwordLength < 8)
      {
         if(passwordLength >= 8)
         {
            break;
         }
         else
         {
            System.out.println("Your password is currently " + passwordLength + " characters long. The password needs 8 characters at least");
            password = kb.nextLine();
            passwordLength = password.length();
         }
      }
      
      System.out.println("Your password is " + password);
      
      System.out.println("Choose an encrpytion between 1 and 10");
      encryptionNumber = kb.nextInt();
      
      while(encryptionNumber < 1 || encryptionNumber>10)
      {
         if(encryptionNumber>=1 && encryptionNumber<= 10)
         {
            break;
         }
         else
         {
            System.out.println("Choose a valid encryption key that is between 1 and 10");
            encryptionNumber = kb.nextInt();
         }
      }
      System.out.println("You chose encryption " + encryptionNumber);
      
      System.out.print("The encrypted password is: ");
      
      for(int i=0; i < passwordLength; i++)
      {
         cypher = (char)(password.charAt(i) + encryptionNumber);
         System.out.print(cypher);
      }
      System.out.println("");
      
      System.out.println("Now enter the encrypted password so it can be decrypted");
      encryptedPassword = kb1.nextLine();
      
      for(int i=0; i < passwordLength; i++)
      {
         decypher = (char)(encryptedPassword.charAt(i) - encryptionNumber);
         System.out.print(decypher);
      }
      
   }
}

有很多方法可以做到这一点。最好的方法是使用正则表达式来验证您的输入。了解 class Pattern。更简单但不是有效的解决方案是遍历字符串中的每个字符并单独验证它。

以下是我为使其正常工作所做的更改

import java.util.Scanner;

public class Password
{
   public static void main(String[] args)
   {
      Scanner kb = new Scanner(System.in);
      Scanner kb1 = new Scanner(System.in);
      
      String password;
      String encryptedPassword;
      int encryptionNumber;
      int passwordLength;
      char cypher;
      char decypher;
            
      System.out.println("Enter password. Has to be 8 characters or longer");
      password = kb.nextLine();
      
      //Checks to see of the password is greater than 8 characters
      passwordLength = password.length();
      while(passwordLength < 8)
      {
         if(passwordLength >= 8)
         {
            break;
         }
         else
         {
            System.out.println("Your password is currently " + passwordLength + " characters long. The password needs 8 characters at least");
            password = kb.nextLine();
            passwordLength = password.length();
         }
      }
      
      System.out.println("Your password is " + password);
      
      System.out.println("Choose an encrpytion between 1 and 10");
      encryptionNumber = kb.nextInt();
      
      while(encryptionNumber < 1 || encryptionNumber>10)
      {
         if(encryptionNumber>=1 && encryptionNumber<= 10)
         {
            break;
         }
         else
         {
            System.out.println("Choose a valid encryption key that is between 1 and 10");
            encryptionNumber = kb.nextInt();
         }
      }
      System.out.println("You chose encryption " + encryptionNumber);
      
      //Encrypts the password to give the user a new encrypted password
      System.out.print("The encrypted password is: ");
      
      for(int i=0; i < passwordLength; i++)
      {
         cypher = (char)(password.charAt(i) + encryptionNumber);
         if ((password.charAt(i) + encryptionNumber)>122)
         {
            cypher = (char)(((password.charAt(i) + encryptionNumber) - 122) + 32);
         }
         System.out.print(cypher);
      }
      System.out.println("");
      
      //Used to decrypt the password
      System.out.println("Now enter the encrypted password so it can be decrypted");
      encryptedPassword = kb1.nextLine();
      
      for(int i=0; i < passwordLength; i++)
      {
         decypher = (char)(encryptedPassword.charAt(i) - encryptionNumber);
         if ((encryptedPassword.charAt(i) - encryptionNumber)<33)
         {
            decypher = (char)(122 - (32 - (encryptedPassword.charAt(i) - encryptionNumber)));
         }
         System.out.print(decypher);
      }      
   }
}