📄 aes.java
字号:
package com.hanssun.aes;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
public class AES extends AESMap {
/**
* This method is used to encrypt data with AES.
* @param OpenPath the path of the file which you want to encrypt.
* @param SavePath the path to save the encrypted file
* @param m_Key the encrypt key of user.
* @param Nb the length of file blocks(32bits)
* @param Nk the length of key.
* @return the length of data.(bytes)
* @throws IOException
////////////////////////////////////////////////////
功能: AES加密
入口参数:m_Key是用户加密密钥;
fp1是要加密的文件指针;
fp2是加密后保存密文的文件指针;
Nb是加密时明文的分组长度(以32bit为单位);
Nk是密钥的长度(以32bit为单位);
///////////////////////////////////////////////////
*/
public long AES_Encrypt(String OpenPath,String SavePath,String m_Key,int Nb,int Nk)
throws IOException
{
//以二进制读的方式打开要加密的文件;
//以二进制写的方式打开保存密文的文件;
FileInputStream fp1 = new FileInputStream(OpenPath);
FileOutputStream fp2 = new FileOutputStream(SavePath,true);
int Length = fp1.available();//得到要加密的文件的长度;
if(Length==0)return 0;
int leave = Length%(4*Nb); //求剩余的字块的字节数;
long rounds = Length/(4*Nb); //得到整块的加密轮数;
if(leave!=0)rounds++;
long copy_rounds = rounds;
byte[] state = new byte[4*8]; //作为加密时存放要加密的明文块;
byte[] copy = new byte[4*8]; //用来进行短块处理时的缓存区;
int Nr=GetRounds(Nb,Nk); //得到加密的轮数;
KeyExpansion(m_Key,Nb,Nk,Nr); //生成各轮子密钥;
if(copy_rounds==1&&rounds==1)
{
if(leave==0) fp1.read(state,0,4*Nb);//明文的长度恰好等于分组长度;
else
{
fp1.read(state,0,leave);//明文的长度小于八个字符;
for(int i=leave;i<4*Nb;i++)
state[i]=0; //后面用空格补齐;
}
state = Transform(ByteToChar(state),Nb,Nr); //加密变换;
fp2.write(state,0,4*Nb);//将加密后的密文块写入目标文件;
rounds--;
}
else if(copy_rounds>1&&leave!=0)//如果明文的长度大于分组长度且字符数不是分组长度的整数倍
{ //时,需要进行短块处理;
fp1.read(state,0,4*Nb);
state = Transform(ByteToChar(state),Nb,Nr);//先加密最前面的一块;
fp2.write(state,0,leave);//仅将余数个字符存入文件,而将后部分密文
//与后面的明文合在一起加密;
int j=0;
for(int i=leave;i<4*Nb;i++)
copy[j++]=state[i];
fp1.read(copy,j,leave);
copy = Transform(ByteToChar(copy),Nb,Nr);
fp2.write(copy,0,4*Nb);
rounds-=2;
}
while(rounds>0)//以下处理的明文是分组的整数倍的情况;
{
fp1.read(state,0,4*Nb);
state = Transform(ByteToChar(state),Nb,Nr);
fp2.write(state,0,4*Nb);
rounds--;
}
fp1.close();//关闭源文件和目标文件;
fp2.close();
return ((copy_rounds-1)*4*Nb+leave);//返回文件长度;
}
/**
* This method is used to de-encrypt cryptograph.
* @param OpenPath the path of cryptograph.
* @param SavePath the path to save the de-encrypted file.
* @param m_Key the key to de-encrypt file.
* @param Nb the length of file blocks(32bits)
* @param Nk the length of key.
* @return the length of data.(bytes)
* @throws IOException
////////////////////////////////////////////////////////
功能: 实现AES的解密
入口参数:m_Key是用户加密密钥;
fp1是要解密的文件指针;
fp2是解密后保存明文的文件指针;
Nb是解密时密文的分组长度(以32bit为单位);
Nk是密钥的长度(以32bit为单位);
注意了, 解密时和加密时的分组长度要一致;
/////////////////////////////////////////////////////////
*/
public long AES_DeEncrypt(String OpenPath,String SavePath,String m_Key, int Nb, int Nk)
throws IOException
{
//以二进制读的方式打开要加密的文件;
//以二进制写的方式打开保存密文的文件;
FileInputStream fp1= new FileInputStream(OpenPath);
FileOutputStream fp2= new FileOutputStream(SavePath,true);
int Length = fp1.available();//得到要加密的文件的长度;
if(Length==0)return 0;
int leave=Length%(4*Nb);//求剩余的字块的字节数;
long rounds=Length/(4*Nb);//得到整块的加密轮数;
if(leave!=0)rounds++;
long copy_rounds=rounds;
byte []state = new byte[4*8]; //解密时存放密文块;
int Nr = GetRounds(Nb,Nk); //得到解密时循环轮数;
KeyExpansion(m_Key,Nb,Nk,Nr); //生成各轮子密钥
byte[] copy = new byte[32];
if(leave!=0)//需要进行短块处理
{
fp1.read(copy,0,leave);//先把余数个密文字符保存;
fp1.read(state,0,4*Nb);//读取紧接着的一个密文块;
state = ReTransform(ByteToChar(state),Nb,Nr); //解密;
int j=0;
for(int i=leave;i<4*Nb;i++) //把解密后的明文和前面的余数个合在一起组成一块,
copy[i]=state[j++]; //一起解密;
copy = ReTransform(ByteToChar(copy),Nb,Nr);
//将解密后的明文写入目标文件;
fp2.write(copy,0,4*Nb);
fp2.write(state,j,leave);//将余数个明文写入目标文件;
rounds-=2; //已经完成了两轮解密所以减二;
}
while(rounds>0)//对后面是分组长度的整数倍的密文块解密;
{
fp1.read(state,0,4*Nb);//读取密文块;
copy = ReTransform(ByteToChar(state),Nb,Nr); //解密变换;
fp2.write(copy,0,4*Nb);//将解密后的明文写入目标文件;
rounds--; //轮数减一;
}
fp1.close();//关闭源文件和目标文件;
fp2.close();
return ((copy_rounds-1)*4*Nb+leave);//返回文件长度
}
/**
* This method is used to shift the data in array A.
* @param A
//////////////////////////////////////////////////////
功能:将数组A中的四个字节循环左移一个字节;
//////////////////////////////////////////////////////
*/
public void RotWord(char[]A)
{
char temp;
temp=A[0];
A[0] = A[1];
A[1] = A[2];
A[2] = A[3];
A[3] = temp;
}
/**
* This method is used to do S-replace durying key-expansion.
* @param A
////////////////////////////////////////////////
功能: 密钥扩展的时候进行S盒替换;
入口参数:A是存放四个字节的数组;
////////////////////////////////////////////////
*/
public void SubWord(char []A)
{
for(int i=0;i<4;i++)
A[i]=S_BOX[A[i]];
}
/**
* This method is used to get rounds of encrypt.
* @param Nb the length of file blocks(32bits)
* @param Nk the length of key.
* @return the rounds of encrypt.
//////////////////////////////////////////////////
功能:返回加密的轮数;
入口参数:Nb以32bit为单位的待加密明文的长度;
Nk是以32bit为单位的初始密钥的长度;
返回值:返回加密轮数(Nr);
////////////////////////////////////////////////////
*/
public int GetRounds(int Nb, int Nk)
{
switch(Nb)
{
case 4:switch(Nk)
{
case 4:return 10;
case 6:return 12;
case 8:return 14;
default:return 0;
}
case 6:switch(Nk)
{
case 4:
case 6:return 12;
case 8:return 14;
default:return 0;
}
case 8:switch(Nk)
{
case 4:
case 6:
case 8:return 14;
default:return 0;
}
default:return 0;
}
}
/**
* This method is used to build sub-keys used in each rounds。
* @param m_Key the key of user.
* @param Nb the length of file blocks(32bits)
* @param Nk the length of key.
* @param Nr the rounds of encrypt in each block.
////////////////////////////////////////////////////
入口参数:Nb以32bit为单位的待加密明文的长度;
Nk是以32bit为单位的初始密钥的长度;
Nr是加密的轮数;
m_Key是用户的密钥;
返回值:扩展后的子密钥存放在数组w中;
*/
public void KeyExpansion(String m_Key,int Nb, int Nk, int Nr)
{
int i=0;
for(;i<4;i++)
for(int j=0;j<Nk;j++)
key[i*Nk+j]=m_Key.charAt(i*4+j);
i=0;
while(i<Nk)
{
w[i*4]=key[i*4];
w[i*4+1]=key[i*4+1];
w[i*4+2]=key[i*4+2];
w[i*4+3]=key[i*4+3];
i++;
}
i=Nk;
while(i<Nb*(Nr+1))
{
char []temp = new char[4];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -