⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keyprogram5.cpp

📁 用VC++编写的钥式加密解密算法
💻 CPP
字号:
#include<iostream>
#include"string.h"
using  namespace  std;
void  ksort(int k,char key[],int l[])
{
   int  i,j,m1,m2;
  for(i=0;i<k;i++)
	  l[i]=i;
  for(i=0;i<k-1;i++)
	  for(j=i+1;j<k;j++)
		  if(key[i]>key[j]||l[i]>l[j])
		  {
		    m1=key[i];
			m2=l[i];
			key[i]=key[j];
			l[i]=l[j];
			key[j]=m1;
			l[j]=m2;
		  }
}

void  ktable(char kt[])
 {
   int  a,b,i,j,k,m,s,t,l[20],dm[20][20];
   char  key[20];
   cout<<"please   input  a  key:\n";
   gets(key);
   cout<<"The  P-Table  is :\n"<<kt;
   k=strlen(key);
   t=strlen(kt);
   m=t/k;
   m+=t%k==0?0:1;
   ksort(k,key,l);
   for(j=0;j<m;j++)
	   for(i=0;i<k;i++)
	   {
	    b=j*k+i;
		if(b<t)
			dm[i][j]=b;
		else
			dm[i][j]=-1;
	   }
	   for(i=0,b=0;i<k;i++)
	   {
	      s=l[i];
		  for(j=0;b<t&&j<m;j++)
		  {
		    a=dm[s][j];
			if(a>=0)
			{
			  kt[b]=a+65;
			  b++;
			}
		  }
	   }
	   cout<<"The  C-Table  is :\n"<<kt;
}
void   main()
{
   static   char  kt[]="abcdefghijklmnopqrstuvwxyz",pdata[40],cdata[40];
   int  i,n;
   cout<<"please  input  a  P-Text:\n";
   gets(pdata);
   n=strlen(pdata);
   ktable(kt);
   cout<<"The  C-Text  is :\n";
   for(i=0;i<n;i++)
   {
      if(pdata[i]>='a'&&pdata[i]<='z')
		  pdata[i]-=97;
	  else  if(pdata[i]>='A'&&pdata[i]<='Z')
		  pdata[i]-=65;
	  else
	  {
	    cout<<pdata[i];
		continue;
	  }
	  cdata[i]=kt[pdata[i]];
	  cout<<cdata[i];
   }
   cout<<endl;
}
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -