这是我的加密程序。主要用于对文件(文本)进行加密。程序的此部分将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);
}
}
由于此处使用的方法,在我的算法中,require先List<Integer>
byte[]
转换为String
,然后转换为List<Integer>
,反之亦然。
加密期间写入文件时的元素与从.enc文件读取的元素不匹配。
是我的转换方法List<Integer>
,以byte[]
正确的?还是其他问题?。我确实知道Java无法打印扩展的ASCII字符,所以我使用了它。但是,即使这样也失败了。它给出了很多?
s是否有解决方案?请帮助我..以及其他格式(.png.mp3 .... etc)的处理方法
加密文件的格式可以是除xx以外的任何格式(不必是.enc)。
有成千上万种不同的“扩展ASCII”代码,而Java支持大约一百种,但是您必须告诉它使用哪个“字符集”,或者默认值通常会导致数据损坏。
而代表任意的“二进制”字节十六进制或Base64较为常见,经常有必要,如果该字节将存储和/或在保留了所有256个值的方式传递,通常被称为“8位干净”,并且File{Input,Output}Stream
做,你可以使用“ ISO-8859-1”可将Javachar
代码0-255映射到字节0-255之间或从字节0-255映射出Java代码,因为Unicode部分基于8859-1。
在输入时,读入a byte[]
,然后new String (bytes, charset)
其中charset是名称 "ISO-8859-1"
或该名称的java.nio.charset.Charset
对象,可以用作java.nio.charset.StandardCharSets.ISO_8859_1
; 或InputStreamReader
在该流上创建一个使用该字符集名称或对象从缓冲区或直接从文件读取字节的流,并String
从中读取char和/或aReader
在输出上,使用String.getBytes(charset)
charset是该charset名称或对象的地方并写出byte[]
; 或创建一个OutputStreamWriter
上的流写入字节的缓冲器或文件,使用该字符集名或对象,和写字符和/或String
到Writer
但你实际上并不需要 char
,并String
与Charset
所有。您实际上想要将一系列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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句