我怎样才能使我的编码/解码器只使用某些ASCII字符

时间:2016-02-24 15:21:01

标签: java encoding decode encode

做一个项目,我需要对输入的密码进行编码和解码。我的问题是,如何才能使我的编码/解码器只使用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);
      }
      
   }
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

这是我为改变它而改变的方式

&#13;
&#13;
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);
      }      
   }
}
&#13;
&#13;
&#13;