/*************************************************************************/
/*File name :RSA.c */
/*Description: */
/* public Key:p q r */
/* private Key:m n */
/*************************************************************************/
/*包含头文件*/
#include <stdio.h>
#include <string.h>
/*类型定义*/
typedef long int li_ELEMTYPE;
typedef int i_ELEMTYPE;
typedef char c_ELEMTYPE;
/*宏定义*/
#define TEXT_MAX_NUM 20
#define RSA_ENCODE_TEXT "2011-02-15"
/*RSA编解码函数*/
/*
输入输出说明:r = a^b mod c
编码 1.a-------原始加密明文数据
2.b-------加密指数(p-1)(q-1)
3.c-------p*q
4.r-------加密后密文数据
解码 1.a-------原始解密密文数据
2.b-------解密指数d
3.c-------p*q
4.r-------解密后明文数据
*/
li_ELEMTYPE Rsa_un_enCode(li_ELEMTYPE a,li_ELEMTYPE b,li_ELEMTYPE c)
{
li_ELEMTYPE r = 1;
b = b + 1;
while(b != 1)
{
r = r * a;
r = r % c;
b--;
}
return r;
}
/*main主函数*/
int main(int argc, char **argv)
{
li_ELEMTYPE p,q,e,d,n,t;
i_ELEMTYPE i = 0;
i_ELEMTYPE acSecret_Text[TEXT_MAX_NUM];
c_ELEMTYPE acPublic_Text[TEXT_MAX_NUM];
memset(acSecret_Text, 0, sizeof(acSecret_Text));
memset(acPublic_Text, 0, sizeof(acPublic_Text));
memcpy(acPublic_Text, RSA_ENCODE_TEXT, sizeof(acPublic_Text));
p = 53;
q = 79;
e = 77777;
printf("Info: p = %ld q = %d e = %d\n",p,q,e);
n=p*q;
printf("\nThe n(p*q) = %3d\n",n);
t=(p-1)*(q-1);
printf("\nThe t((p-1)*(q-1)) = %ld\n",t);
printf("\nCaculating d \n>>…………\n");
d = 1;
while(((e * d) % t) != 1)
{
d++;
}
printf("The d = %ld\n",d);
printf("\n--------------Encoding--------------\n");
printf("The acSecret_Text is:\n");
for(i = 0; i < TEXT_MAX_NUM && acPublic_Text[i] != 0; i++)
{
acSecret_Text[i] = Rsa_un_enCode(acPublic_Text[i],e,n);
printf("%d ",acSecret_Text[i]);
}
printf("\n");
printf("\n--------------Decoding--------------\n");
printf("The acPublic_Text is:\n");
for(i = 0; i < TEXT_MAX_NUM && acPublic_Text[i] != 0; i++)
{
acPublic_Text[i] = (c_ELEMTYPE)Rsa_un_enCode(acSecret_Text[i],d,n);
printf("%c",acPublic_Text[i]);
}
printf("\n");
return 0;
}
/*************************源文件结束(+回车)************* ****************/
|