|

楼主 |
发表于 2022-5-4 10:32:28
|
显示全部楼层
[code]#include<stdio.h>
#include<stdlib.h>
int long_n(int n);
int shuru(char* arr, int k, char* wei, int is_first);
void jiami(char* arr, int k, int e, int n);
int shuru(char* arr, int k, char* wei, int is_first)
{
int i;
char ch;
if (is_first == 1)
ch = getchar();
else
ch = *wei;
for (i = 0; (i < k) && (ch != '\n'); i++)
{
arr[i] = ch;
ch = getchar();
}
*wei = ch;
for (i = i; i < k; i++)
arr[i] = 'a';
if (ch == '\n')
return 0;
else
return 1;
}
void jiami(char* arr, int k, int e, int n)
{
int m = 0, c = 1, i, j, t = 0, shu, temp, num = 0;
int* array;
for (i = 0; i < k; i++)
{
temp = 1;
for (j = 0; j < (k - i - 1) * 2; j++)
temp = temp * 10;
shu = (int)arr[i] - 97;
m = m + temp * shu;
}
temp = e;
do {
temp = temp / 2;
num++;
} while (temp != 0);
array = (int*)malloc(sizeof(int) * k);
temp = e;
for (i = 0; i < num; i++)
{
array[i] = temp % 2;
temp = temp / 2;
}
for (i = num - 1; i >= 0; i--)
{
t = t * 2;
temp = c * c;
if (temp > n)
{
for (j = 0; temp - n * j >= 0; j++);
j--;
c = temp - n * j;
}
else
c = temp;
if (array[i] == 1)
{
t = t + 1;
temp = c * m;
if (temp > n)
{
for (j = 0; temp - n * j >= 0; j++);
j--;
c = temp - n * j;
}
else
c = temp;
}
e = e / 2;
}
temp = c;
i = 0;
do {
temp = temp / 10;
i++;
} while (temp != 0);
for (i; i < num; i++)
printf("0");
printf("%d", c);
}
int long_n(int n)
{
int temp, i, j, k, shi, comp = 0;
temp = n;
for (i = 1; temp / 10 != 0; i++)
{
temp = temp / 10;
}
temp = i;
if (i % 2 != 0)
{
i = i - 1;
return i;
}
else
{
for (j = 0; j < i / 2; j++)
{
shi = 1;
for (k = 0; k < temp - 2; k++)
shi = shi * 10;
comp = comp + shi * 25;
temp = temp - 2;
}
if (comp <= n)
return i;
else
{
i = i - 2;
return i;
}
}
}
int main()
{
int p, q, e, d, n, fai_n, k, i, is_first = 1;
char ch, * arr, wei = 'a';
printf("请输入p、q、e值,用空格间隔开\n");
scanf_s("%d%d%d", &p, &q, &e);
n = p * q;
fai_n = (p - 1) * (q - 1); //Φ(n)
for (k = 0; (k * n + 1) % e != 0; k++);
if ((k * n + 1) % e == 0)
d = (k * n + 1) / e; //d * e ≡ 1 (mod Φ(n))
k = long_n(n);
k = k / 2;
ch = getchar();
arr = (char*)malloc(sizeof(char) * k);
printf("请输入明文\n");
while (1)
{
i = shuru(arr, k, &wei, is_first);
is_first = 0;
jiami(arr, k, e, n);
if (i == 0)
break;
}
printf("\n");
return 0;
} |
|