Java教程

Java加密程序

本文主要是介绍Java加密程序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package mypack;

import java.util.*;
/**
  @author qile_0
 * @create 2021-11-8
 * ******************
 */
public class Encry{
    public static void main (String[] args){
        Scanner sc = new Scanner(System.in);
        sc.useDelimiter("\n");
        String hint = "****************This is QILE encryption app. "+"Type 'help' for more information"+"****************\n"+"Your option:";
        while(true) {
            System.out.print(hint);
            String response = sc.next();
            System.out.println();
            if (response.equals("encry")) {
                System.out.println("***********");
                System.out.print("Enter the encryption code:");
                int encryB = 123;
                try {
                    encryB = sc.nextInt();
                    if (encryB <= 0) {
                        try {
                            throw new RuntimeException("Enter a non-negative number!");
                        } catch (RuntimeException e) {
                            System.out.println(e.getMessage());
                            break;
                        }
                    }
                } catch (InputMismatchException e) {
                    System.out.println();
                    System.out.println("Please enter a number!");
                    System.out.println();
                    break;
                }
                System.out.println();
                System.out.print("Enter the text you want to be encrypted:");
                String text = sc.next();
                char[] chars = text.toCharArray();
                for (int i = 0; i < chars.length; i++) {
                    chars[i] = (char) (chars[i] ^ encryB);
                }
                System.out.println("Your encrypted text is:");
                System.out.println(chars);
                System.out.println("***********");
            }
            else if (response.equals("code")){
                System.out.println();
                int num = new Random().nextInt(100000) + 1;
                System.out.println("your code is:" + num);
                System.out.println();
            } else if (response.equals("decry")){ System.out.println();
                System.out.print("Enter the encryption code:");
                int encryB1 = 123;
                try {
                    encryB1 = sc.nextInt();
                }catch (InputMismatchException e){
                    System.out.println();
                    System.out.println("Please enter a number!");
                    System.out.println();
                    break;
                }
                System.out.println();
                System.out.print("Enter the encrypted text:");
                String enText = sc.next();
                char[] chars1 = enText.toCharArray();
                for (int j = 0; j < chars1.length; j++) {
                    chars1[j] = (char) (chars1[j] ^ encryB1);
                }
                System.out.println("The decrypted text is:");
                System.out.println(chars1);
                System.out.println("***********");
            }else if (response.equals("help")){
                System.out.println("***********");
                System.out.println("QILE encryption app provides any encryption" +
                        " service you want.Here are some usual commands:");
                System.out.println("encry -- for encrypt a text");
                System.out.println("code -- for generating encryption code randomly");
                System.out.println("decry -- for decrypt a text");
                System.out.println("exit -- for quit the program");
                System.out.println();
                System.out.println("The encryption code(number) is a key, you will"
                        +"l need the key for decrypted and encrypted"
                        +"We suggested You should use a more longer key for safety"
                        +"Thanks very much choosing and using the QILE Encryption App!");
                System.out.println("** If you have a problem,contact us in vwu2021@outlook.com **");
                System.out.println("***********");
            }else if (response.equals("exit")) {
                System.out.println("***********");
                System.out.println("Bye,Thanks for using!");
                System.out.println("***********");
                System.exit(0);
            }
            else {
                System.out.println();
                System.out.println("Not a valid response!");
                System.out.println();
            }
        }
    }
}


这篇关于Java加密程序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!