文件转成字节数组,字节数组加密后成乱码的问题(加密大神,编码大神进)

时间:2022-11-05 22:43:11
我要用AES 来实现加密一个文件的整个过程,文件是txt的,全英文的。先把文件取出来变成字节数组。然后,把这个字节数组切成一个一个的block,每个block是16bytes。最后一个block不够16bytes的话,就补齐。然后把每个block变成一个4*4矩阵。
M = { (a0, a4, a8,a12), (a1,a5,a9,a13), (a2,a6,a10,a14),(a3, a7,a11,a15)}
然后在把这每个block(暂且称为M矩阵)进行加密(程序里用这个方法名字表示encryptS(S, W); ),等我加密完成之后,在把整个M矩阵转换成字节数组,存到文件里。
但是,我的程序出现的问题是:切出来的字节数组第一步打印出来没什么问题,但是加密后,在转化为字节数组,强制转化字节数组打印出来, 就出现各种奇怪的符号:/????_????_???z??a?4?(??
N/_?*???a;??_?'v"?_??z??a?0>?a_va?_?4??????????EN____?W_
我是不明白,这个应该是我的加密过程出错了吗?还是编码出问题了?
 如果是加密过程错了,那至少也该打印出正常的英文字符啊。有什么改正办法没?
还有,我的算法也不好,请求高手指正

public File encrypt(File originalFile, byte[] key){
File encryptedFile = new File("encryptedFile.txt");
int len = (int)originalFile.length();
int length;
System.out.println("the length is " + len);
FileInputStream fis = null;
FileOutputStream fos = null;
int numBlk = 0;
// initialize W[0] to W[43] from key[]
byte[][] W = expandKey(initW(key));

//decide length of byte array from file length
if( (len+1) % 16 == 0){
numBlk = len/16;
}else{
numBlk = len/16 + 1;
}
byte[][] S = new byte[4][4];
byte[][] encryptedS = new byte[4][4];

try {
fis = new FileInputStream(originalFile);
fos = new FileOutputStream(encryptedFile);
byte[] orgFileByte = new byte[1024];
byte[] cipherText = new byte[1024];
while((length = fis.read(orgFileByte, 0, orgFileByte.length)) > 0){
//System.out.println("orgfile " + orgFileByte.length);
for(int p=0; p<numBlk; p++){
//assign every 4*4 elements from orgFileByte to state[][] each round, then encrypt state[][]

for(int i=0; i<4; i++){
for(int j=0; j<4; j++){

System.out.print((char)(orgFileByte[16*p + 4*i + j]));

S[j][i] = orgFileByte[16*p + 4*i + j];
}
}
//after previous i and j for loop, we get one complete state[][]
encryptedS = encryptS(S, W);
//System.out.println("the length of encryptedS is " + encryptedS.length);
//save each statematrix to cipherText

for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
cipherText[16*p + 4*i + j] = encryptedS[j][i];
}
}
}
}
System.out.println("cipher text is " );
String s1 = new String(cipherText);
for(int i=0; i<cipherText.length; i++){

System.out.print((char)cipherText[i]);
}
fos.write(cipherText);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e){
System.out.println("System read file, something going wrong!");
} finally{
try {
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return encryptedFile;
}


public byte[][] encryptS(byte[][] S, byte[][] W){

byte[][] sPrime = null;
sPrime = addRoundKey(0, S, W);
for(int i=1; i<=10; i++){
sPrime = shiftRow(subBytes(S));
if(i != 10){
sPrime = mixColumns(sPrime);
}
sPrime = addRoundKey(i,sPrime, W);
}
return sPrime;
}


3 个解决方案

#1


现成的加密算法不用,自己去搞矩阵,搞加密啊。。。

#2


但看那个字符串应该是编码引起的乱码问题

#3


引用 1 楼 huxiweng 的回复:
现成的加密算法不用,自己去搞矩阵,搞加密啊。。。
对啊,要把最底层的东西实现一下,其实是课程的一个项目,要训练训练自己的水平。
如果是编码问题,那该怎么改呢?

#1


现成的加密算法不用,自己去搞矩阵,搞加密啊。。。

#2


但看那个字符串应该是编码引起的乱码问题

#3


引用 1 楼 huxiweng 的回复:
现成的加密算法不用,自己去搞矩阵,搞加密啊。。。
对啊,要把最底层的东西实现一下,其实是课程的一个项目,要训练训练自己的水平。
如果是编码问题,那该怎么改呢?