String encryption and decryption

Keywords: Java

Title Description:
1. Encrypt and decrypt the input string and output.

2. Encryption method:

When the content is an English letter, replace it with the next letter of the English letter.
At the same time, letters change case, such as the letter A is replaced by B; the letter Z is replaced by a;

When the content is a number, add 1 to the number, such as 0 for 1, 1 for 2, 9 for 0;

Other characters do not change.

3. The decryption method is the reverse process of encryption.

Input Description:
Input specification
 Enter a string of passwords to encrypt
 Enter a string of encrypted passwords

Output Description:
Output specification
 Output encrypted characters
 Output decrypted characters
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            String s = in.nextLine();
            String sc = in.nextLine();
            System.out.println(enCryption(s));
            System.out.println(deCryption(sc));
        }
    }
//Character encryption
    public static char encryption(char c){
        if(c >= 'a' && c < 'z'){
            return (char) (c+1-32);
        }else if(c >= 'A' && c < 'Z'){
            return (char)(c+1+32);
        }else if(c >= '0' && c < '9'){
            return (char)(c+1);
        }else if(c == 'z'){
            return 'A';
        }else if(c == 'Z'){
            return 'a';
        }else if(c == '9'){
            return '0';
        }else{
            return c;
        }
    }
//Character decryption
    public static char decryption(char c){
        if(c > 'a' && c <= 'z'){
            return (char) (c-1-32);
        }else if(c > 'A' && c <= 'Z'){
            return (char)(c-1+32);
        }else if(c > '0' && c <= '9'){
            return (char)(c-1);
        }else if(c == 'a'){
            return 'Z';
        }else if(c == 'A'){
            return 'z';
        }else if(c == '0'){
            return '9';
        }else{
            return c;
        }
    }
//Character encryption
    public static String enCryption(String s){
        char[] sc = s.toCharArray();
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < sc.length;i++){
            sb.append(encryption(sc[i]));
        }
        return sb.toString();
    }
//Character decryption
     public static String deCryption(String s){
        char[] sc = s.toCharArray();
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < sc.length;i++){
            sb.append(decryption(sc[i]));
        }
         return sb.toString();
    }
   
   
}

 

Posted by adi on Fri, 18 Oct 2019 11:02:13 -0700