DES:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<windows.h>
#include<time.h>
//函数声明
void en_des();
int** str2hex(char* src);
void KEY(int* keys);
int* bi2c(int* c);
void de_des();
//全局变量
int ct[64];
int K[16][48];
int IP[8][8]={{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}};//初始置换表
int antiIP[8][8]={{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}};//逆初始置换表
int E[48]={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};//扩展置换表
int P[32]={16,7,20,21,29,12,28,17,
1,15,23,26,5,18,31,10,
2,8,24,14,32,27,3,9,
19,13,30,6,22,11,4,25};//置换函数
int PC1[56]={57,49,41,33,25,17,9,
1,58,50,42,32,26,18,
10,2,59,51,43,35,27,
19,11,3,60,52,44,36,
63,55,47,39,31,23,15,
7,62,54,46,38,30,22,
14,6,61,53,45,37,29,
21,13,5,28,20,12,4};//置换选择1
int PC2[48]={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};//置换选择2
int LN[16]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};//循环左移次数
int S[8][64]={{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},
{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},
{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},
{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},
{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},
{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},
{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},
{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}};//S盒
int main(void)
{
//加密
en_des();
//解密
de_des();
return 0;
}
//加密
void en_des()
{
printf("加密系统:\n");
char p[8],kw[8];
int** phex = (int**)malloc(sizeof(int*)*8);
int** kw2 = (int**)malloc(sizeof(int*)*8);
int i,j,k, pb[64], pe[64], ape[64], keys[64];
int* cyp=(int*)malloc(sizeof(int)*8);
printf("请输入八位明文:");
scanf("%s",p);
printf("请输入八位密钥:");
scanf("%s",kw);
printf("即将加密的明文为:%s\n",p);
//明文转二进制
phex=str2hex(p);
printf("二进制明文为:");
k=0;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
pb[k]=phex[i][j];
k++;
}
}
for(k=0;k<64;k++)
{
printf("%d",pb[k]);
if((k+1)%8==0)
{
printf(" ");
}
}
//初始置换
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
pe[i*8+j]=pb[IP[i][j]-1];
}
}
printf("\n初始置换:");
for(k=0;k<64;k++)
{
printf("%d",pe[k]);
if((k+1)%8==0)
{
printf(" ");
}
}
//密钥词转二进制
kw2=str2hex(kw);
k=0;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
keys[k]=kw2[i][j];
k++;
}
}
//密钥生成
KEY(keys);
int cl[32],cr[32];
//左右分裂
for(j=0;j<32;j++)
{
cl[j]=pe[j];
cr[j]=pe[j+32];
}
//轮函数
for(i=0;i<16;i++)
{
int ce[48],cs[8],temp[4],pf[32],pf2[32];
int t2[2],t4[4];
for(j=0;j<48;j++)//扩展置换
{
ce[j]=cr[E[j]-1];
}
for(j=0;j<48;j++)//异或
{
ce[j]=(ce[j]+K[i][j])%2;
//printf("%d",ce[j]);
}
for(j=0;j<8;j++)//S盒代替
{
t2[0]=ce[j*6];
t2[1]=ce[j*6+5];
t4[0]=ce[j*6+1];
t4[1]=ce[j*6+2];
t4[2]=ce[j*6+3];
t4[3]=ce[j*6+4];
cs[j]=S[j][(t2[0]*2+t2[1]*1)*16+(t4[0]*8+t4[1]*4+t4[2]*2+t4[3]*1)];
for(k=3;k>=0;k--)
{
temp[k]=cs[j]%2;
cs[j]/=2;
}
for(k=0;k<4;k++)
{
pf[k+j*4]=temp[k];
//printf("%d",pf[k+j*4]);
}
//printf("\t");
}
for(j=0;j<32;j++)//P置换备份
{
pf2[j]=pf[j];
}
//printf("\n\n");
for(j=0;j<32;j++)//P置换
{
pf[j]=pf2[P[j]-1];
//printf("%d",pf[j]);
}
//printf("\n\n");
for(j=0;j<32;j++)//左右异或
{
pf[j]=(cl[j]+pf[j])%2;
}
for(j=0;j<32;j++)
{
cl[j]=cr[j];
cr[j]=pf[j];
}
printf("\n第%d轮加密结果:",i+1);
for(j=0;j<32;j++)
{
printf("%d",cl[j]);
if((j+1)%8==0)
{
printf(" ");
}
}
for(j=0;j<32;j++)
{
printf("%d",cr[j]);
if((j+1)%8==0)
{
printf(" ");
}
}
}
//左右置换
for(i=0;i<32;i++)
{
ape[i]=cr[i];
ape[i+32]=cl[i];
}
//逆初始置换
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
ct[i*8+j]=ape[antiIP[i][j]-1];
}
}
printf("\n逆初始置换:");
for(k=0;k<64;k++)
{
printf("%d",ct[k]);
if((k+1)%8==0)
{
printf(" ");
}
}
printf("\n加密结果:");
SetConsoleOutputCP(437);//为了输出特殊字符集
cyp=bi2c(ct);
for(i=0;i<8;i++)
{
printf("%c ",(unsigned char)cyp[i]);
}
return;
}
int** str2hex(char* src) //字符转ASCII二进制
{
int t1[8];
int i,j;
int** bi = (int**)malloc(sizeof(int*)*8);
for(i=0;i<8;i++)
{
t1[i]=src[i]+0;
}
for(i=0;i<8;i++)
{
bi[i] = (int*)malloc(sizeof(int) * 8);
for(j=7;j>=0;j--)
{
bi[i][j]=t1[i]%2;
t1[i]=t1[i]/2;
}
}
return bi;
}
void KEY(int* keys) //密钥生成
{
int k0[56],tl[28],tr[28],temp;
int i,j,k;
for(i=0;i<56;i++) //置换选择1
{
k0[i]=keys[PC1[i]-1];
}
for(i=0;i<28;i++) //左右分裂
{
tl[i]=k0[i];
tr[i]=k0[i+28];
}
for(i=0;i<16;i++)
{
for(j=0;j<LN[i];j++) //循环左移
{
temp=tl[0];
for(k=0;k<27;k++)
{
tl[k]=tl[k+1];
}
tl[27]=temp;
temp=tr[0];
for(k=0;k<27;k++)
{
tr[k]=tr[k+1];
}
tr[27]=temp;
}
for(j=0;j<28;j++)
{
k0[j]=tl[j];
k0[j+28]=tr[j];
}
for(j=0;j<48;j++)//置换选择2
{
K[i][j]=k0[PC2[j]-1];
}
}
return;
}
int* bi2c(int* c) //结果显示
{
int i;
int* cp=(int*)malloc(sizeof(int)*8);
for(i=0;i<8;i++)
{
cp[i]=c[i*8+0]*128+c[i*8+1]*64+c[i*8+2]*32+c[i*8+3]*16+c[i*8+4]*8+c[i*8+5]*4+c[i*8+6]*2+c[i*8+7]*1;
}
return cp;
}
//解密
void de_des()
{
SetConsoleOutputCP(936);
printf("\n\n解密系统:");
int ce[64],pp[64],dpt[64];
int* dep=(int*)malloc(sizeof(int)*8);
int i,j,k;
//初始置换
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
ce[i*8+j]=ct[IP[i][j]-1];
}
}
printf("\n初始置换:");
for(k=0;k<64;k++)
{
printf("%d",ce[k]);
if((k+1)%8==0)
{
printf(" ");
}
}
int pl[32],pr[32];
//左右分裂
for(j=0;j<32;j++)
{
pl[j]=ce[j];
pr[j]=ce[j+32];
}
//轮函数
for(i=0;i<16;i++)
{
int pe[48],ps[8],temp[4],cf[32],cf2[32];
int t2[2],t4[4];
for(j=0;j<48;j++)//扩展置换
{
pe[j]=pr[E[j]-1];
}
for(j=0;j<48;j++)//异或
{
pe[j]=(pe[j]+K[15-i][j])%2;
}
for(j=0;j<8;j++)//S盒代替
{
t2[0]=pe[j*6];
t2[1]=pe[j*6+5];
t4[0]=pe[j*6+1];
t4[1]=pe[j*6+2];
t4[2]=pe[j*6+3];
t4[3]=pe[j*6+4];
ps[j]=S[j][(t2[0]*2+t2[1]*1)*16+(t4[0]*8+t4[1]*4+t4[2]*2+t4[3]*1)];
for(k=3;k>=0;k--)
{
temp[k]=ps[j]%2;
ps[j]/=2;
}
for(k=0;k<4;k++)
{
cf[k+j*4]=temp[k];
}
}
for(j=0;j<32;j++)//P置换备份
{
cf2[j]=cf[j];
}
for(j=0;j<32;j++)//P置换
{
cf[j]=cf2[P[j]-1];
}
for(j=0;j<32;j++)//左右异或
{
cf[j]=(pl[j]+cf[j])%2;
}
for(j=0;j<32;j++)
{
pl[j]=pr[j];
pr[j]=cf[j];
}
printf("\n第%d轮解密结果:",i+1);
for(j=0;j<32;j++)
{
printf("%d",pl[j]);
if((j+1)%8==0)
{
printf(" ");
}
}
for(j=0;j<32;j++)
{
printf("%d",pr[j]);
if((j+1)%8==0)
{
printf(" ");
}
}
}
//左右置换
for(i=0;i<32;i++)
{
pp[i]=pr[i];
pp[i+32]=pl[i];
}
//逆初始置换
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
dpt[i*8+j]=pp[antiIP[i][j]-1];
}
}
printf("\n逆初始置换:");
for(k=0;k<64;k++)
{
printf("%d",dpt[k]);
if((k+1)%8==0)
{
printf(" ");
}
}
printf("\n解密结果:");
dep=bi2c(dpt);
for(i=0;i<8;i++)
{
printf("%c",(unsigned char)dep[i]);
}
return;
}
RSA:
#include <stdio.h>
#include <string.h>
//最大公约数。
int gcd(int x,int y){
if(y)
return gcd(y, x%y);
else
return x;
}
//求e关于(p-1)(q-1)的逆元d:私钥
int extend(int e, int fhla){
for(int d=2; d<fhla; d++){ // 1<d<fhla
if(e*d%fhla==1){
return d;
}
}
}
//加密
int c[100]; //用于存储密文
int len;
void encrypt(int e, int n){
char plaintext[100];
printf("请输入明文:");
scanf("%s", plaintext);
len = strlen(plaintext);//获取输入的明文长度。
int ascll[len]; //ascll用于存储明文的ascll转换码
//将字母转换成ascll编码
for(int i=0; i<len; i++) {
ascll[i] = plaintext[i]; //将字母转换成数字
}
printf("\n") ;
printf("开始加密--------------------------------------\n");
int temp = 1;
for(int i=0; i<len; i++){
for(int j=0;j<e;j++){
temp = temp * ascll[i] %n; // 根据剩余定理 (a*b)%n = ((a%n)*(b%n))%n
}
c[i] = temp;
temp = 1;
}
printf("加密的密文为:");
for(int i=0;i<len;i++){
printf("%d",c[i]);
}
printf("\n加密结束--------------------------------------\n");
}
//解密函数
void decrypto(int d, int n) {
int de_plaintext[len];
int temp = 1;
char de_ascll[len];
for(int i=0; i<len; i++){
for(int j=0;j<d;j++){
temp = temp*c[i]%n;
}
de_plaintext[i] = temp;
temp = 1;
}
printf("解密开始--------------------------------------\n");
printf("解密后的符号明文为:");
for(int i=0;i<len;i++){
de_ascll[i] = de_plaintext[i];
printf("%c", de_ascll[i]);
}
printf("\n解密结束--------------------------------------\n");
}
int main(){
int p,q,e,d,n,z;
printf("请输入p、q、e值,用空格间隔开(其中p,q为素数)\n");
scanf("%d%d%d", &p, &q, &e); //从键盘获取p、q、e值
n = p*q;
z = (p-1)*(q-1);
if (gcd(p,q) == 1){
printf("p,q互质\n");
if (gcd(e,z) == 1){
printf("e 的选择符合要求\n");
d = extend(e,z);
printf("公钥为:(e,n) = (%d,%d),请公开!\n",e,n);
printf("密钥为:(d,n) = (%d,%d),不能公开!\n\n\n",d,n);
encrypt(e,n);
printf("\n请输入正确的密钥,密钥正确将解密上面的密文:");
scanf("%d",&d);
decrypto(d,n);
}else
printf("e 的选择不符合要求\n");
}else
printf("p,q不是质数!");
return 0;
}