ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java加密程序

2021-11-10 09:35:27  阅读:167  来源: 互联网

标签:加密 String hint encryB 程序 System sc Java out


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();
            }
        }
    }
}


标签:加密,String,hint,encryB,程序,System,sc,Java,out
来源: https://blog.csdn.net/vroland2010/article/details/121241464

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有