资源描述:
《信息安全课程设计21384》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、信息系统安全课程设计报告课题名称:OpenSSL非对称加密提交文档学生姓名:提交文档学生学号:同组成员名单:指导教师姓名:指导教师评阅成绩:指导教师评阅意见:..提交报告时间:2014年6月10日1.课程设计目标了解OpenSSL非对称加密算法的背景知识,并实现加密解密。2.分析与设计(1)实现方法:编程语言为C++语言。编程方法:根据算法要求编写代码。(2)利用OpenSSL命令制作生成密钥:#include#include#include#in
2、clude#includeintmain(){charplain[256]="啦啦啦啦啦";charencrypted[1024];chardecrypted[1024];constchar*pub_key="public.pem";constchar*priv_key="private.pem";printf("%s",plain);intlen=strlen(plain);BIO*pBio=BIO_new_file(pub_key,"r"
3、);if(pBio==NULL){printf("failedtoopenpub_keyfile%s!",pub_key);return-1;}RSA*rsa1=PEM_read_bio_RSA_PUBKEY(pBio,NULL,NULL,NULL);if(rsa1==NULL){printf("unabletoreadpublickey!");return-1;}BIO_free_all(pBio);intoutlen=RSA_public_encrypt(len,(unsignedchar*)
4、plain,(unsignedchar*)encrypted,rsa1,RSA_PKCS1_PADDING);printf("%d",outlen);encrypted[outlen]=' ';printf("%s",encrypted);FILE*fp=fopen("out.txt","w");fwrite(encrypted,1,outlen,fp);fclose(fp);pBio=BIO_new_file(priv_key,"r");if(pBio==NULL){printf("faile
5、dtoopenpriv_keyfile%s!",pub_key);return-1;}RSA*rsa2=PEM_read_bio_RSAPrivateKey(pBio,NULL,NULL,NULL);if(rsa2==NULL){printf("unabletoreadprivatekey!");return-1;}BIO_free_all(pBio);outlen=RSA_private_decrypt(outlen,(unsignedchar*)encrypted,(unsignedchar*
6、)decrypted,rsa2,RSA_PKCS1_PADDING);decrypted[outlen]=' ';printf("%s",decrypted);return0;}(3)手动产生公钥:#include#include#include#include#includeintmain(){charplain[256]="rtre";charencrypted[1024];ch
7、ardecrypted[1024];constchar*pub_key="public2.pem";constchar*priv_key="private2.pem";constintg_nBits=1024;printf("%s",plain);intlen=strlen(plain);RSA*rsa1=RSA_generate_key(g_nBits,RSA_F4,NULL,NULL);if(rsa1==NULL){printf("rsa_generate_keyerror");return-
8、1;}BIO*pBio=BIO_new_file(pub_key,"w");if(pBio==NULL){printf("BIO_new_file%serror",pub_key);return-1;}PEM_write_bio_RSAPublicKey(pBio,rsa1);BIO_free_all(pBio);intoutlen=RSA_public_encrypt(len,(unsignedchar*)plain,(