使用openssl api进行加密解密
来源:互联网 发布:php artisan clear 编辑:程序博客网 时间:2024/05/18 18:55
[root@playmud sec]#cat sec.c
#include <stdio.h>
#include <openssl/evp.h>
int do_crypt(FILE *in, FILE *out, int do_encrypt);
int main(int argc,char **argv)
{
FILE * fin;
FILE * fout;
fin = fopen(argv[1], "a+");
fout = fopen(argv[2], "a+");
do_crypt(fin, fout, atoi(argv[3]));
return 0;
}
int do_crypt(FILE *in, FILE *out, int do_encrypt)
{
char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
int inlen, outlen;
EVP_CIPHER_CTX ctx;
unsigned char key[] = "0123456789";
unsigned char iv[] = "12345678";
/* 这时候不进行key和IV的设置,因为我们还要修改参数 */
EVP_CIPHER_CTX_init(&ctx);
EVP_CipherInit_ex(&ctx, EVP_rc4(), NULL, NULL, NULL, do_encrypt);
EVP_CIPHER_CTX_set_key_length(&ctx, 10);
/* 完成参数设置,进行key和IV的设置 */
EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt);
for(;;)
{
inlen = fread(inbuf, 1, 1024, in);
if(inlen <= 0) break;
if(!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf, inlen))
{
/*出错处理 */
return 0;
}
fwrite(outbuf, 1, outlen, out);
}
if(!EVP_CipherFinal_ex(&ctx, outbuf, &outlen))
{
/* 出错处理*/
return 0;
}
fwrite(outbuf, 1, outlen, out);
EVP_CIPHER_CTX_cleanup(&ctx);
return 1;
}
[root@playmud sec]# gcc -o sec sec.c -lcrypto
[root@playmud sec]# echo "abcdefg" >in.txt
[root@playmud sec]# ./sec in.txt out.txt 1
[root@playmud sec]# ls
in.txt out.txt sec sec.c
[root@playmud sec]# cat out.txt
庄~W絊[root@playmud sec]#
[root@playmud sec]# ./sec out.txt out2.txt 0
[root@playmud sec]# cat out2.txt
abcdefg
- 使用openssl api进行加密解密
- 使用openssl进行加密解密
- 使用openssl进行RSA加密解密
- 利用openssl进行RSA加密解密
- 利用openssl进行RSA加密解密
- openssl命令行进行RSA加密解密
- iOS -- OpenSSL进行RSA加密解密概念
- 利用openssl进行RSA加密解密
- 利用openssl进行RSA加密解密
- 利用openssl进行RSA加密解密实例
- 使用Java Base64解密算对openssl的base64加密字符串进行解密
- 使用OpenSSL进行DES加密
- VS中使用openssl/aes加密解密
- Openssl加密解密原理与命令使用
- 使用openssl命令进行加密解密及散列运算的命令行
- php使用openssl进行Rsa长数据加密(117)解密(128)
- C/C++使用openssl进行摘要和加密解密(md5, sha256, des, rsa)
- 使用C语言,对OpenSSL命令行加密的文件进行解密
- 续fcntl()之F_SETFD,F_GETFD
- iptables的time模块下载(适用于linux 2.6.18.1内核)
- linux的线程是否受到了保护?
- C++咬文嚼字-'Functions'
- linux socket的bug??
- 使用openssl api进行加密解密
- c++“反射”?
- 一个适用于windows和linux的抓包程序
- OGRE教程(一):SceneNode, Entity, SceneManager
- OGRE教程(二):Cameras, Lights, and Shadows
- 使用JspSmartUpload组件上传文件
- ogre的主要渲染流程
- JList 双击事件的实现
- HTML在线编辑器的基本概念