利用openssl实现字符串加密解密
来源:互联网 发布:淘宝商品侵权如何处理 编辑:程序博客网 时间:2024/06/10 05:28
#include <string.h>#include <stdio.h>#include <stdlib.h>#include <openssl/aes.h>#include <openssl/evp.h>#define EVP_DES_CBC EVP_des_cbc()//#define EVP_DES_CBC EVP_aes_128_cbc();#define MAX_CHAR_SIZE 512unsigned char *decrypt_text(unsigned char *iv, unsigned char *key, unsigned char *ciphertext,int *ciphertext_len,unsigned char* plaintext) { EVP_CIPHER_CTX de; EVP_CIPHER_CTX_init(&de); const EVP_CIPHER *cipher_type; int bytes_written = 0; int update_len = 0; cipher_type = EVP_DES_CBC;// rc = EVP_CIPHER_CTX_set_key_length(&de, strlen(pstRedirectConf->key)); EVP_DecryptInit_ex(&de, cipher_type, NULL, key, iv); if(!EVP_DecryptInit_ex(&de, NULL, NULL, NULL, NULL)){ printf("ERROR in EVP_DecryptInit_ex \n"); return NULL; } int plaintext_len = 0; if(!EVP_DecryptUpdate(&de, plaintext, &update_len, ciphertext, *ciphertext_len)){ printf("ERROR in EVP_DecryptUpdate\n"); return NULL; } if(!EVP_DecryptFinal_ex(&de, plaintext + update_len, &bytes_written)){ printf("ERROR in EVP_DecryptFinal_ex\n"); return NULL; } bytes_written += update_len; *(plaintext+bytes_written) = '\0'; printf("out_buf(%d->%d) : %s\n", *ciphertext_len,bytes_written, plaintext); EVP_CIPHER_CTX_cleanup(&de); return plaintext;}unsigned char *encrypt_text(unsigned char *iv, unsigned char *key, unsigned char *plaintext,int *ciphertext_len,unsigned char *ciphertext ) { EVP_CIPHER_CTX en; EVP_CIPHER_CTX_init(&en); const EVP_CIPHER *cipher_type; int input_len = 0; // cipher_type = EVP_aes_128_cbc(); cipher_type = EVP_DES_CBC; //init cipher EVP_EncryptInit_ex(&en, cipher_type, NULL, key, iv); // We add 1 because we're encrypting a string, which has a NULL terminator // and want that NULL terminator to be present when we decrypt.// input_len = strlen(plaintext) + 1; input_len = strlen(plaintext); /* allows reusing of 'e' for multiple encryption cycles */ if(!EVP_EncryptInit_ex(&en, NULL, NULL, NULL, NULL)){ printf("ERROR in EVP_EncryptInit_ex \n"); return NULL; } // This function works on binary data, not strings. So we cast our // string to an unsigned char * and tell it that the length is the string // length + 1 byte for the null terminator. int bytes_written = 0; //encrypt if(!EVP_EncryptUpdate(&en, ciphertext, &bytes_written, (unsigned char *) plaintext, input_len ) ) { return NULL; } *ciphertext_len += bytes_written; //do padding if(!EVP_EncryptFinal_ex(&en, ciphertext + bytes_written, &bytes_written)){ printf("ERROR in EVP_EncryptFinal_ex \n"); return NULL; } *ciphertext_len += bytes_written; int i = 0; printf("encrypt string: "); for( i =0;i < *ciphertext_len; i++) printf("%.02x", ciphertext[i]); printf("\n"); //cleanup EVP_CIPHER_CTX_cleanup(&en); return ciphertext;}int main(int argc, char **argv) { unsigned char * in = "hello world,yesterday once more!!!!!!!!!!!";// static char *in="Once More Yesterday"; printf("Input: %s\n", in); unsigned char * out = NULL; unsigned char * final = NULL; //out = (unsigned char *) malloc(strlen(in)); unsigned char * iv = "aaaaaaaaaaaaaaaa"; unsigned char * key = "bbbbbbbbbbbbbbbb"; int ciphertext_len = 0; unsigned char ciphertext[MAX_CHAR_SIZE]; unsigned char plaintext[MAX_CHAR_SIZE]; out = encrypt_text(iv, key, in, &ciphertext_len,ciphertext); printf("in: %s([%d] - > out:[%d])\n", in,strlen(in) ,ciphertext_len); final = decrypt_text(iv, key, out,&ciphertext_len,plaintext); printf("final: %s[%d]\n", final,strlen(final)); return 0;}
0 0
- 利用openssl实现字符串加密解密
- 利用openssl进行RSA加密解密
- 利用openssl进行RSA加密解密
- 利用openssl加密解密tar包
- 利用openssl加密解密tar包
- 利用openssl进行RSA加密解密
- 利用openssl进行RSA加密解密
- 利用openssl进行RSA加密解密实例
- 利用openssl实现MD5加密
- 通过 JNI 调用 OpenSSL 实现加密解密
- 利用OpenSSL 实现明文加解密
- .net 实现字符串加密解密
- Linux下利用openssl对文件进行加密和解密
- Linux下利用openssl对文件进行加密和解密
- linux下面C 利用openssl的AES库加密,解密
- PKI加密解密 OpenSSL
- OpenSSl 加密解密 示例
- openssl base64加密解密
- 机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理)
- 陈怡暖:2015.5.19早间最强现货黄金白银操作建议
- multiple-media streaming on p2p overlay across oversea network
- 空密码访问共享
- Android开发之时间刻度盘
- 利用openssl实现字符串加密解密
- python(第五天):__getattr__ 和 __getattribute__
- java 虚拟机 垃圾回收器原理和使用总结
- const 和 #define区别
- 一步一步学会http获取tomcat服务端的图片,在android客户端显示
- js 直接添加属性
- Ubuntu 完全卸载Apache2
- IO 异常读请求分析
- weblogic突然挂掉后nodemanager起不来