下载库文件

libtomcrypt下载链接

libtomath下载链接

下载好之后解压文件如下,

编译 libtommath

因为 教材上面的 libtomcrypt 库自 1.0.6 版本之后就不提供数学库接口了,所以会缺少第三方库的依赖,因此我们需要先编译一个 libtommath 的 lib 静态库文件,添加进 libtomcrypt 项目

先点开 ltm-1.2.0 文件夹,打开 libtommath_VS2008.sln 解决方案

他这个库是用 VS2008 生成的解决方案,我自己用的是 VS2022 所以会提示升级,确定就行了

进入项目界直接生成解决方案就行了

看到底下生成 ..\MSVC_x64_Debug\tommath.lib 就说明成功了

然后去找到这个 lib 文件,建议新建一个文件夹 lib 然后把这个 tommath.lib 放进去

然后再到项目文件根目录下,能找到一些头文件,建议也是新建一个文件夹 headers 然后把这些头文件放进去,然后把 lib 和 headers 两个文件夹放到同一级,方便找到

做到这一步,文件结构应该是这样的

AES_build

├─headers

│ tommath.h

│ tommath_class.h

│ tommath_cutoffs.h

│ tommath_private.h

│ tommath_superclass.h

└─lib

tommath.lib

编译 libtomcrypt 库

点开 crypt-1.18.2 文件夹,打开 libtomcrypt_VS2008.sln 解决方案

和之前一样,也是提示升级

打开项目属性

在 包含目录 里面加上刚刚自己文件里面的 headers

在 库目录 里面加上自己文件夹里面的 lib

然后生成解决方案

可以看到 libtomcrypt.lib 已经成功生成了

去找到这个静态库文件,和之前一样放到自己创建的 lib 文件夹里面

去 ..\src\headers 下面找到 libtomcrypt 库的头文件,也放到那个 headers 文件夹里面

做到这步,文件结构应该是这样的

AES_build

│ tree.txt

├─headers

│ tomcrypt.h

│ tomcrypt_argchk.h

│ tomcrypt_cfg.h

│ tomcrypt_cipher.h

│ tomcrypt_custom.h

│ tomcrypt_hash.h

│ tomcrypt_mac.h

│ tomcrypt_macros.h

│ tomcrypt_math.h

│ tomcrypt_misc.h

│ tomcrypt_pk.h

│ tomcrypt_pkcs.h

│ tomcrypt_prng.h

│ tommath.h

│ tommath_class.h

│ tommath_cutoffs.h

│ tommath_private.h

│ tommath_superclass.h

└─lib

tomcryptd.lib

tommath.lib

利用 libtomcrypt 库进行AES加密实验

自己创建一个实验项目,我的这个叫 AES_Test

进入项目属性界面,编辑 附加包含目录 ,把 headers 放进去

编辑 链接器 下的 附加库目录, 把 lib 文件夹放进去

编辑 链接器 下的 附加依赖项

在里面编辑加入 lib 里面的两个静态库文件的文件名

完成设置之后,项目代码里面 include 的时候就能看到可以调用库里面的很多头文件了

附上项目代码(参考论 libtomcrypt 库里面 aes.c 中的实例函数 )

注释里面的是文件加密解密,还没调试成功

#include

#include

#include

#include

int main(void)

{

#ifndef LTC_TEST

return CRYPT_NOP;

#else

int err;

static const struct {

int keylen;

unsigned char key[32], pt[16], ct[16];

} tests[] = {

{ 16,

{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,

0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },

{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,

0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },

{ 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30,

0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a }

}, {

24,

{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,

0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,

0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 },

{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,

0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },

{ 0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0,

0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91 }

}, {

32,

{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,

0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,

0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,

0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },

{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,

0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },

{ 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf,

0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89 }

}

};

symmetric_key key;

unsigned char tmp[2][16];

int i, y, p;

for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {

zeromem(&key, sizeof(key));

if ((err = rijndael_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {

return err;

}

rijndael_ecb_encrypt(tests[i].pt, tmp[0], &key);

rijndael_ecb_decrypt(tmp[0], tmp[1], &key);

if (compare_testvector(tmp[0], 16, tests[i].ct, 16, "AES Encrypt", i) ||

compare_testvector(tmp[1], 16, tests[i].pt, 16, "AES Decrypt", i)) {

return CRYPT_FAIL_TESTVECTOR;

}

/* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */

for (y = 0; y < 16; y++) tmp[0][y] = 0;

//for (y = 0; y < 16; y++) printf("%x", tmp[0][y]);

for (y = 0; y < 20; y++) {

rijndael_ecb_encrypt(tmp[0], tmp[0], &key);

printf("%d:", y+1);

for (p = 0; p < 16; p++) printf("%x", tmp[0][p]);

printf("\n");

}

for (y = 0; y < 20; y++) {

rijndael_ecb_decrypt(tmp[0], tmp[0], &key);

printf("%d:", y + 1);

for (p = 0; p < 16; p++) printf("%x", tmp[0][p]);

printf("\n");

}

for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;

printf("end...\n");

}

//symmetric_CBC cbcAES = { 0 };

//size_t len = 0;

//int error = 0;

//int index = 0;

//FILE* filePlainText = 0, * fileCipherText = 0, * decryptFile = 0;//文件

//unsigned char ct[16] = {0},pt[16] = {0};//加解密数据块 size_t len = 0;

//index = find_cipher("aes");

//unsigned char key[16] = { "12345678" };

//unsigned char bar[16] = { 0 };

//register_cipher(&aes_desc);

//cbc_start(index, bar, key, sizeof(key), 0, &cbcAES);

//filePlainText = fopen("D:/plaintext.txt", "r");

//fileCipherText = fopen("D:/ciphertext.txt", "w");

//while (!feof(filePlainText)) {

// memset(pt, 0, sizeof(pt));

// memset(ct, 0, sizeof(ct));

// len = fread(pt, sizeof(pt[0]), 16, filePlainText);

// if (len < 1){//没有读成功 break; //加密

// error = cbc_encrypt(pt, ct, 16, &cbcAES);

// }

// fwrite(ct, sizeof(ct[0]), 16, fileCipherText);

//}

//fclose(filePlainText);

//fclose(fileCipherText);

//cbc_done(&cbcAES);

解密

//cbc_start(index, bar, key, sizeof(key), 0, &cbcAES);

打开要解密的文件

//fileCipherText = fopen("D:/ciphertext.txt", "r");

创建解密后的文件

//decryptFile = fopen("D:/decrypttext.txt", "w");

//while (!feof(fileCipherText)) {

// memset(pt, 0, sizeof(pt));

// memset(ct, 0, sizeof(ct));

// len = fread(ct, sizeof(ct[0]), 16, fileCipherText);

// if (len < 1) {//没有读成功 break; //解密

// error = cbc_decrypt(ct, pt, 16, &cbcAES);

// }

// fwrite(pt, sizeof(pt[0]), 16, decryptFile);

//}

关闭文件

//fclose(fileCipherText);

//fclose(decryptFile); //完成流加解密 cbc_done(&cbcAES); //end 解密流程

//cbc_done(&cbcAES);

return CRYPT_OK;

#endif

}

测试结果

好文推荐

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。