Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 简单加密程序故障_Java_Encryption - Fatal编程技术网

Java 简单加密程序故障

Java 简单加密程序故障,java,encryption,Java,Encryption,我的加密程序应该使用两个字符的密钥(16位,每个字符8位)来加密和解密文本文件。加密和解密的公式由相同的公式给出: Encrypted(M) = M ^ (K||K||...) and Decrypted(M) = M ^ (K||K||...) 其中M是文本文件的内容,^是独占或,K是键。键将被连接,直到其大小与文本文件内容相同 下面的encrypt()方法似乎很有效,但当我尝试使用decrypt()方法时,有些词的效果很好,但大部分都是胡言乱语。除了一些变量命名外,这两种方法是相同的。我真

我的加密程序应该使用两个字符的密钥(16位,每个字符8位)来加密和解密文本文件。加密和解密的公式由相同的公式给出:

Encrypted(M) = M ^ (K||K||...)
and
Decrypted(M) = M ^ (K||K||...)
其中M是文本文件的内容,^是独占或,K是键。键将被连接,直到其大小与文本文件内容相同

下面的encrypt()方法似乎很有效,但当我尝试使用decrypt()方法时,有些词的效果很好,但大部分都是胡言乱语。除了一些变量命名外,这两种方法是相同的。我真的很想得到一些关于问题所在的反馈

  public static void encrypt() throws IOException {
    String path = System.console().readLine("Enter the file to encrypt: ");
    String baseKey = System.console().readLine("\nEnter two characters for the key: ");

    FileReader file_to_read = new FileReader(path);
    BufferedReader buffReader = new BufferedReader(file_to_read);

    String preCrypt = ""; // preCrypt is the file, read as a string, before        encyption.
    String line = "";

    while ((line = buffReader.readLine()) != null) {
      preCrypt = preCrypt + line;
    }

    buffReader.close();

    int numOfBytes = preCrypt.length();

    // If the number of bytes in the file is odd, add a space to the end.
    if(numOfBytes % 2 != 0) {
      preCrypt = preCrypt + " ";
      numOfBytes++;
}

    // Adjust the key so that it is the same length as the file.
    String key = baseKey;
     while (key.length() != numOfBytes)
    {
      key = key + baseKey;
    }

    StringBuilder decrypted = new StringBuilder();
    for(int i = 0; i < numOfBytes; i++) // Perform the encryption.
    {
      decrypted.append((char)(preCrypt.charAt(i) ^ key.charAt(i)));
    }

    PrintWriter out = new PrintWriter(path); // Write changes to file.
    out.println(decrypted.toString());
    out.close();
    }

  public static void decrypt() throws IOException {
    String path = System.console().readLine("Enter the file to decrypt: ");
    String baseKey = System.console().readLine("\nEnter two characters for the key: ");

    FileReader file_to_read = new FileReader(path);
    BufferedReader buffReader = new BufferedReader(file_to_read);

    String preCrypt = ""; // preCrypt is the file, read as a string, before decyption.
    String line = "";

    while ((line = buffReader.readLine()) != null) {
     preCrypt = preCrypt + line;
   }

    buffReader.close();

    int numOfBytes = preCrypt.length();

    // If the number of bytes in the file is odd, add a space to the end.
    if(numOfBytes % 2 != 0) {
      preCrypt = preCrypt + " ";
      numOfBytes++;
    }

    // Adjust the key so that it is the same length as the file.
    String key = baseKey;
    while (key.length() != numOfBytes)
    {
      key = key + baseKey;
    }

    StringBuilder encrypted = new StringBuilder();
    for(int i = 0; i < numOfBytes; i++)// Perform the decryption.
    {
      encrypted.append((char)(preCrypt.charAt(i) ^ key.charAt(i)));
    }

    PrintWriter out = new PrintWriter(path); // Write changes to the file.
    out.println(encrypted.toString());
    out.close();
  }
public static void encrypt()引发IOException{
String path=System.console().readLine(“输入要加密的文件:”);
字符串baseKey=System.console().readLine(“\n输入两个字符作为键:”);
FileReader file_to_read=新的FileReader(路径);
BufferedReader buffReader=新的BufferedReader(从文件到读取);
String preCrypt=“”;//preCrypt是加密之前的文件,读取为字符串。
字符串行=”;
而((line=buffReader.readLine())!=null){
preCrypt=preCrypt+线;
}
buffReader.close();
int numobytes=preCrypt.length();
//如果文件中的字节数为奇数,请在末尾添加一个空格。
如果(numobytes%2!=0){
preCrypt=preCrypt+“”;
numobytes++;
}
//调整关键点,使其与文件长度相同。
字符串键=基本键;
while(key.length()!=numobytes)
{
键=键+基本键;
}
StringBuilder decrypted=新StringBuilder();
对于(int i=0;i
使用UTF-8编码将输入字符数据转换为字节。密钥应为两个字节,而不是两个字符。密码应该是一个字节序列,而不是字符序列。这可能是因为这里的字节/字符混淆。不能安全地对字符进行异或运算。使用
InputStream
OutputStream
,而不是
Reader
Writer
将文件视为字节流。由于加密和解密的功能完全相同,您应该只有一个功能。谢谢。根据everyones的评论,我对代码进行了彻底的修改,使其具有一个使用ByteArrayInputStream/ByteArrayOutputStream的方法和一个UTF-8字节数组。它似乎也能更快地工作和执行。