利用openssl实现字符串加密解密

#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 512

unsigned 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值