利用openssl库进行RSA加密
2016-04-27 15:54
501 查看
NSString *RSA_encrypt(NSString *data) { unsigned char *str=(unsigned char*)[data UTF8String]; //把要加密的数据进行utf8编码 unsigned char *p_en; RSA *p_rsa; int rsa_len; NSString *public = formatPublicKey(PUBLIC_KEY); //把服务器的公钥格式化 NSData *pub= [public dataUsingEncoding: NSUTF8StringEncoding]; //公钥utf8编码 BIO *bio = BIO_new_mem_buf((void *)[pub bytes], (int)[pub length]); //用bio函数把编码过的公钥写到内存中 p_rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, 0, NULL); //用pem格式读取内存中的公钥 BIO_free(bio); //内存中释放bio rsa_len=RSA_size(p_rsa); //计算公钥的长度 p_en=(unsigned char *)malloc(rsa_len+1); //分配公钥长度的内存 memset(p_en,0,rsa_len+1); //内存清零 int Result=RSA_public_encrypt((int)strlen((const char*)str),(unsigned char *)str,(unsigned char*)p_en,p_rsa,RSA_PKCS1_PADDING); //加密公钥 int RSA_public_encrypt(int flen, unsigned char *from, unsigned char *to, RSA *rsa, int padding); if(Result<0) return NULL; RSA_free(p_rsa); //内存中释放p_rsa NSMutableString *result = [NSMutableString stringWithCapacity:128]; //创建一个128字符的可变字符串 for(int i = 0; i < 128; i++) [result appendFormat:@"%02x", (unsigned char)p_en[i]]; //把加密的Result存储到result中去 free(p_en); //内存中释放p_en p_en=NULL; return result; }
相关文章推荐
- Hadoop生态系统介绍
- iOS应用架构谈-view层的组织和调用方案
- shell-awk高级应用
- LNMP中nginx代理
- OP-TEE TA:读写寄存器数据
- 阿里云安装docker的问题
- [企业级linux安全管理]- 主机安全管理
- Linux下安装Activemq-5.11.1
- Auto packing the repository in background for optimum performance
- [企业级linux安全管理]- 系统日志管理
- Android系统启动流程 -- linux kernel
- win7 vm10.0 centos7 共享目录问题
- [企业级linux安全管理]- 安全管理基础(1)
- Linux命令常用速查(自用)
- Linux驱动常用知识小结
- 【Linux】Red Hat Linux在虚拟机安装指南
- vi 普通操作 linux
- 让shell脚本在后台运行的两种方法
- linux私房菜学习笔记—vim编辑器
- html5手机网站需要加的那些meta/link标签,html5 meta全解