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

#include 
#include 
#include 
#include 
#include 

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

你可能感兴趣的:(C语言)