为了方面直接调用!我只给出DES和RSA两个类,我自己的Test类就不贴了!
如果想要告我一声!
或者发到我的邮箱maidi_446136150@tom.com
DES类:
package DES_RSA;
public class DES{
//定义一个整形变量
private static final int MAX=100005;
//初始置换表---65位
private int PI[]={0,
58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7
};
//逆初始置换表--65位
private int PI_1[]={0,
40,8,48,16,56,24,64,32,
39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25
};
//扩充函数E--49位
private int E[]={0,
32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11,12,13,
12,13,14, 15,16,17,
16,17,18, 19,20,21,
20,21,22, 23,24,25,
24,25,26, 27,28,29,
28,29,30, 31,32 ,1
};
//找密钥时的第一次初始置换--从64位筛选到56位--57位
int PC_1[]={0,
57,49,41,33,25,17,9,
1,58,50,42,34,26,18,
10,2,59,51,43,35,27,
19,11,3,60,52,44,36,
63,55,47,39,31,33,15,
7,62,54,46,38,30,22,
14,6,61,53,45,37,29,
21,13,5,28,20,12,4
};
//找密钥时的第二次初始置换--从56位筛选到48位--49位
int PC_2[]={0,
14,17,11,24,1,5,
3,28,15,6,21,10,
23,19,12,4,26,8,
16,7,27,20,13,2,
41,52,31,37,47,55,
30,40,51,45,33,48,
44,49,39,56,34,53,
46,42,50,36,29,32
};
//8个P盒函数4行16列
private int s1[][]={
{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7},
{0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8},
{4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0},
{15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13}};
private int s2[][]={
{15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10},
{3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5},
{0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15},
{13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9}};
private int s3[][]={
{10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8},
{13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1},
{13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7},
{1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12}};
private int s4[][]={
{7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15},
{13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9},
{10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4},
{3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14}};
private int s5[][]={
{2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9},
{14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6},
{4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14},
{11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3}};
private int s6[][]={
{12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11},
{10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8},
{9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6},
{4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13}};
private int s7[][]={
{4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1},
{13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6},
{1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2},
{6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12}};
private int s8[][]={
{13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7},
{1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2},
{7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8},
{2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}};
//保存16个48位的密钥
int key[][]=new int[17][49];;
//原始的64位密钥
int key64[]=new int[65];;
//置换为56位密钥
int key56[]=new int[57];
//的左移的位数--17位
int pos[]={0,1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
//保存明文的数组
int yuan[]=new int[65];
//初始置换后的明文数组
int yuan1[]=new int[65];
//保存左右明文的数组
int left[][]=new int[17][33];
int right[][]=new int[17][33];
//保存最后密文的数组
int miwen[]=new int[65];
//保存最后翻译的明文
int mingwen[]=new int[65];
String str3;//存放加密后的密文
String str4;//存放解密后的明文
//将字符转化为8位的二进制!
public void Change(int a[],char ch){
int i,j,n=(int)ch;
int b[]=new int[10];
b[0]=0;
while(n!=0)
{
b[++b[0]]=n%2;
n/=2;
}
for(i=8,j=1;i>=1&&j<=b[0];i--,j++)
a[i]=b[j];
for(;i>=1;i--)
a[i]=0;
a[0]=8;
}
public void ProduceKey(){
int i,j,k=0;
int c[][]=new int[17][57];
int d[][]=new int[17][57];
//合并为56位密钥
int union56[]=new int[57];
for(i=1;i<=56;i++)
key56[i]=key64[PC_1[i]];
for(i=1;i<=28;i++)
c[0][i]=key56[i];
for(i=29;i<=56;i++)
c[0][i]=key56[i-28];
c[0][0]=56;
for(i=29;i<=56;i++)
d[0][i-28]=key56[i];
for(i=29;i<=56;i++)
d[0][i]=key56[i];
d[0][0]=56;
for(i=1;i<=16;i++)
{
k+=pos[i];
for(j=1;j<=28;j++)
c[i][j]=c[0][k+j];
for(j=1;j<=28;j++)
d[i][j]=d[0][k+j];
for(j=1;j<=28;j++)
union56[j]=c[i][j];
for(j=29;j<=56;j++)
union56[j]=d[i][j-28];
for(j=1;j<=48;j++)
key[i][j]=union56[PC_2[j]];
}
}
//将两位的二进制转为十进制
public int Change2(int a,int b){
int sum=0;
sum=2*sum+a;
sum=2*sum+b;
return sum;
}
//将四位的二进制转为十进制
public int Change4(int a,int b,int c,int d){
int sum=0;
sum=2*sum+a;
sum=2*sum+b;
sum=2*sum+c;
sum=2*sum+d;
return sum;
}
public void ProduceMiWen(){
int i,j,n,m,num;
//保存异或后的中间数据-注意是48位
int zhong[]=new int[49];;
//保存转化为32位的中间数据-注意是32位
int zhong1[]=new int[33];
int b[]=new int[5];
for(i=1;i<=64;i++)
yuan1[i]=yuan[PI[i]];
for(i=1;i<=32;i++)
left[0][i]=yuan1[i];
for(i=33;i<=64;i++)
right[0][i-32]=yuan1[i];
for(i=1;i<=16;i++)
{
for(j=1;j<=32;j++)
left[i][j]=right[i-1][j];//L(i)=R(i-1)
for(j=1;j<=48;j++)
zhong[j]=right[i-1][E[j]];//将32扩充到48位
for(j=1;j<=48;j++)
zhong[j]^=key[i][j];//R(i-1)的48位异或48的密钥
//下面是将48位转化为32位
zhong1[0]=0;
//第一个将6位为转化为4位
n=Change2(zhong[1],zhong[6]);
m=Change4(zhong[2],zhong[3],zhong[4],zhong[5]);
num=s1[n][m];
b[0]=4;
for(j=1;j<=4;j++)
b[j]=0;
while(num!=0)
{
b[b[0]--]=num%2;
num/=2;
}
for(j=1;j<=4;j++)
zhong1[++zhong1[0]]=b[j];
//第二个将6位为转化为4位
n=Change2(zhong[7],zhong[12]);
m=Change4(zhong[8],zhong[9],zhong[10],zhong[11]);
num=s2[n][m];
b[0]=4;
for(j=1;j<=4;j++)
b[j]=0;
while(num!=0)
{
b[b[0]--]=num%2;
num/=2;
}
for(j=1;j<=4;j++)
zhong1[++zhong1[0]]=b[j];
//第三个将6位为转化为4位
n=Change2(zhong[13],zhong[18]);
m=Change4(zhong[14],zhong[15],zhong[16],zhong[17]);
num=s3[n][m];
b[0]=4;
for(j=1;j<=4;j++)
b[j]=0;
while(num!=0)
{
b[b[0]--]=num%2;
num/=2;
}
for(j=1;j<=4;j++)
zhong1[++zhong1[0]]=b[j];
//第四个将6位为转化为4位
n=Change2(zhong[19],zhong[24]);
m=Change4(zhong[20],zhong[21],zhong[22],zhong[23]);
num=s4[n][m];
b[0]=4;
for(j=1;j<=4;j++)
b[j]=0;
while(num!=0)
{
b[b[0]--]=num%2;
num/=2;
}
for(j=1;j<=4;j++)
zhong1[++zhong1[0]]=b[j];
//第五个将6位为转化为4位
n=Change2(zhong[25],zhong[30]);
m=Change4(zhong[26],zhong[27],zhong[28],zhong[29]);
num=s5[n][m];
b[0]=4;
for(j=1;j<=4;j++)
b[j]=0;
while(num!=0)
{
b[b[0]--]=num%2;
num/=2;
}
for(j=1;j<=4;j++)
zhong1[++zhong1[0]]=b[j];
//第六个将6位为转化为4位
n=Change2(zhong[31],zhong[36]);
m=Change4(zhong[32],zhong[33],zhong[34],zhong[35]);
num=s6[n][m];
b[0]=4;
for(j=1;j<=4;j++)
b[j]=0;
while(num!=0)
{
b[b[0]--]=num%2;
num/=2;
}
for(j=1;j<=4;j++)
zhong1[++zhong1[0]]=b[j];
//第七个将6位为转化为4位
n=Change2(zhong[37],zhong[42]);
m=Change4(zhong[38],zhong[39],zhong[40],zhong[41]);
num=s7[n][m];
b[0]=4;
for(j=1;j<=4;j++)
b[j]=0;
while(num!=0)
{
b[b[0]--]=num%2;
num/=2;
}
for(j=1;j<=4;j++)
zhong1[++zhong1[0]]=b[j];
//第八个将6位为转化为4位
n=Change2(zhong[43],zhong[48]);
m=Change4(zhong[44],zhong[45],zhong[46],zhong[47]);
num=s8[n][m];
b[0]=4;
for(j=1;j<=4;j++)
b[j]=0;
while(num!=0)
{
b[b[0]--]=num%2;
num/=2;
}
for(j=1;j<=4;j++)
zhong1[++zhong1[0]]=b[j];
//求得R(i)
for(j=1;j<=32;j++)
right[i][j]=left[i-1][j]^zhong1[j];
}
for(j=1;j<=32;j++)
yuan[j]=right[16][j];
for(j=1;j<=32;j++)
yuan[j+32]=left[16][j];
for(j=1;j<=64;j++)
miwen[j]=yuan[PI_1[j]];
}
public void ProduceMingWen(){
int i,j,n,m,num;
//保存异或后的中间数据-注意是48位
int zhong[]=new int[49];
//保存转化为32位的中间数据-注意是32位
int zhong1[]=new int[33];
int b[]=new int[5];
for(i=1;i<=64;i++)
yuan1[i]=yuan[PI[i]];
for(i=1;i<=32;i++)
left[0][i]=yuan1[i];
for(i=33;i<=64;i++)
right[0][i-32]=yuan1[i];
for(i=1;i<=16;i++)
{
for(j=1;j<=32;j++)
left[i][j]=right[i-1][j];//L(i)=R(i-1)
for(j=1;j<=48;j++)
zhong[j]=right[i-1][E[j]];//将32扩充到48位
for(j=1;j<=48;j++)
zhong[j]^=key[17-i][j];//R(i-1)的48位异或48的密钥
//下面是将48位转化为32位
zhong1[0]=0;
//第一个将6位为转化为4位
n=Change2(zhong[1],zhong[6]);
m=Change4(zhong[2],zhong[3],zhong[4],zhong[5]);
num=s1[n][m];
b[0]=4;
for(j=1;j<=4;j++)
b[j]=0;
while(num!=0)
{
b[b[0]--]=num%2;
num/=2;
}
for(j=1;j<=4;j++)
zhong1[++zhong1[0]]=b[j];
//第二个将6位为转化为4位
n=Change2(zhong[7],zhong[12]);
m=Change4(zhong[8],zhong[9],zhong[10],zhong[11]);
num=s2[n][m];
b[0]=4;
for(j=1;j<=4;j++)
b[j]=0;
while(num!=0)
{
b[b[0]--]=num%2;
num/=2;
}
for(j=1;j<=4;j++)
zhong1[++zhong1[0]]=b[j];
//第三个将6位为转化为4位
n=Change2(zhong[13],zhong[18]);
m=Change4(zhong[14],zhong[15],zhong[16],zhong[17]);
num=s3[n][m];
b[0]=4;
for(j=1;j<=4;j++)
b[j]=0;
while(num!=0)
{
b[b[0]--]=num%2;
num/=2;
}
for(j=1;j<=4;j++)
zhong1[++zhong1[0]]=b[j];
//第四个将6位为转化为4位
n=Change2(zhong[19],zhong[24]);
m=Change4(zhong[20],zhong[21],zhong[22],zhong[23]);
num=s4[n][m];
b[0]=4;
for(j=1;j<=4;j++)
b[j]=0;
while(num!=0)
{
b[b[0]--]=num%2;
num/=2;
}
for(j=1;j<=4;j++)
zhong1[++zhong1[0]]=b[j];
//第五个将6位为转化为4位
n=Change2(zhong[25],zhong[30]);
m=Change4(zhong[26],zhong[27],zhong[28],zhong[29]);
num=s5[n][m];
b[0]=4;
for(j=1;j<=4;j++)
b[j]=0;
while(num!=0)
{
b[b[0]--]=num%2;
num/=2;
}
for(j=1;j<=4;j++)
zhong1[++zhong1[0]]=b[j];
//第六个将6位为转化为4位
n=Change2(zhong[31],zhong[36]);
m=Change4(zhong[32],zhong[33],zhong[34],zhong[35]);
num=s6[n][m];
b[0]=4;
for(j=1;j<=4;j++)
b[j]=0;
while(num!=0)
{
b[b[0]--]=num%2;
num/=2;
}
for(j=1;j<=4;j++)
zhong1[++zhong1[0]]=b[j];
//第七个将6位为转化为4位
n=Change2(zhong[37],zhong[42]);
m=Change4(zhong[38],zhong[39],zhong[40],zhong[41]);
num=s7[n][m];
b[0]=4;
for(j=1;j<=4;j++)
b[j]=0;
while(num!=0)
{
b[b[0]--]=num%2;
num/=2;
}
for(j=1;j<=4;j++)
zhong1[++zhong1[0]]=b[j];
//第八个将6位为转化为4位
n=Change2(zhong[43],zhong[48]);
m=Change4(zhong[44],zhong[45],zhong[46],zhong[47]);
num=s8[n][m];
b[0]=4;
for(j=1;j<=4;j++)
b[j]=0;
while(num!=0)
{
b[b[0]--]=num%2;
num/=2;
}
for(j=1;j<=4;j++)
zhong1[++zhong1[0]]=b[j];
//求得R(i)
for(j=1;j<=32;j++)
right[i][j]=left[i-1][j]^zhong1[j];
}
for(j=1;j<=32;j++)
yuan[j]=right[16][j];
for(j=1;j<=32;j++)
yuan[j+32]=left[16][j];
for(j=1;j<=64;j++)
mingwen[j]=yuan[PI_1[j]];
}
public void Fun(String s1,String s2)
{
int i,j,k,sum;
int a[]=new int[10];
char ch[]=new char[MAX];
int num=0;
int count;
key64[0]=0;
str3="";
str4="";
//将密钥字符转化为二进制
for(i=0;i<s2.length();i++)
{
Change(a,s2.charAt(i));
for(j=1;j<=8;j++)
key64[++key64[0]]=a[j];
}
//产生密钥
ProduceKey();
//剔除字符串中的换行符号!
for(i=0;i<s1.length();i++)
if(s1.charAt(i)!='/n')
ch[++num]=s1.charAt(i);
//保证ch[]存放的是8的倍数!
if(num%8!=0)
{
count=num%8;
for(j=1;j<=8-count;j++)
ch[++num]='.';//不够补上'.'字符
}
//每8个字符进行加密!
for(i=1;i<=num;i+=8)
{
yuan[0]=0;
for(j=i,count=1;count<=8;j++,count++)
{
Change(a,ch[j]);
for(k=1;k<=8;k++)
yuan[++yuan[0]]=a[k];
}
ProduceMiWen();
for(j=1;j<=64;j++)
str3+=miwen[j];
for(j=1;j<=64;j++)
yuan[j]=miwen[j];
ProduceMingWen();
for(j=1;j<=64;j+=8)
{
sum=0;
for(k=j,count=1;count<=8;k++,count++)
sum=2*sum+mingwen[k];
str4+=(char)sum;
}
}
}
//返回加密后的密文
public String returnMiWen(){
return str3;
}
//返回解密后的明文
public String returnMingWen(){
return str4;
}
}