使用openssl 生成AES加密并且对加密结果进行base64加密

#include <stdio.h>  
#include <string.h>  
#include <stdlib.h>  
#include <stdio.h>  
#include <openssl/evp.h>  
#define ALAN_BASE64_H
#define RETURNMALLOC 1

/****************base64********************************************/


static char B64[64] =
{
'A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X',
'Y','Z',
'a','b','c','d','e','f','g','h',
'i','j','k','l','m','n','o','p',
'q','r','s','t','u','v','w','x',
'y','z',
'0','1','2','3','4','5','6','7',
'8','9','+','/'
};




char *base64_encode(char *s)
{
char *p = s, *e, *r, *_ret;
int len = strlen(s);
unsigned char unit[4], temp[3];

e = s + len;
len = len / 3 * 4 + 4 + 1;
r = _ret = (char *)malloc(len);

while (p < e) {
memset(temp,0,3);
if (e-p >= 3) {
memcpy(temp,p,3);
p += 3;
} else {
memcpy(temp,p,e-p);
p = e;
}

unit[0] = temp[0] >> 2;
unit[1] = temp[0] << 6;
unit[1] = (unit[1]>>2) | (temp[1]>>4);
unit[2] = temp[1] << 4;
unit[2] = (unit[2]>>2) | (temp[2]>>6);
unit[3] = temp[2] << 2;
unit[3] = unit[3] >> 2;
*r++ = B64[unit[0]];
*r++ = B64[unit[1]];
*r++ = (unit[2] ? B64[unit[2]] : '=');
*r++ = (unit[3] ? B64[unit[3]] : '=');
}
*r = 0;

#if RETURNMALLOC == 0
strcpy(s,_ret);
free(_ret);
_ret = s;
#endif //RETURNMALLOC

return _ret;
}


char *base64_decode(char *s)
{
char *p = s, *e, *r, *_ret;
int len = strlen(s);
unsigned char i, unit[4];

e = s + len;
len = len / 4 * 3 + 1;
r = _ret = (char *)malloc(len);

while (p < e) {
memcpy(unit,p,4);
if (unit[3] == '=')
unit[3] = 0;
if (unit[2] == '=')
unit[2] = 0;
p += 4;

for (i=0; unit[0]!=B64[i] && i<64; i++);
unit[0] = i==64 ? 0 : i;
for (i=0; unit[1]!=B64[i] && i<64; i++);
unit[1] = i==64 ? 0 : i;
for (i=0; unit[2]!=B64[i] && i<64; i++);
unit[2] = i==64 ? 0 : i;
for (i=0; unit[3]!=B64[i] && i<64; i++);
unit[3] = i==64 ? 0 : i;
*r++ = (unit[0]<<2) | (unit[1]>>4);
*r++ = (unit[1]<<4) | (unit[2]>>2);
*r++ = (unit[2]<<6) | unit[3];
}
*r = 0;

#if RETURNMALLOC == 0
strcpy(s,_ret);
free(_ret);
_ret = s;
#endif //RETURNMALLOC

return _ret;
}






/******************************************aes*****************************************/


void handleErrors(void)  
{  
  ERR_print_errors_fp(stderr);  
  abort();  
}  
  
int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,  
  unsigned char *iv, unsigned char *ciphertext)  
{  
  EVP_CIPHER_CTX *ctx;  
  
  int len;  
  
  int ciphertext_len;  
  
  /* Create and initialise the context */  
  if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();  
  
  /* Initialise the encryption operation. IMPORTANT - ensure you use a key 
   * and IV size appropriate for your cipher 
   * In this example we are using 256 bit AES (i.e. a 256 bit key). The 
   * IV size for *most* modes is the same as the block size. For AES this 
   * is 128 bits */  
  if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))  
    handleErrors();  
  
  /* Provide the message to be encrypted, and obtain the encrypted output. 
   * EVP_EncryptUpdate can be called multiple times if necessary 
   */  
  if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))  
    handleErrors();  
  ciphertext_len = len;  
  
  /* Finalise the encryption. Further ciphertext bytes may be written at 
   * this stage. 
   */  
  if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();  
  ciphertext_len += len;  
  
  /* Clean up */  
  EVP_CIPHER_CTX_free(ctx);  
  
  return ciphertext_len;  
}  
  
int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,  
  unsigned char *iv, unsigned char *plaintext)  
{  
  EVP_CIPHER_CTX *ctx;  
  
  int len;  
  
  int plaintext_len;  
  
  /* Create and initialise the context */  
  if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();  
  
  /* Initialise the decryption operation. IMPORTANT - ensure you use a key 
   * and IV size appropriate for your cipher 
   * In this example we are using 256 bit AES (i.e. a 256 bit key). The 
   * IV size for *most* modes is the same as the block size. For AES this 
   * is 128 bits */  
  if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))  
    handleErrors();  
  
  /* Provide the message to be decrypted, and obtain the plaintext output. 
   * EVP_DecryptUpdate can be called multiple times if necessary 
   */  
  if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))  
    handleErrors();  
  plaintext_len = len;  
  
  /* Finalise the decryption. Further plaintext bytes may be written at 
   * this stage. 
   */  
  if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();  
  plaintext_len += len;  
  
  /* Clean up */  
  EVP_CIPHER_CTX_free(ctx);  
  
  return plaintext_len;  
}  








/*****************************************main***********************************/
int main(int arc, char *argv[])  
{  
  /* Set up the key and iv. Do I need to say to not hard code these in a 
   * real application? :-) 
   */  
  
  /* A 256 bit key */  
  //unsigned char *key = "01234567890123456789012345678901";  
  unsigned char *key = "62ac6afdd993c395b953c50874e33b19";  
  
  /* A 128 bit IV */  
  //unsigned char *iv = "01234567890123456";  
  unsigned char *iv = "e593c395b953c508";  
  
  /* Message to be encrypted */  
  //unsigned char *plaintext = "The quick brown fox jumps over the lazy dog1234";  
  unsigned char *plaintext = "18811047170&from_list";  
  
  /* Buffer for ciphertext. Ensure the buffer is long enough for the 
   * ciphertext which may be longer than the plaintext, dependant on the 
   * algorithm and mode 
   */  
  unsigned char ciphertext[64];  
  
  /* Buffer for the decrypted text */  
  unsigned char decryptedtext[64];  
  
  int decryptedtext_len, ciphertext_len;  
  
  /* Initialise the library */  
/*  ERR_load_crypto_strings(); 
  OpenSSL_add_all_algorithms(); 
  OPENSSL_config(NULL);*/  
  
  printf("Plaintext is:\n%s~\n", plaintext);  
  
  /* Encrypt the plaintext */  
  ciphertext_len = encrypt(plaintext, strlen(plaintext), key, iv,  
    ciphertext);  
  
  /* Do something useful with the ciphertext here */  
  printf("Ciphertext is %d bytes long:\n", ciphertext_len);  
  printf("%s\n",base64_encode(ciphertext));
 // BIO_dump_fp(stdout, ciphertext, ciphertext_len);  
  
  /* Decrypt the ciphertext */  
  decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv, decryptedtext);  
  
  /* Add a NULL terminator. We are expecting printable text */  
  decryptedtext[decryptedtext_len] = '\0';  
  
  /* Show the decrypted text */  
  printf("Decrypted text is:\n");  
  printf("%s~\n", decryptedtext);  
  
  /* Clean up */  
  EVP_cleanup();  
  ERR_free_strings();  
  
  return 0;  
}  
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值