#include "stdlib.h"
#include "stdio.h"
#define LTM_DESC
#include <tomcrypt.h>
int main(void)
{
int err, hash_idx, prng_idx, res,padding,inpadding;
unsigned long l1, l2;
unsigned char pt[17], pt2[17], out[1024],out2[1024],out3[1024];
rsa_key key;
printf("\t\t\t**************************\n");
printf(" \t\t\tRSA加解密算法");
printf("\n\t\t\t**************************\n");
printf("请输入明文s(16字节):\n");
scanf("%s",&pt);
printf("选择加密机制:1---RSAES-PKCS1-V1_5 2---RSAES_OAEP\n");
scanf("%d",&inpadding);
if(inpadding ==1 )
padding = LTC_LTC_PKCS_1_V1_5;
else
padding = LTC_LTC_PKCS_1_OAEP;
/* register prng/hash */
if (register_prng(&sprng_desc) == -1) {
printf("Error registering sprng");
return EXIT_FAILURE;
}
/* register a math library (in this case TomsFastMath)*/
ltc_mp = ltm_desc;
if (register_hash(&sha1_desc) == -1) {
printf("Error registering sha1");
return EXIT_FAILURE;
}
hash_idx = find_hash("sha1");
prng_idx = find_prng("sprng");
/* make an RSA-1024 key */
if ((err = rsa_make_key(NULL, /* PRNG state */
prng_idx, /* PRNG idx */
1024/8, /* 1024-bit key */
65537, /* we like e=65537 */
&key) /* where to store the key */
) != CRYPT_OK) {
printf("rsa_make_key %s", error_to_string(err));
return EXIT_FAILURE;
}
/* fill in pt[] with a key we want to send ... */
l1 = sizeof(out);
if ((err = rsa_encrypt_key_ex(pt, /* data we wish to encrypt */
16, /* data is 16 bytes long */
out, /* where to store ciphertext */
&l1, /* length of ciphertext */
(unsigned char*)"TestApp", /* our lparam for this program */
7, /* lparam is 7 bytes long */
NULL, /* PRNG state */
prng_idx, /* prng idx */
hash_idx, /* hash idx */
padding,
&key) /* our RSA key */
) != CRYPT_OK) {
printf("rsa_encrypt_key %s", error_to_string(err));
return EXIT_FAILURE;
}
printf("\n加密后的密文S1:");
for(int i=0;i<=127;i++)
printf("%02x",out[i]);
printf("\n");
/* now let’s decrypt the encrypted key */
l2 = sizeof(pt2);
if ((err = rsa_decrypt_key_ex(out, /* encrypted data */
l1, /* length of ciphertext */
pt2, /* where to put plaintext */
&l2, /* plaintext length */
(unsigned char*)"TestApp", /* lparam for this program */
7, /* lparam is 7 bytes long */
hash_idx, /* hash idx */
padding,
&res, /* validity of data */
&key) /* our RSA key */
) != CRYPT_OK) {
printf("rsa_decrypt_key %s", error_to_string(err));
return EXIT_FAILURE;
}
printf("将S1解密后的明文:%s\n",pt2);
int m=0;
for(int i=0;i<16;i++)
{
if(pt[i]!=pt2[i])
{
printf("解密结果与原明文不同\n");
break;
}
else
m++;
}
if(m==16)
printf("经比较得知:解密结果与原明文相同\n");
if ((err = rsa_encrypt_key_ex(pt, /* data we wish to encrypt */
16, /* data is 16 bytes long */
out2, /* where to store ciphertext */
&l1, /* length of ciphertext */
(unsigned char*)"TestApp", /* our lparam for this program */
7, /* lparam is 7 bytes long */
NULL, /* PRNG state */
prng_idx, /* prng idx */
hash_idx, /* hash idx */
padding,
&key) /* our RSA key */
) != CRYPT_OK) {
printf("rsa_encrypt_key %s", error_to_string(err));
return EXIT_FAILURE;
}
printf("\n用相同密钥对同一个明文加密后的密文S2:");
for(int i=0;i<=127;i++)
printf("%02x",out2[i]);
printf("\n");
l2 = sizeof(pt2);
if ((err = rsa_decrypt_key_ex(out2, /* encrypted data */
l1, /* length of ciphertext */
pt2, /* where to put plaintext */
&l2, /* plaintext length */
(unsigned char*)"TestApp", /* lparam for this program */
7, /* lparam is 7 bytes long */
hash_idx, /* hash idx */
padding,
&res, /* validity of data */
&key) /* our RSA key */
) != CRYPT_OK) {
printf("rsa_decrypt_key %s", error_to_string(err));
return EXIT_FAILURE;
}
printf("将S2解密后的明文:%s\n",pt2);
if ((err = rsa_encrypt_key_ex(pt, /* data we wish to encrypt */
16, /* data is 16 bytes long */
out3, /* where to store ciphertext */
&l1, /* length of ciphertext */
(unsigned char*)"TestApp", /* our lparam for this program */
7, /* lparam is 7 bytes long */
NULL, /* PRNG state */
prng_idx, /* prng idx */
hash_idx, /* hash idx */
padding,
&key) /* our RSA key */
) != CRYPT_OK) {
printf("rsa_encrypt_key %s", error_to_string(err));
return EXIT_FAILURE;
}
printf("\n用相同密钥对同一个明文加密后的密文S3:");
for(int i=0;i<=127;i++)
printf("%02x",out3[i]);
printf("\n");
l2 = sizeof(pt2);
if ((err = rsa_decrypt_key_ex(out3, /* encrypted data */
l1, /* length of ciphertext */
pt2, /* where to put plaintext */
&l2, /* plaintext length */
(unsigned char*)"TestApp", /* lparam for this program */
7, /* lparam is 7 bytes long */
hash_idx, /* hash idx */
padding,
&res, /* validity of data */
&key) /* our RSA key */
) != CRYPT_OK) {
printf("rsa_decrypt_key %s", error_to_string(err));
return EXIT_FAILURE;
}
printf("将S3解密后的明文:%s\n",pt2);
/* if all went well pt == pt2, l2 == 16, res == 1 */
system("pause");
}
没有合适的资源?快使用搜索试试~ 我知道了~
RSA编程实验——使用libTomCrypt库
共28个文件
tlog:10个
pdb:3个
lastbuildstate:1个
4星 · 超过85%的资源 需积分: 16 33 下载量 112 浏览量
2014-12-23
00:06:47
上传
评论 1
收藏 1.16MB ZIP 举报
温馨提示
1. 调用LibTomCrypt库函数实现 2. 程序输入:文本串 s 3. 使用RSA算法对s加密,输出加密结果s’ 4. 解密s’ ,得到明文s’’, 比较s与s’’是否相同 5. 加解密过程要求支持RSAES-OAEP和RSAES-PKCS1-V1_5两种机制 6. 相同密钥对条件下, 多次对s加密,比较每次加密得到的s’是否相同.
资源推荐
资源详情
资源评论
收起资源包目录
RSA扩展实验.zip (28个子文件)
RSA扩展实验
main.opt 48KB
main.dsw 516B
RSA扩展实验.v11.suo 4KB
main.ncb 33KB
main.dsp 3KB
main.plg 2KB
RSA扩展实验.vcxproj 4KB
main.cpp 5KB
Debug
RSA扩展实验.log 4KB
vc110.idb 99KB
CL.write.1.tlog 420B
RSA扩展实验.lastbuildstate 110B
CL.read.1.tlog 4KB
cl.command.1.tlog 712B
link-cvtres.read.1.tlog 2B
RSA扩展实验.ilk 411KB
RSA扩展实验.exe 98KB
main.obj 19KB
link.write.1.tlog 590B
link-rc.write.1.tlog 2B
link-cvtres.write.1.tlog 2B
link.command.1.tlog 1KB
link-rc.read.1.tlog 2B
link.read.1.tlog 3KB
vc110.pdb 76KB
main.pdb 25KB
RSA扩展实验.pdb 571KB
RSA扩展实验.sdf 3.19MB
共 28 条
- 1
资源评论
- qq_407239712018-06-05很不错的资源!学习了!
- haozi5962018-03-23没有体现出公钥和私钥,用的是统一秘钥加解密,虽然能用但是和AES对称加密区别在哪?
qq_22862877
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功