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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
简要介绍<br>JAVA CLASS文件加密工具是一款专门为保护您的JAVA源代码而设计的软件。传统的JAVA代码保护方式通常是扰乱生成的CLASS文件,从而降低反编译生成的源代码的可读性;有的保护工具甚至能生成部分废代码(垃圾代码),使得反编译后的源码在重新编译时出现编译错误,不能直接生成CLASS文件。这种方法具有一定的效果,但是不能彻底保护您的JAVA源代码。 <br><br>JAVA CLASS文件加密工具对CLASS文件进行加密保护,加密密钥高达256位(bit,即:32字节),并采用多重加密的算法,既安全又高效。加密后的CLASS文件不可能被破解;反编译工具对加密后的CLASS文件无能为力,根本就不能处理! <br><br>运行方式<br>运行时,加密后的CLASS文件要能正常加载,需要使用我们提供的动态库hidea.dll。执行java时带上参数<br>-agentlib:<动态库文件所在路径>\hidea<br>注意:不要加文件后缀.dll,直接使用文件的名字部分(classloader)!<br><br>举例说明:例如,本加密工具安装在c:\hideasoft\java_protect,执行加密后的CLASS文件的命令行如下:<br>java -agentlib:c:\hideasoft\java_protect\hidea <您的CLASS类及参数><br>应用场合<br>独立的应用程序(Application,自定义main方法),运行java时,带上参数-agentlib:<所在路径>\hidea <br>Tomcat等JAVA Web Server,修改启动脚本,把执行java的命令行加上参数-agentlib:<所在路径>\hidea <br>JBOSS等JAVA Application Server(应用服务器),修改启动脚本,把执行java的命令行加上参数-agentlib:<所在路径>\hidea <br>适用环境操作系统:Windows 98/2000/XP 等Windows系统 <br>JDK:1.5.0及以上版本
好的,我先给您提供一个简单的Java加密程序,并对其逐行解释: ```java import java.io.*; public class SimpleEncryption { public static void main(String[] args) { String key = ""; // 1)读入密钥的key是一个字符串; try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); key = br.readLine(); // 从标准输入流中读入一个密钥串 br.close(); } catch (IOException e) { e.printStackTrace(); } File file1 = new File("file1.txt"); File file2 = new File("file2.txt"); try (FileReader fr = new FileReader(file1); FileWriter fw = new FileWriter(file2)) { char[] buffer = new char[1024]; int len; int i = 0; while ((len = fr.read(buffer)) != -1) { for (int j = 0; j < len; j++) { if (key.length() == 0) { fw.write(buffer[j]); // 如果key为空,则不作加密 } else { fw.write(buffer[j] ^ key.charAt(i)); // 明文中字符c的密文为c^key[i] i = (i + 1) % key.length(); // 循环使用key中的各个字符,直至处理完全部明文 } } } fw.flush(); } catch (IOException e) { e.printStackTrace(); } } } ``` 程序使用了 Java 的 IO 流读取文件和输出文件,其中使用了 BufferedReader 从标准输入流中读入一个密钥串,使用其对当前目录下的文件1.txt进行加密,将文件密文输出到文件2.txt。 具体来说,程序分为以下几步: 1. 定义一个空字符串 `key`,用于存储从标准输入流中读入的密钥串。 2. 使用 `BufferedReader` 从标准输入流中读入一个密钥串,并存储到 `key` 变量中。 3. 定义 `file1` 和 `file2` 两个文件对象,分别代表当前目录下的文件1.txt和文件2.txt。 4. 使用 `FileReader` 读取文件1.txt的内容,使用 `FileWriter` 将加密后的内容写入文件2.txt中。 5. 定义一个长度为 1024 的字符数组 `buffer`,用于存储每次从文件1.txt中读入的内容。 6. 使用 `while` 循环读取文件1.txt中的内容,直到读取到文件末尾为止。 7. 在每次读取的内容中,使用 `for` 循环逐个处理字符,如果 `key` 为空,则不作加密,直接将明文写入文件2.txt中;否则,对明文进行加密,并将密文写入文件2.txt中。 8. 在加密过程中,使用位运算符 `^` 对明文字符和 `key` 中的某个字符进行异或运算,得到密文字符。 9. 为了循环使用 `key` 中的各个字符,使用变量 `i` 记录当前使用的字符的下标,每次处理完一个明文字符后,将 `i` 的值加1,如果 `i` 的值等于 `key` 的长度,则将 `i` 的值设为0。 10. 使用 `fw.flush()` 将加密后的内容写入文件2.txt中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值