AES加密

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/aes.h>
 
int  main( int  argc,  char ** argv) {
     AES_KEY aes;
     unsigned  char  key[AES_BLOCK_SIZE];         // AES_BLOCK_SIZE = 16
     unsigned  char  iv[AES_BLOCK_SIZE];         // init vector
     unsigned  char * input_string;
     unsigned  char * encrypt_string;
     unsigned  char * decrypt_string;
     unsigned  int  len;         // encrypt length (in multiple of AES_BLOCK_SIZE)
     unsigned  int  i;
 
     // check usage
     if  (argc != 2) {
         fprintf (stderr,  "%s <plain text>\n" , argv[0]);
         exit (-1);
     }
 
     // set the encryption length
     len = 0;
     if  strlen (argv[1])>=AES_BLOCK_SIZE || 
          ( strlen (argv[1]) + 1) % AES_BLOCK_SIZE == 0) {
         len =  strlen (argv[1]) + 1;
     else  {
         len = (( strlen (argv[1]) + 1) / AES_BLOCK_SIZE + 1) * AES_BLOCK_SIZE;
     }
 
     // set the input string
     input_string = (unsigned  char *) calloc (len,  sizeof (unsigned  char ));
     if  (input_string == NULL) {
         fprintf (stderr,  "Unable to allocate memory for input_string\n" );
         exit (-1);
     }
     strncpy (( char *)input_string, argv[1],  strlen (argv[1]));
     
     // Generate AES 128-bit key
     memset (key, 0x01, AES_BLOCK_SIZE);
 
     // Set encryption key
     memset (iv, 0x01, AES_BLOCK_SIZE);
     if  (AES_set_encrypt_key(key, 128, &aes) < 0) {
         fprintf (stderr,  "Unable to set encryption key in AES\n" );
         exit (-1);
     }
 
     // alloc encrypt_string
     encrypt_string = (unsigned  char *) calloc (len,  sizeof (unsigned  char ));    
     if  (encrypt_string == NULL) {
         fprintf (stderr,  "Unable to allocate memory for encrypt_string\n" );
         exit (-1);
     }
 
     // encrypt (iv will change)
     AES_cbc_encrypt(input_string, encrypt_string, len, &aes, iv, AES_ENCRYPT);
 
     /
     
     // alloc decrypt_string
     decrypt_string = (unsigned  char *) calloc (len,  sizeof (unsigned  char ));
     if  (decrypt_string == NULL) {
         fprintf (stderr,  "Unable to allocate memory for decrypt_string\n" );
         exit (-1);
     }
     
     // Set decryption key
     memset (iv, 0x01, AES_BLOCK_SIZE);
     if  (AES_set_decrypt_key(key, 128, &aes) < 0) {
         fprintf (stderr,  "Unable to set decryption key in AES\n" );
         exit (-1);
     }
 
     // decrypt
     AES_cbc_encrypt(encrypt_string, decrypt_string, len, &aes, iv, 
             AES_DECRYPT);
 
     // print
     printf ( "input_string =%s\n" , input_string);
     printf ( "encrypted string =" );
     for  (i=0; i<len; ++i) {
         printf ( "%u " , encrypt_string[i]);    
     }
     printf ( "\n" );
     printf ( "decrypted string =%s\n" , decrypt_string);
 
     return  0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值