简体   繁体   English

Java加密txt文件

[英]Java Encryption txt file

I am trying to write a encryption program that will encrypt a file and make an new txt file with the encrypted text.我正在尝试编写一个加密程序,该程序将加密文件并使用加密文本创建一个新的 txt 文件。

I want to write "Happy 30th Birthday" and encrypt that.我想写“30 岁生日快乐”并加密。 The encrypted text should be "8 1 16 16 25 Th Z 20 8 2 9 18 20 8 4 1 25"密文应该是“8 1 16 16 25 Th Z 20 8 2 9 18 20 8 4 1 25”

They key is that each letter is replaced with corresponding number (a=1, b=2,.., z=26), and numbers are to be replaced by the first letter of the number's name.它们的关键是每个字母都替换为相应的数字(a=1, b=2,.., z=26),并且数字将替换为数字名称的第一个字母。

At this point I got a code that encrypts any text to the same characters.在这一点上,我得到了一个代码,可以将任何文本加密为相同的字符。

Below is what I've got at this moment.以下是我目前所拥有的。 Please help me with this, I have no idea how to make it work.请帮我解决这个问题,我不知道如何使它工作。

import java.util.Scanner;
import java.io.PrintWriter;
import java.io.IOException;
public class CaesarCipher
{
   private int key;

   public CaesarCipher(int key)
   {
      this.key = key;
   }

   public char encrypt(char ch)
   {
      return (char)(ch + key);
   }

   public void encryptFile(Scanner in, PrintWriter out)
      throws IOException
   {

      while(in.hasNextLine())
      {
         String line = in.nextLine();
         for(int i = 0; i < line.length(); i++)
         {
            char ch1 = line.charAt(i);
            char ch2 = encrypt(ch1);
            System.out.print(ch1);
            out.print(ch2);
         }
         if(in.hasNextLine())
            out.println();
      }
   }
}

Below is the tester that reads the file and creates an encrypted one:下面是读取文件并创建加密文件的测试程序:

import java.util.Scanner;
import java.io.*;

public class CipherTester
{
   public static void main(String[] args)
   {
      try
      {
         File poem = new File("text.txt");
         Scanner in = new Scanner(poem);

         PrintWriter out = new PrintWriter("textencrypted.txt");

         CaesarCipher cipher = new CaesarCipher(3);
         cipher.encryptFile(in, out);

         //poem.close();
         in.close();
         out.close();
      }
      catch(IOException e)
      {
         System.out.print(e);
      }

   }
}

Thanks谢谢

You will want your encrypt method to look something like this:你会希望你的encrypt方法看起来像这样:

public String encrypt (char ch)
{
    ch = Character.toLowerCase(ch);

    // if it's a letter return the index in the alphabet (a=1, b=2, ...)
    if (ch >= 'a' && ch <= 'z')
    {
        return String.valueOf(ch - 96);
    }
    // if it'a number, return the first character of the english word of the number.
    // 1='O', 2='T', ...
    else if (ch >= '0' && ch <= '9')
    {
        char ret = ch;
        switch (ch)
        {
            case '0':
                ret = 'Z';
                break;
            case '1':
                ret = 'O';
                break;

            // rest of cases here...
        }

        return String.valueOf(ret);
    }
    // just return the same character back.
    else
    {
        return String.valueOf(ch);
    }
}

In your for loop in the encryptFile method, change the ch2 variable to a String:encryptFile方法的 for 循环中,将ch2变量更改为字符串:

for (int i = 0; i < line.length(); i++)
{
    char ch1 = line.charAt(i);
    String ch2 = encrypt(ch1); // returns a String now          

    out.print(ch2);
}

The good answer already was provided so here is a method for retrieving the letters from numbers without resorting to so many case operators.已经提供了很好的答案,所以这里是一种从数字中检索字母的方法,而无需求助于如此多的case运算符。 Other than this it should be quite a bit faster.除此之外,它应该快得多。

public static char letterOfNumber(int number) {
    final int MIN_NUM = 0;
    final int MAX_NUM = 9;
    final char[] LETTERS = {'Z','O','T','T','F','F','S','S','E','N'};
    if (number < MIN_NUM || number > MAX_NUM) {
        //throw an exception or something
    } else {
        return LETTERS[number];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM