package myLib
/**
* This class is designed to encrypt file using AES
* @author ZhuTao
* Email:
* QQ:
* @version
*/
/**
* 可以把它們做成自己的庫函數
*/
public class AESMap {
public static char [] key;
public static char [] w;//用來存放擴展後的子密鑰;
public static char []Log = {
public static char[] Log_
// S盒置換
public static char [] S_BOX = {
// S盒逆置換
public char [] S_BOX_
public static char []Rcon={
public AESMap()
{
key = new char[
w = new char[
}
}
package myLib
import java
import java
import java
* This class is designed to encrypt file using AES
* @author ZhuTao
* Email:
* QQ:
* @version
*/
/**
*
* 把它們做成自己的庫函數
*/
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(
* @param Nk the length of key
* @return the length of data
* @throws IOException
////////////////////////////////////////////////////
功能
入口參數
fp
fp
Nb是加密時明文的分組長度(以
Nk是密鑰的長度(以
///////////////////////////////////////////////////
*/
public long AES_Encrypt(String OpenPath
throws IOException
{
//以二進制讀的方式打開要加密的文件
//以二進制寫的方式打開保存密文的文件
FileInputStream fp
FileOutputStream fp
int Length = fp
if(Length==
int leave = Length%(
long rounds = Length/(
if(leave!=
long copy_rounds = rounds;
byte[] state = new byte[
byte[] copy = new byte[
int Nr=GetRounds(Nb
KeyExpansion(m_Key
if(copy_rounds==
{
if(leave==
else
{
fp
for(int i=leave;i<
state[i]=
}
state = Transform(ByteToChar(state)
fp
rounds
}
else if(copy_rounds>
{ //時
fp
state = Transform(ByteToChar(state)
fp
//與後面的明文合在一起加密
int j=
for(int i=leave;i<
copy[j++]=state[i];
fp
copy = Transform(ByteToChar(copy)
fp
rounds
}
while(rounds>
{
fp
state = Transform(ByteToChar(state)
fp
rounds
}
fp
fp
return ((copy_rounds
}
/**
* This method is used to de
* @param OpenPath the path of cryptograph
* @param SavePath the path to save the de
* @param m_Key the key to de
* @param Nb the length of file blocks(
* @param Nk the length of key
* @return the length of data
* @throws IOException
////////////////////////////////////////////////////////
功能
入口參數
fp
fp
Nb是解密時密文的分組長度(以
Nk是密鑰的長度(以
注意了
/////////////////////////////////////////////////////////
*/
public long AES_DeEncrypt(String OpenPath
throws IOException
{
//以二進制讀的方式打開要加密的文件
//以二進制寫的方式打開保存密文的文件
FileInputStream fp
FileOutputStream fp
int Length = fp
if(Length==
int leave=Length%(
long rounds=Length/(
if(leave!=
long copy_rounds=rounds;
byte []state = new byte[
int Nr = GetRounds(Nb
KeyExpansion(m_Key
byte[] copy = new byte[
if(leave!=
{
fp
fp
state = ReTransform(ByteToChar(state)
int j=
for(int i=leave;i<
copy[i]=state[j++]; //一起解密
copy = ReTransform(ByteToChar(copy)
//將解密後的明文寫入目標文件
fp
fp
rounds
}
while(rounds>
{
fp
copy = ReTransform(ByteToChar(state)
fp
rounds
}
fp
fp
return ((copy_rounds
}
/**
* This method is used to shift the data in array A
* @param A
//////////////////////////////////////////////////////
功能
//////////////////////////////////////////////////////
*/
public void RotWord(char[]A)
{
char temp;
temp=A[
A[
A[
A[
A[
}
/**
* This method is used to do S
* @param A
////////////////////////////////////////////////
功能
入口參數
////////////////////////////////////////////////
*/
public void SubWord(char []A)
{
for(int i=
A[i]=S_BOX[A[i]];
}
/**
* This method is used to get rounds of encrypt
* @param Nb the length of file blocks(
* @param Nk the length of key
* @return the rounds of encrypt
//////////////////////////////////////////////////
功能
入口參數
Nk是以
返回值
////////////////////////////////////////////////////
*/
public int GetRounds(int Nb
{
switch(Nb)
{
case
{
case
case
case
default:return
}
case
{
case
case
case
default:return
}
case
{
case
case
case
default:return
}
default:return
}
}
/**
* This method is used to build sub
* @param m_Key the key of user
* @param Nb the length of file blocks(
* @param Nk the length of key
* @param Nr the rounds of encrypt in each block
////////////////////////////////////////////////////
入口參數
Nk是以
Nr是加密的輪數
m_Key是用戶的密鑰
返回值
*/
public void KeyExpansion(String m_Key
{
int i=
for(;i<
for(int j=
key[i*Nk+j]=m_Key
i=
while(i<Nk)
{
w[i*
w[i*
w[i*
w[i*
i++;
}
i=Nk;
while(i<Nb*(Nr+
{
char []temp = new char[
temp[
temp[
if((i%Nk)==
{
RotWord(temp);
SubWord(temp);
for(int j=
temp[j]^=Rcon[((i
}
else if(Nk==
SubWord(temp);
w[i*
w[i*
w[i*
w[i*
i++;
}
}
/**
* This method is used to do S
* @param state is a array which stored the data block
* @param Nb the length of data block
///////////////////////////////////////////////////
功能
入口參數
state為明文塊
////////////////////////////////////////////////////
*/
public void SubChar(char []state
{
for(int i=
state[i]=S_BOX[state[i]%
}
/**
* @param state is a array which stored the data block
* @param Nb the length of data block
/////////////////////////////////////////////////////
功能
入口參數
Nb是以
//////////////////////////////////////////////////////
*/
public void ShiftRows(char []state
{
char[] t = new char [
for( int r=
{
for(int c=
for(int c=
state[Nb*r+c]=t[c];
}
}
/**
* This method is used to mix columns
* @param state is a array which stored the data block
* @param Nb the length of data block
//////////////////////////////////////////////////
功能
入口參數
Nb是以
//////////////////////////////////////////////////
*/
public void MixColumns(char[]state
{
int [] t = new int[
for( int c=
{
for(int r=
for(int r=
{
state[Nb*r+c] = (char)(Ffmul(
^t[(r+
}
}
}
/**
* This method is used to get the product of A and B
* @param A first number
* @param B second number
* @return the product of A and B
/////////////////////////////////////////////////////////
功能
/////////////////////////////////////////////////////////
*/
public int Ffmul(int A
{
//查對數表;
if(A==
A = Log[A];
B = Log[B];
A =(A+B)%
//查反對數表;
A = Log_
return A;
}
/**
* This method is used to add round key and data
* @param state is the array which contains the data
* @param Nb length of data block(
* @param round the index of current round
///////////////////////////////////////////////////////////////
功能: 輪密鑰加變換;
入口參數: state明文塊
w為子密鑰
///////////////////////////////////////////////////////////////
*/
public void AddRoundKey(char[]state
{
for(int c=
for(int r=
state[r*Nb+c] = (char)(state[r*Nb+c]^w[round*
}
/**
* This method is used to do exchang durying the proccess of encryption
* @param state is a array which contains the data to encrypt
* @param Nb the length of data block(
* @param Nr the length of user key
* @return the cryptograph
*/
public byte[] Transform(char[]state
{
int round=
AddRoundKey(state
for(;round<Nr;round++)
{
SubChar(state
ShiftRows(state
MixColumns(state
AddRoundKey(state
}
SubChar(state
ShiftRows(state
AddRoundKey(state
return CharToByte(state);
}
/**
* This method is used to do exchang durying the proccess of de
* @param state is a array which contains the cryptograph to de
* @param Nb the length of cryptograph block(
* @param Nr the length of user key
* @return the original text
*/
public byte[] ReTransform(char []state
{
AddRoundKey(state
for(int round=Nr
{
InvShiftRows(state
InvSubint(state
AddRoundKey(state
InvMixColumns(state
}
InvShiftRows(state
InvSubint(state
AddRoundKey(state
return CharToByte(state);
}
/**
* This method is used to do S
* @param state is a array which contains the cryptograph to de
* @param Nb the length of cryptograph block(
* @param Nr the length of user key
/////////////////////////////////////////////////////////////
功能
入口參數
Nb為密文塊的大小
*/
public void InvSubint(char []state
{
for(int i=
state[i] = S_BOX_
}
/**
* This method is used to shift rows durying de
* @param state is a array which contains the cryptograph to de
* @param Nb the length of cryptograph block(
////////////////////////////////////////////////////////////////
功能
入口參數
Nb為密文塊的大小
////////////////////////////////////////////////////////////////
*/
public void InvShiftRows(char[]state
{
char [] t = new char[
for( int r=
{
for(int c=
t[(c+r)%Nb] = state[r*Nb+c];
for(int c=
state[r*Nb+c]=t[c];
}
}
/**
* This method is used to mix columns durying de
* @param state is a array which contains the cryptograph to de
* @param Nb the length of cryptograph block(
//////////////////////////////////////////////////////////////
功能
入口參數
Nb為密文塊的大小
//////////////////////////////////////////////////////////////
*/
public void InvMixColumns(char []state
{
char []t = new char[
for( int c=
{
for(int r=
for(int r=
{
state[Nb*r+c] = (char)(Ffmul(
^Ffmul(
}
}
}
/**
* This method is used to transform a array from byte type to char type
* @param data a byte type array
* @return a char type array
*/
public static char[] ByteToChar(byte[] data)
{
char []A = new char[data
for(int i =
A[i] = (char)data[i];
return A;
}
/**
* This method is used to transform a array from char type to byte type
* @param data a char type array
* @return a byte type array
*/
public static byte[]CharToByte(char[]data)
{
byte[] A = new byte[data
for(int i =
A[i] = (byte)data[i];
return A;
}
}
package myLib
import java
public class TestAes {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto
AES test = new AES();
System
//(String OpenPath
long time = System
try{
test
test
}catch(IOException e){System
long time
time = time
System
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25654.html