Chinaunix首页 | 论坛 | 博客
  • 博客访问: 302971
  • 博文数量: 79
  • 博客积分: 3458
  • 博客等级: 中校
  • 技术积分: 921
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-25 17:09
个人简介

自、管

文章分类

全部博文(79)

文章存档

2013年(7)

2012年(20)

2011年(18)

2010年(34)

分类: LINUX

2011-03-10 14:06:49

/*
* aes.cc
* - Show the usage of AES encryption/decryption
*/

#include
#include
#include
#include

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 \n", argv[0]);
        exit(-1);
    }

    // set the encryption length
    len = 0;
    if ((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
    for (i=0i<16++i{
        key[i] = 32 + i;
    }

    // Set encryption key
    for (i=0i<AES_BLOCK_SIZE++i{
        iv[i] = 0;
    }
    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
    for (i=0i<AES_BLOCK_SIZE++i{
        iv[i] = 0;
    }
    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=0i<len++i{
        printf("%x%x", (encrypt_string[i] >> 4& 0xf, 
                encrypt_string[i] & 0xf);    
    }
    printf("\n");
    printf("decrypted string = %s\n", decrypt_string);

    return 0;
}

Makefile

CC=g++
CFLAGS=-Wall -g -O2
LIBS=-lcrypto

all: aes

aes: aes.cc
    $(CC$(CFLAGS) aes.cc -o $@ $(LIBS)

clean:
    @rm -f aes

阅读(4493) | 评论(2) | 转发(1) |
给主人留下些什么吧!~~

yshiilu2012-05-22 08:47:45

请问博主安装的是哪个版本的openssl呢

chinaunix网友2011-03-27 18:41:17

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com