使用openssl api进行加密解密
2007-03-13 00:15
525 查看
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 |
相关文章推荐
- VC++使用DES算法进行文件加密和解密
- 使用Python进行AES加密和解密
- iOS中使用RSA对数据进行加密解密
- 使用openssl进行RSA加密解密
- iOS使用Security.framework进行RSA 加密解密签名和验证签名
- 使用Spring AOP对url参数进行解密以及对返回结果进行加密
- iOS中使用RSA对数据进行加密解密
- 使用java进行 AES 加密 解密
- PHP 使用 mcrypt 扩展中的 mcrypt_encrypt() 和 mcrypt_decrypt() 对数据进行加密和解密
- iOS中使用RSA对数据进行加密解密
- PHP使用DES进行加密和解密
- Qt使用异或进行加密解密
- iOS中使用RSA对数据进行加密解密
- 使用Dezender对zend加密后的php文件进行解密
- 使用Microsoft CryptoAPI进行加密、解密、签名及验证
- 使用Java Base64解密算对openssl的base64加密字符串进行解密
- 使用DES对资源进行加密解密
- 【VB.Net】使用SHA256进行简单的XOR文本加密与解密
- 如何使用Base64进行加密和解密
- JS使用插件cryptojs进行加密解密数据实例