Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.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中将扩展ASCII字符作为字符串读/写到ANSI编码的文本文件中_Java_Encryption_Arraylist_Ascii - Fatal编程技术网

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

如何在java中将扩展ASCII字符作为字符串读/写到ANSI编码的文本文件中,java,encryption,arraylist,ascii,Java,Encryption,Arraylist,Ascii,这是我的加密程序。主要用于加密文件(文本) 程序的这一部分将列表元素转换为字节[],并将其写入文本文件。不幸的是,我不能提供算法 void printit(List<Integer> prnt, File outputFile) throws IOException { StringBuilder building = new StringBuilder(prnt.size()); for (Integer element : prnt) { int e

这是我的加密程序。主要用于加密文件(文本) 程序的这一部分将
列表
元素转换为
字节[]
,并将其写入文本文件。不幸的是,我不能提供算法

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文件读取的元素不匹配

我将
列表
转换为
字节[]
的方法正确吗?? 还是出了什么问题。我知道java不能打印扩展的ASCII字符,所以我使用了这个。但是,即使这样也失败了。它提供了很多
s 有解决办法吗?? 请帮帮我。。以及如何对其他格式(.png.mp3…等)执行此操作

加密文件的格式可以是任何格式(不必是.enc) 加密部分中的thanxx

 `for (Integer element : prnt)
{
    int elmnt = element;
    //building.append(getascii(elmnt));
    char b = Integer.toString(elmnt).charAt(0);
    building.append(b);
}`

-->这会将int转换为char,如1到“1”和5到“5”

任意数据字节都不能转换为任何字符编码,加密会创建包含所有值0-255的数据字节


如果必须将加密数据转换为字符串格式,标准方法是转换为Base64或十六进制。

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

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

  • 在输入时,读取(到)一个
    字节[]
    ,然后
    新字符串(字节,字符集)
    ,其中字符集是名称
    “ISO-8859-1”
    或者该名称的
    java.nio.charset.charset
    对象,可作为
    java.nio.charset.StandardCharSets.ISO_8859_1
    获得; 或者使用该字符集名称或对象在从缓冲区读取字节或直接从文件读取字节的流上创建
    InputStreamReader
    ,并从
    读取器读取字符和/或
    字符串

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

但是您实际上根本不需要
字符
字符串
字符集
。实际上,您希望将一系列
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
}
void printit(List prnt,File outputFile)引发IOException
{
字节[]输出字节=新字节[prnt.size()];int i=0;
for(整型元素:prnt)outputBytes[i++]=(byte)元素;
FileOutputStream outputStream=新的FileOutputStream(outputFile);
outputStream.write(b);
outputStream.close();
//或将前三行替换为一行
java.nio.file.Files.write(outputFile.toPath(),outputBytes);
}
void getfyle(文件输入文件)引发IOException
{
FileInputStream inputStream=新的FileInputStream(inputFile);
byte[]inputBytes=新字节[(int)inputFile.length()];
inputStream.read(inputBytes);
inputStream.close();
//或者将这四行替换为
byte[]inputBytes=java.nio.file.Files.readAllBytes(inputFile.toPath());
对于(字节b:inputBytes)System.out.println(b&0xFF);
//或者如果你真的想要一个列表而不仅仅是一个打印件
ArrayList=新的ArrayList(inputBytes.length);
for(字节b:inputBytes)list.add(b&0xFF);
//返回列表或存储它或任何东西
}

由于您没有披露加密方法(古典与现代),问题很可能是
String fylenters=新字符串(inputBytes),因为字符与字节不同。我知道如何在不损坏字符串的情况下将字节转换为字符。请使用Base64或Hex之类的编码。
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
}