Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > 使用openssl api进行加密解密
【标  题】:使用openssl api进行加密解密
【关键字】:openssl,api
【来  源】:http://www.cublog.cn/u/1574/showart.php?id=231015

使用openssl api进行加密解密

Your Ad Here
openssl库是个好东西!
 
 

[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

Socket Introduction:【上一篇】
组播:【下一篇】
【相关文章】
  • Win32 APIs 资源 for .net
  • java API概述
  • EJB3 Java 持久化API来标准化Java的持久化学习笔记
  • DEBUG API写简单的Loader
  • 使用C#查询路由接口,同时小议一些.net 2.0的诡异API
  • 围棋程序 提子算法 WIN32 API
  • JDK6.0的新特性之六:插入式注解处理API(Pluggable Annotation Processing API)
  • 讽刺&Google搜索API
  • JDK6.0的新特性之四:使用Compiler API
  • Windows 2000下Api函数的拦截分析
  • 【随机文章】
  • 抵挡DoS加密远程连接让网络更安全
  • [shell][最简单的] 写个bash游戏
  • UNIX System V 文件系统S5FS的抽象数据结构总结
  • XML-实例讲解
  • 在数据库中不用 EOF 以加快记录循环
  • SQLServer2000 的sa密码忘记之后的解决方法
  • ES310-chapter4:VMSA Software
  • 用PHP将mysql数据表转换为excel文件格式
  • 检查代码是否存在整数操作安全漏洞
  • 电脑操作最忌讳的十八个小动作
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.