rsa算法对字符串的加密解密
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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));
#define RSA_ENCODE_TEXT "xidianuniversity"
/*RSA编解码函数*/
/*
输入输出说明:r = a^b mod c
编码1.a-------原始加密明文数据
2.b-------加密指数(p-1)(q-1)
3.c-------p*q
4.r-------加密后密文数据
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];
printf("\nCaculating d \n>>…………\n");
d = 1;
while(((e * d) % t) != 1)
{
d++;
}
printf("The d = %ld\n",d);
printf("\n--------------Encoding--------------\n");
{
acPublic_Text[i] = (c_ELEMTYPE)Rsa_un_enCode(acSecret_Text[i],d,n);
printf("%c",acPublic_Text[i]);
}
printf("\n");
return 0;
}
}
printf("\n");
printf("\n--------------Decoding--------------\n");
printf("The acPublic_Text is:\n");
for(i = 0; i < TEXT_MAX_NUM && acPublic_Text[i] != 0; i++)
#include <stdio.h>
#include <string.h>
/*类型定义*/
typedef long int li_ELEMTYPE;
typedef int i_ELEMTYPE;
typedef char c_ELEMTYPE;
/*宏定义*/
#define TEXT_MAX_NUM 20
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]);
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);
解码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 Biblioteka Baidu 1;
memset(acSecret_Text, 0, sizeof(acSecret_Text));
memset(acPublic_Text, 0, sizeof(acPublic_Text));
memcpy(acPublic_Text, RSA_ENCODE_TEXT, sizeof(acPublic_Text));
#define RSA_ENCODE_TEXT "xidianuniversity"
/*RSA编解码函数*/
/*
输入输出说明:r = a^b mod c
编码1.a-------原始加密明文数据
2.b-------加密指数(p-1)(q-1)
3.c-------p*q
4.r-------加密后密文数据
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];
printf("\nCaculating d \n>>…………\n");
d = 1;
while(((e * d) % t) != 1)
{
d++;
}
printf("The d = %ld\n",d);
printf("\n--------------Encoding--------------\n");
{
acPublic_Text[i] = (c_ELEMTYPE)Rsa_un_enCode(acSecret_Text[i],d,n);
printf("%c",acPublic_Text[i]);
}
printf("\n");
return 0;
}
}
printf("\n");
printf("\n--------------Decoding--------------\n");
printf("The acPublic_Text is:\n");
for(i = 0; i < TEXT_MAX_NUM && acPublic_Text[i] != 0; i++)
#include <stdio.h>
#include <string.h>
/*类型定义*/
typedef long int li_ELEMTYPE;
typedef int i_ELEMTYPE;
typedef char c_ELEMTYPE;
/*宏定义*/
#define TEXT_MAX_NUM 20
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]);
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);
解码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 Biblioteka Baidu 1;