//下面的密码算法适合来堤防你的小妹妹~~~~~
//JJCCX
#include<stdio.h>
#include<string.h>
void Encryption(char*, char*);
void Decryption(char*, char*);
void EuclidEx(int a,int b,int &x,int &y);
int main()
{
char str1[256], str2[256], str3[256];
strcpy(str1, "abcdefghijklmnopqrstuvwxyz");
printf("before encryption: %s\n", str1);
Encryption(str1, str2);
printf("after encryption: %s\n", str2);
Decryption(str2, str3);
printf("after decryption: %s\n", str3);
return 0;
}
void EuclidEx(int a,int b,int &x,int &y)
{
int temp;
if (b == 0)
{
x = 1;
y = 0;
return;
}
EuclidEx(b, a % b, x, y);
temp = x;
x = y;
y = temp - a / b * y;
}
//加密
void Encryption(char* source, char* dest)
{
int i;
int length = strlen(source);
char m;
char c;
for(i = 0; i < length; i++)
{
m = source[i] - 'a';
//c = (m + 3) % 26;//“凯撒密码”的加密变换
//c = (4 * m) % 26;//“乘同余码”加密变换
c = (7 * m + 7) % 26;//“线性同余”或者说“仿射变换”的加密变换,书上100页第7题
dest[i] = c + 'a';
}
dest[length] = 0;
}
//解密
void Decryption(char* source, char* dest)
{
int i;
int length = strlen(source);
char m;
char c;
int x = 0;
int y = 0;
EuclidEx(7, 26, x, y);
x = (x + 26) % 26;
for(i = 0; i < length; i++)
{
c = source[i] - 'a';
//m = (26 + c - 3) % 26;//“凯撒密码”的解密变换
//m = (22 * c) % 26;//“乘同余码”的解密变换
m = (x * (26 + c - 7)) % 26;///“线性同余”或者说“仿射变换”的解密变换,书上100页第7题
dest[i] = m + 'a';
}
dest[length] = 0;
}