使用openssl api进行加密解密

本文介绍了一个使用OpenSSL库实现的简单文件加密程序。该程序通过C语言实现了对文件内容的加密和解密功能,并详细展示了如何初始化加密上下文、设置密钥和初始化向量等关键步骤。
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

 
OpenSSL是一个开源的加密库,它提供了RSA加密解密、签名和验签的功能。 对于RSA加密解密,我们可以使用OpenSSL提供的命令行工具或者API来实现。 使用命令行工具,我们可以通过以下命令进行RSA加密openssl rsautl -encrypt -in <input file> -out <output file> -inkey <public key file> -pubin 其中,<input file>是要加密的文件,<output file>是加密后的文件,<public key file>是存储公钥的文件,-pubin参数表示输入的是公钥。 使用命令行工具,我们可以通过以下命令进行RSA解密openssl rsautl -decrypt -in <input file> -out <output file> -inkey <private key file> 其中,<input file>是要解密的文件,<output file>是解密后的文件,<private key file>是存储私钥的文件。 对于RSA签名和验签,我们可以使用以下命令进行签名: openssl rsautl -sign -in <input file> -out <output file> -inkey <private key file> 其中,<input file>是要签名的文件,<output file>是签名后的文件,<private key file>是存储私钥的文件。 使用以下命令进行验签: openssl rsautl -verify -in <input file> -out <output file> -inkey <public key file> -pubin 其中,<input file>是要验签的文件,<output file>是验签后的文件,<public key file>是存储公钥的文件,-pubin参数表示输入的是公钥。 使用OpenSSLAPI进行RSA加密解密、签名和验签的操作也是类似的,我们可以通过调用相应的函数来实现。需要注意的是,API使用需要在代码中显式引入OpenSSL的头文件和链接OpenSSL的库文件。 总之,OpenSSL提供了便捷的工具和API来实现RSA加密解密、签名和验签的功能,无论是命令行工具还是API,都可以选择合适的方式进行操作。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值