Download library file
After downloading, unzip the file as follows,
compilelibtommath
Because
libtomcrypt
the library1.0.6
does not provide a math library interface since the version, there will be a lack of dependencies on third-party libraries, so we need to compilelibtommath
alib
static library file of and add it tolibtomcrypt
the project
First click on ltm-1.2.0
the folder and open libtommath_VS2008.sln
the solution
His library is a solution generated VS2008
by , and I use it myself VS2022
, so it will prompt to upgrade, just confirm it
Just enter the project world and directly generate the solution
If you see the generation below ..\MSVC_x64_Debug\tommath.lib
, it means success
Then find this lib
file , it is recommended to create a new folder lib
and tommath.lib
put in it
Then go to the root directory of the project file, and you can find some header files. It is recommended to create a new folder headers
and put these header files in, and then put lib
the headers
and folders at the same level, so that you can find them easily.
To do this, the file structure should look like this
AES_build
│
├─headers
│ tommath.h
│ tommath_class.h
│ tommath_cutoffs.h
│ tommath_private.h
│ tommath_superclass.h
│
└─lib
tommath.lib
compile libtomcrypt
library
Click on crypt-1.18.2
the folder and open libtomcrypt_VS2008.sln
the solution
As before, it also prompts to upgrade
Open project properties
包含目录
Add the content in your own file just nowheaders
库目录
Add in your own folder insidelib
and then generate the solution
It can be seen libtomcrypt.lib
that it has been successfully generated
Find this static library file and put it in lib
the folder
Go to ..\src\headers
the following to find libtomcrypt
the header file of the library, and put it in that headers
folder
To do this, the file structure should be like this
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
AES encryption experiment using libtomcrypt
the library
Create an experimental project by yourself, mine is calledAES_Test
Enter the project properties interface, edit 附加包含目录
, headers
put in
Edit 链接器
it 附加库目录
, put lib
the folder in it
edited 链接器
under附加依赖项
Edit and add the file names of the two static library files lib
inside
After completing the settings, you can see many header files in the library include
that
Attach the project code (refer libtomcrypt
to aes.c
the instance function in the library)
The comment inside is the file encryption and decryption, and it has not been successfully debugged yet.
#include <stdio.h>
#include <tchar.h>
#include <tomcrypt.h>
#include <math.h>
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
}
Test Results