看你催就仓促写了个,自我感觉写的不是很好,但是能用了。数据只能是大写字母组成的字符串。
加密的时候,输入Y,然后输入要加密的文本(大写字母)
解密的时候,输入N,然后输入一个整数n表示密文的个数,然后n个整数表示加密时候得到的密文。
/*RSA algorithm */
#include
#include
#include
#define MM 7081
#define KK 1789
#define PHIM 6912
#define PP 85
typedef char strtype[10000];
int len;
long nume[10000];
int change[126];
char antichange[37];
void initialize()
{ int i;
char c;
for (i = 11, c = 'A'; c <= 'Z'; c ++, i ++)
{ change[c] = i;
antichange[i] = c;
}
}
void changetonum(strtype str)
{ int l = strlen(str), i;
len = 0;
memset(nume, 0, sizeof(nume));
for (i = 0; i < l; i ++)
{ nume[len] = nume[len] * 100 + change[str[i]];
if (i % 2 == 1) len ++;
}
if (i % 2 != 0) len ++;
}
long binamod(long numb, long k)
{ if (k == 0) return 1;
long curr = binamod (numb, k / 2);
if (k % 2 == 0)
return curr * curr % MM;
else return (curr * curr) % MM * numb % MM;
}
long encode(long numb)
{ return binamod(numb, KK);
}
long decode(long numb)
{ return binamod(numb, PP);
}
main()
{ strtype str;
int i, a1, a2;
long curr;
initialize();
puts("Input 'Y' if encoding, otherwise input 'N':");
gets(str);
if (str[0] == 'Y')
{ gets(str);
changetonum(str);
printf("encoded: ");
for (i = 0; i < len; i ++)
{ if (i) putchar('-');
printf(" %ld ", encode(nume[i]));
}
putchar('\n');
}
else
{ scanf("%d", &len);
for (i = 0; i < len; i ++)
{ scanf("%ld", &curr);
curr = decode(curr);
a1 = curr / 100;
a2 = curr % 100;
printf("decoded: ");
if (a1 != 0) putchar(antichange[a1]);
if (a2 != 0) putchar(antichange[a2]);
}
putchar('\n');
}
putchar('\n');
system("PAUSE");
return 0;
}
测试:
输入:
Y
FERMAT
输出:
encoded: 5192 - 2604 - 4222
输入
N
3 5192 2604 4222
输出
decoded: FERMAT
程序修改如下:
(主要是你的循环写的不对,输入的字符应该-'0'才能与正常的数字对应)
#include
#include
int
candp(int
a,int
b,int
c)
{int
r=1;
int
s;
int
i=1;
for(i=1;i<=b;i++)r=r*a;
printf("%d\n",r);
s=r%c;
printf("%d\n",s);
return
s;}
void
main()
{
int
p,q,e,d,m,n,t,c,r
;
char
s;
printf("please
input
the
p,q:");
scanf("%d%d",&p,&q);
n=p*q;
t=(p-1)*(q-1);
printf("the
n
is
%12d\n",n);
printf("please
input
the
e:");
scanf("%d",&e);
while(e<1||e>n)
//此处修改为while循环
{
printf("e
is
error,please
input
again:");
scanf("%d",&e);
}
d=1;
while(((e*d)%t)!=1)
d++;
printf("then
caculate
out
that
the
d
is
%d\n",d);
printf("the
cipher
please
input
1\n");
printf("the
plain
please
input
2\n");
scanf("%c",&s);
while((s-'0')!=1&&(s-'0')!=2)
//消除后面的getchar()
此处增加while循环注意括号内的字符
{scanf("%c",&s);}
switch(s-'0')
{
case
1:printf("intput
the
m:");
scanf("%d",&m);
c=candp(m,e,n);
printf("the
plain
is
%d\n",c);break;
case
2:printf("input
the
c:");
scanf("%d",&c);
m=candp(c,d,n);
printf("the
cipher
is
%8d\n",m);
break;
}
}
看你催就仓促写了个,自我感觉写的不是很好,但是能用了。数据只能是大写字母组成的字符串。
加密的时候,输入Y,然后输入要加密的文本(大写字母)
解密的时候,输入N,然后输入一个整数n表示密文的个数,然后n个整数表示加密时候得到的密文。
/*RSA
algorithm
*/
#include
#include
#include
#define
MM
7081
#define
KK
1789
#define
PHIM
6912
#define
PP
85
typedef
char
strtype[10000];
int
len;
long
nume[10000];
int
change[126];
char
antichange[37];
void
initialize()
{
int
i;
char
c;
for
(i
=
11,
c
=
'A';
c
<=
'Z';
c
++,
i
++)
{
change[c]
=
i;
antichange[i]
=
c;
}
}
void
changetonum(strtype
str)
{
int
l
=
strlen(str),
i;
len
=
0;
memset(nume,
0,
sizeof(nume));
for
(i
=
0;
i
<
l;
i
++)
{
nume[len]
=
nume[len]
*
100
+
change[str[i]];
if
(i
%
2
==
1)
len
++;
}
if
(i
%
2
!=
0)
len
++;
}
long
binamod(long
numb,
long
k)
{
if
(k
==
0)
return
1;
long
curr
=
binamod
(numb,
k
/
2);
if
(k
%
2
==
0)
return
curr
*
curr
%
MM;
else
return
(curr
*
curr)
%
MM
*
numb
%
MM;
}
long
encode(long
numb)
{
return
binamod(numb,
KK);
}
long
decode(long
numb)
{
return
binamod(numb,
PP);
}
main()
{
strtype
str;
int
i,
a1,
a2;
long
curr;
initialize();
puts("Input
'Y'
if
encoding,
otherwise
input
'N':");
gets(str);
if
(str[0]
==
'Y')
{
gets(str);
changetonum(str);
printf("encoded:
");
for
(i
=
0;
i
<
len;
i
++)
{
if
(i)
putchar('-');
printf("
%ld
",
encode(nume[i]));
}
putchar('\n');
}
else
{
scanf("%d",
&len);
for
(i
=
0;
i
<
len;
i
++)
{
scanf("%ld",
&curr);
curr
=
decode(curr);
a1
=
curr
/
100;
a2
=
curr
%
100;
printf("decoded:
");
if
(a1
!=
0)
putchar(antichange[a1]);
if
(a2
!=
0)
putchar(antichange[a2]);
}
putchar('\n');
}
putchar('\n');
system("PAUSE");
return
0;
}
测试:
输入:
Y
FERMAT
输出:
encoded:
5192
-
2604
-
4222
输入
N
3
5192
2604
4222
输出
decoded:
FERMAT