java - 如何在java中将扩展ASCII字符作为字符串读/写到ANSI编码文本文件中

标签 java encryption arraylist ascii

这是我的加密程序。主要用于加密文件(文本) 这部分程序将 List<Integer> 元素转换为 byte [] 并将其写入文本文件。不幸的是我无法提供算法。

void printit(List<Integer> prnt, File outputFile) throws IOException
{
    StringBuilder building = new StringBuilder(prnt.size());

    for (Integer element : prnt)
 {
     int elmnt = element;
     //building.append(getascii(elmnt));
     building.append((char)elmnt);
 }

    String encryptdtxt=building.toString();
    //System.out.println(encryptdtxt);
    byte [] outputBytes = offo.getBytes();

    FileOutputStream outputStream =new FileOutputStream(outputFile);
    outputStream.write(outputBytes);


    outputStream.close();

}

这是解密程序,解密程序从 .enc 文件获取输入

void getfyle(File inputFile) throws IOException
{
    FileInputStream inputStream = new FileInputStream(inputFile);
    byte[] inputBytes = new byte[(int)inputFile.length()];
    inputStream.read(inputBytes);
    inputStream.close();
    String fylenters = new String(inputBytes);
    for (char a:fylenters.toCharArray())
  {
      usertext.add((int)a);
  }
    for (Integer bk : usertext)
  {
      System.out.println(bk);
  }

}

由于这里使用的方法,在我的算法中需要 List<Integer> byte[] 首先转换为 String,然后转换为 List<Integer>,反之亦然。

加密期间写入文件时的元素与从 .enc 文件中读取的元素不匹配。

我将 List<Integer> 转换为 byte[] 的方法正确吗? 或者还有什么问题吗? 。我确实知道java无法打印扩展的ASCII字符,所以我使用了这个。但是,即使这样也失败了。它给出了很多 ? s 有解决办法吗?? 请帮助我..以及如何处理其他格式(.png.mp3....等)

加密文件的格式可以是任何格式(不必是.enc) 感谢xx

最佳答案

数千种不同的“扩展 ASCII”代码,Java 支持其中大约一百种, 但您必须告诉它使用哪个“字符集”,否则默认值通常会导致数据损坏。

虽然以十六进制或 base64 表示任意“二进制”字节很常见并且通常是必要的, 如果字节将以保留所有 256 个值的方式存储和/或传输,通常称为“8 位干净”, 和File{Input,Output}Stream是的,您可以使用映射 Java char 的“ISO-8859-1”将 0-255 编码到字节 0-255 或从字节 0-255 进行编码不会丢失,因为 Unicode 部分基于 8859-1。

  • 输入时,读入 byte[]然后new String (bytes, charset)其中字符集是名称 "ISO-8859-1"java.nio.charset.Charset该名称的对象,可用作 java.nio.charset.StandardCharSets.ISO_8859_1 ; 或创建一个 InputStreamReader在从缓冲区或直接从文件读取字节的流上,使用该字符集名称或对象,并读取字符和/或 String来自Reader

  • 在输出时,使用 String.getBytes(charset)其中 charset 是字符集名称或对象,并写入 byte[] ; 或创建一个 OutputStreamWriter在流上使用该字符集名称或对象将字节写入缓冲区或文件,并写入字符和/或 StringWriter

但你实际上并不需要 charStringCharset 根本。你实际上想写一系列Integer s 作为字节,并读取一系列字节为 Integer s。所以就这样做:

void printit(List<Integer> prnt, File outputFile) throws IOException
{
    byte[] outputBytes = new byte[prnt.size()]; int i = 0;
    for (Integer element : prnt) outputBytes[i++] = (byte)element;

    FileOutputStream outputStream =new FileOutputStream(outputFile);
    outputStream.write(b);
    outputStream.close();
    // or replace the previous three lines by one
    java.nio.file.Files.write (outputFile.toPath(), outputBytes);
}

void getfyle(File inputFile) throws IOException
{
    FileInputStream inputStream = new FileInputStream(inputFile);
    byte[] inputBytes = new byte[(int)inputFile.length()];
    inputStream.read(inputBytes);
    inputStream.close();
    // or replace those four lines with
    byte[] inputBytes = java.nio.file.Files.readAllBytes (inputFile.toPath());

    for (byte b: inputBytes) System.out.println (b&0xFF);
    // or if you really wanted a list not just a printout
    ArrayList<Integer> list = new ArrayList<Integer>(inputBytes.length);
    for (byte b: inputBytes) list.add (b&0xFF);
    // return list or store it or whatever
}

关于java - 如何在java中将扩展ASCII字符作为字符串读/写到ANSI编码文本文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34472047/

相关文章:

java - 如何等待 AdMob 奖励视频正确加载

java - ArrayList size() 方法导致程序崩溃

java - 如何将 Jsoup 输出存储在 ArrayList 中?

encryption - 如何加密光盘上的 EventStore?

javascript - 使用 Android 从 JavaScript 代码进行 AES 加密

java - 从 Arraylist 中删除整数

java - ProGuard 中类路径前的感叹号有什么作用?

java - 如何添加没有Jar的库

java - 匹配的通配符是严格的,但找不到元素'context :component-scan的声明

java - 读取证书crt文件时公钥长48字节