A test .c file for aes
来源:互联网 发布:数据挖掘实例 编辑:程序博客网 时间:2024/06/07 01:25
/* crypto/aes/aes_wrap.c */
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL* project.
*/
/* ====================================================================
* Copyright (c) 2008 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*/
#include "cryptlib.h"
#include <openssl/aes.h>
#include <openssl/bio.h>
int main(int argc, char **argv)
{
ifndef NULL
#define NULL (void*)0
#endif
FILE * fp;
unsigned char * keybuff = NULL;
unsigned char * datbuff = NULL;
int flen;
int keylen;
int datlen;
if (NULL == (fp = fopen("/data/mykey.txt", "rb"))) {
return 0;
} else {
fseek(fp, 0, SEEK_END);
keylen = flen = ftell(fp);
fseek(fp, 0, SEEK_SET);
keybuff = malloc(flen + 8);
if (NULL == keybuff) {
fclose(fp);
return 0;
} else {
memset(keybuff, 0, flen + 8);
fread(keybuff, flen, 1, fp);
fclose(fp);
}
}
if (NULL == (fp = fopen("/data/mydat.txt", "rb"))) {
free(keybuff);
return 0;
} else {
fseek(fp, 0, SEEK_END);
datlen = flen = ftell(fp);
fseek(fp, 0, SEEK_SET);
datbuff = malloc(flen + 8);
if (NULL == datbuff) {
fclose(fp);
free(keybuff);
return 0;
} else {
memset(datbuff, 0, flen + 8);
fread(datbuff, flen, 1, fp);
fclose(fp);
}
}
ret = AES_wrap_unwrap_test(keybuff, 128, NULL, e1, datbuff, 16);
fprintf(stderr, "My test result %d\n", ret);
ret = AES_wrap_unwrap_test(keybuff, 256, NULL, e6, datbuff, 32);
fprintf(stderr, "My test result %d\n", ret);
ret = AES_wrap_unwrap_test(keybuff, 128, NULL, e1, key, 16);
fprintf(stderr, "My test result %d\n", ret);
ret = AES_wrap_unwrap_test(keybuff, 256, NULL, e6, key, 32);
fprintf(stderr, "My test result %d\n", ret);
//free(keybuff);
//free(datbuff);
{
AES_KEY aes;
unsigned char keys[AES_BLOCK_SIZE]; // AES_BLOCK_SIZE = 16
unsigned char iv[AES_BLOCK_SIZE]; // init vector
unsigned char* input_string = datbuff;
unsigned char* encrypt_string;
unsigned char* decrypt_string;
unsigned int len; // encrypt length (in multiple of AES_BLOCK_SIZE)
unsigned int i;
// set the encryption length
len = 0;
if (datlen % AES_BLOCK_SIZE == 0) {
len = datlen + 1;
} else {
len = ((datlen + 1) / AES_BLOCK_SIZE + 1) * AES_BLOCK_SIZE;
}
// set the input string
input_string = datbuff;
// Generate AES 128-bit key
for (i=0; i<16; ++i) {
keys[i] = keybuff[i];// 32 + i;
}
// Set encryption key
for (i=0; i<AES_BLOCK_SIZE; ++i) {
iv[i] = 0;
}
if (AES_set_encrypt_key(keys, 128, &aes) < 0) {
fprintf(stderr, "Unable to set encryption keys in AES/n");
goto myerr;
}
// alloc encrypt_string
encrypt_string = (unsigned char*)malloc(len);
if (encrypt_string == NULL) {
fprintf(stderr, "Unable to allocate memory for encrypt_string/n");
goto myerr;
}
// encrypt (iv will change)
AES_cbc_encrypt(input_string, encrypt_string, len, &aes, iv, AES_ENCRYPT);
// alloc decrypt_string
decrypt_string = (unsigned char*)malloc(len);
if (decrypt_string == NULL) {
fprintf(stderr, "Unable to allocate memory for decrypt_string/n");
goto myerr;
}
// Set decryption key
for (i=0; i<AES_BLOCK_SIZE; ++i) {
iv[i] = 0;
}
if (AES_set_decrypt_key(keys, 128, &aes) < 0) {
fprintf(stderr, "Unable to set decryption keys in AES/n");
goto myerr;
}
// decrypt
AES_cbc_encrypt(encrypt_string, decrypt_string, len, &aes, iv, AES_DECRYPT);
printf("----- input_string = %s -----\n", input_string);
printf("\n----- encrypted string = -----\n");
for (i=0; i<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;
}
myerr:
free(keybuff);
free(datbuff);
return 0;
}
本文由 http://blog.csdn.net/tang_fu/article/details/6554767 修改,在Android上调试和测试
- A test .c file for aes
- CUnit - A Unit Test Framework For C
- just a test for my bolg first file
- Just for a test!...
- NO.1 test a file
- How do I set file.encoding for a junit test in ant?
- How do I set file.encoding for a junit test in ant?
- How do I set file.encoding for a junit test in ant?
- A Test for CSDN blogger
- test process for a project
- a small test for array
- Error 'Incorrect key file for table 'test'
- how to test a file 's existence
- this is a test file thanks!
- Unit Test summary for C
- A 'C' Test: The 0x10 Best Questions for Would-be Embedded Programmers
- A 'C' Test: The 0x10 Best Questions for Would-be Embedded Programmers
- A 'C' Test: The 0x10 Best Questions for Would-be Embedded Programmers
- OT源代码的分析,OrtHello 迟早攻破你 (十)第5个例子,用代码来创建物体(未完)
- QtWebkit2.2为HTML5.0而来
- 猜数字游戏(html)
- android 定时器
- Ext Js 3.2 store的用法学习
- A test .c file for aes
- JQuery easyui 获取 datebox 的值
- mysql导入导出数据库表命令行
- 安卓第五天——Activity生命周期
- 站长朋友搞seo应该具备哪些策略(转载)
- WikiPedia技术架构学习笔记
- Java中的泛型
- 未来的平板电脑是透明的
- mfc 设置光标一法