?? 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 + -