openssl对数组加密解密的完整实现代码
本例是用C实现的对一个数组进行加密,加密到第二个数组,然后解密到另一个数组的完整实现代码。
- #include <stdio.h>
- #include <string.h>
- #include "openssl/evp.h"
- #include "openssl/x509.h"
- static void disp(void * pbuf,int size)
- { int i=0;
- for( i=0;i<size;i++)
- printf("%02x ",*((unsigned char *)pbuf+i));
- putchar('\n');
- }
- /*
- * key:加密密钥,一般设置位24,不知为啥
- * iv:加密初始向量
- * in_enc:明文数组,输入数组
- * out_enc:加密后的数组,输出密文数组
- * in_len:明文长度
- * out_len:密文长度
- * */
- //加密函数
- int EncryptBuffer(unsigned char * key,unsigned char *iv,unsigned char * in_enc, unsigned char *out_enc,int in_len,int *out_len)
- {
- ;
- int outl; //第一次使用update加密的数据长度
- int outl2; //剩余的字段,经过final填充后的长度
- int inl;
- int rv;
- EVP_CIPHER_CTX ctx;
- EVP_CIPHER_CTX_init(&ctx); //初始化ctx
- rv = EVP_EncryptInit_ex(&ctx,EVP_des_ede3_ecb(),NULL,key,iv); //设置密码算法、key和iv
- if(rv != 1)
- {
- printf("Err\n");
- return -1;
- }
- inl=in_len;
- rv = EVP_EncryptUpdate(&ctx,out_enc,&outl,in_enc,in_len);//加密
- if(rv != 1)
- {
- printf("Err\n");
- return -1;
- }
- //加密结束
- rv = EVP_EncryptFinal_ex(&ctx,out_enc+outl,&outl2);
- if(rv != 1)
- {
- EVP_CIPHER_CTX_cleanup(&ctx);
- return -1;
- }
- *out_len=outl+outl2;
- EVP_CIPHER_CTX_cleanup(&ctx); //清除EVP加密上下文环境
- printf("加密已完成\n");
- }
- /*
- * key:加密密钥,一般设置位24,不知为啥
- * iv:加密初始向量
- * in_dec:密文数组,输入数组
- * out_dec:解密后的数组,输出数组
- * in_len:密文长度
- * out_len:明文长度
- * */
- //解密函数
- int DecryptBuffer(unsigned char * key,unsigned char *iv,unsigned char * in_dec, unsigned char *out_dec,int in_len,int *out_len)
- {
- int outl; //第一次使用update解密的数据长度
- int outl2; //剩余的字段,经过final解密并去除填充后的长度
- int rv;
- EVP_CIPHER_CTX ctx;
- //初始化ctx
- EVP_CIPHER_CTX_init(&ctx);
- //设置解密的算法、key和iv
- rv = EVP_DecryptInit_ex(&ctx,EVP_des_ede3_ecb(),NULL,key,iv);
- if(rv != 1)
- {
- EVP_CIPHER_CTX_cleanup(&ctx);
- return -1;
- }
- //循环读取原文,解密后后保存到明文文件。
- rv = EVP_DecryptUpdate(&ctx,out_dec,&outl,in_dec,in_len);//解密
- if(rv != 1)
- {
- EVP_CIPHER_CTX_cleanup(&ctx);
- return -1;
- }
- //解密结束
- rv = EVP_DecryptFinal_ex(&ctx,out_dec+outl,&outl2);
- if(rv != 1)
- {
- EVP_CIPHER_CTX_cleanup(&ctx);
- return -1;
- }
- *out_len=outl+outl2;
- EVP_CIPHER_CTX_cleanup(&ctx);//清除EVP加密上下文环境
- printf("解密已完成\n");
- }
- int main()
- {
- int len=128+4;
- int dec_len,len2;
- unsigned char key[EVP_MAX_KEY_LENGTH]; //保存密钥的数组
- unsigned char iv[EVP_MAX_KEY_LENGTH]; //保存初始化向量的数组
- //EVP加密上下文环境
- unsigned char out[len+EVP_MAX_KEY_LENGTH]; //保存加密后明文的缓冲区数组
- unsigned char dec[len+EVP_MAX_KEY_LENGTH]; //保存解密后明文的缓冲区数组
- unsigned char in[len+EVP_MAX_KEY_LENGTH]; //保存原文的缓冲区
- int i=0;
- //设置key和iv
- for(i=0;i<8;i++)
- {
- key[i]=i;
- }
- for(i=0;i<8;i++)
- {
- iv[i]=i;
- }
- for(i=0;i<len;i++)
- {
- in[i]=i;
- }
- disp(in,len);
- EncryptBuffer(key,iv,in,dec,len,&dec_len);
- printf("dec_len:%d\n",dec_len);
- disp(dec,dec_len);
- DecryptBuffer(key,iv,dec,out,dec_len,&len2);
- disp(out,len2);
- printf("解密候数据长度:%d\n",len2);
- return 0;
- }
http://blog.csdn.net/xueyushenzhou/article/details/23281675
openssl对数组加密解密的完整实现代码的更多相关文章
- openssl evp RSA 加密解密
openssl evp RSA 加密解密 可以直接使用RSA.h 提供的接口 如下测试使用EVP提供的RSA接口 1. EVP提供的RSA 加密解密 主要接口: int EVP_PKEY_encryp ...
- OpenSSL 中 RSA 加密解密实现源代码分析
1.RSA 公钥和私钥的组成.以及加密和解密的公式: 2.模指数运算: 先做指数运算,再做模运算.如 5^3 mod 7 = 125 mod 7 = 6 3.RSA加密算法流程: 选择一对不同的.而且 ...
- CryptoAPI与openssl RSA非对称加密解密(PKCS1 PADDING)交互
(以下代码中都只做测试用,有些地方没有释放内存...这个自己解决下) 1.RSA非对称的,首先提供一个供测试用的证书和私钥的数据 1)pem格式的证书和私钥(公私钥是对应的)的base64编码 voi ...
- AES字节数组加密解密流程
AES类时微软MSDN中最常用的加密类,微软官网也有例子,参考链接:https://docs.microsoft.com/zh-cn/dotnet/api/system.security.crypto ...
- 利用openssl进行RSA加密解密
openssl是一个功能强大的工具包,它集成了众多密码算法及实用工具.我们即可以利用它提供的命令台工具生成密钥.证书来加密解密文件,也可以在利用其提供的API接口在代码中对传输信息进行加密. RSA是 ...
- 用openssl库RSA加密解密
#include <stdio.h> #include <openssl/rsa.h> #include <openssl/pem.h> #include < ...
- C++调用openssl实现DES加密解密cbc模式 zeropadding填充方式 pkcs5padding填充方式 pkcs7padding填充方式
============================================== des cbc 加密 zeropadding填充方式 ======================= ...
- openssl RSA基本加密解密
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <openssl/bn ...
- 提供openssl -aes-256-cbc兼容加密/解密的简单python函数
原文链接:http://joelinoff.com/blog/?p=885 这里的示例显示了如何使用python以与openssl aes-256-cbc完全兼容的方式加密和解密数据.它是基于我在本网 ...
随机推荐
- 虚拟机最佳实践:单个 VM、临时存储和已上传磁盘
大家好! 我是 Drew McDaniel,来自 Microsoft Azure虚拟机功能研发团队,我从团队成立之初就已加入. 在本博客文章中,我将分享一些最佳实践指南,帮助您充分利用您的Azure虚 ...
- bzoj3629[JLOI2014]聪明的燕姿
http://www.lydsy.com/JudgeOnline/problem.php?id=3629 搜索. 我们知道: 如果$N=\prod\limits_{i=1}^{m}p_{i}^{k_{ ...
- global.asax?app.config?webconfig??
一.Global.asax 1.global.asax是什么? 一个文本文件,至于它包含写什么内容?顾名思义,global 肯定是掌管一个应用程序(application)的全局性的东西,例如应用程序 ...
- PHP基础设计模式——工厂模式
<?php//文件名:Factory namespace IMooc; class Factory { //工程模式 static function creatDatabase() { $db ...
- 【Nginx】事件和连接
不同的操作系统相应不同的事件驱动机制.在Linux 2.6之后使用epoll机制.相应的事件驱动模块是ngx_epoll_module.Nginx的ngx_event_core_module模块依据操 ...
- Chapter 4: Spring and AOP:Spring's AOP Framework -- draft
Spring's AOP Framework Let's begin by looking at Spring's own AOP framework - a proxy-based framewor ...
- git删除未跟踪文件
# 删除 untracked files git clean -f # 连 untracked 的目录也一起删掉 git clean -fd # 连 gitignore 的untrack 文件 ...
- java基础之String
字符串的含义 字符串的应用 字符串的方法
- DEV PivotGridControl 全选行或列
foreach (string item in fieldProductName.FilterValues.Values) { pivotGridControl.Cells.SetSelectionB ...
- 老生常谈的Javascript作用域问题
在前端学习中,作用域这个问题一直被广泛提起,什么是作用域,什么又是作用域链?在Javascript中,怎么去理解这些概念都是学好这门语言的关键,所以在学习前端开发的过程中,我需要也很有必要去学习和总结 ...