如何在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”将Javachar
代码0-255映射到字节0-255,并且不丢失字节0-255,因为Unicode部分基于8859-1
- 在输入时,读取(到)一个
,然后字节[]
,其中字符集是名称新字符串(字节,字符集)
或者该名称的“ISO-8859-1”
对象,可作为java.nio.charset.charset
获得; 或者使用该字符集名称或对象在从缓冲区读取字节或直接从文件读取字节的流上创建java.nio.charset.StandardCharSets.ISO_8859_1
,并从InputStreamReader
读取器读取字符和/或
字符串
- 在输出时使用
,其中charset是该字符集名称或对象,并写入String.getBytes(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
}