fangshe.cpp

来自「仿射加解密算法 比较简单」· C++ 代码 · 共 67 行

CPP
67
字号
#include<string.h>
#include<iostream.h>
void encode(int a, int b);
void uncode(int a, int b);
int getUncode(int a);

int main()
{
	int a, b,c,d;
	cout<<"please input encode K(A,B)"<<endl;
	cin>>a>>b;
	c = getUncode(a);
	d = -b;
    cout<<"the uncode is K("<<c<<","<<d<<")"<<endl;
	encode(a,b);
	uncode(c,d);
	return 0;
}


void encode(int a,int b)
{
    int  len=0,i=0,j=0,temp;
    char s[1000];
	char s1[1000];
	
	cout<<"please input the string"<<endl;
	cout<<"(warning the length not more than 1000 and must be small letters)"<<endl;
    cin>>s;
	len = strlen(s);
    for(i = 0,j = 0;i<len;i++)
	{   
		temp = (a*(s[i]-97)+b)%26;
		if(temp<0) temp += 26; 
		s1[i]= temp +97;
	}
	s1[i]='\0';
	cout<<"the changed string is:"<<endl;
	cout<<s1<<endl;
}

void uncode(int a,int b)
{
	int  len=0,i=0,j=0;
    char s[1000];
	char s1[1000];
	cout<<"please input the string"<<endl;
	cout<<"(warning the length not more than 1000 and must be small letters)"<<endl;
    cin>>s;
	len = strlen(s);
    for(i = 0,j = 0;i<len;i++)
		s1[i]=(a*(s[i]-97)+b)%26+97;
	s1[i]='\0';
	cout<<"the changed string is:"<<endl;
	cout<<s1<<endl;
}

int getUncode(int a)
{
   int i,b;
   for(i = 1; i<a;i++)
   {
     b = ( 26*i + 1 )/a;
     if(( 26*i + 1 )%a==0)
		 return b;
   }
}

⌨️ 快捷键说明

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