Use of Libtomcrypt password library

Download library file

libtomcrypt download link

libtomath download link

After downloading, unzip the file as follows,

image-20220413132244772

compilelibtommath

Because libtomcryptthe library 1.0.6does not provide a math library interface since the version, there will be a lack of dependencies on third-party libraries, so we need to compile libtommatha libstatic library file of and add it to libtomcryptthe project

First click on ltm-1.2.0the folder and open libtommath_VS2008.slnthe solution

image-20220413133301804

His library is a solution generated VS2008by , and I use it myself VS2022, so it will prompt to upgrade, just confirm it

image-20220413133638695

Just enter the project world and directly generate the solution

image-20220413133847614

If you see the generation below ..\MSVC_x64_Debug\tommath.lib, it means success

image-20220413134027348

Then find this libfile , it is recommended to create a new folder liband tommath.libput in it

image-20220413134154154

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 headersand put these header files in, and then put libthe headersand folders at the same level, so that you can find them easily.

image-20220413134437758

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 libtomcryptlibrary

Click on crypt-1.18.2the folder and open libtomcrypt_VS2008.slnthe solution

image-20220413151135758

As before, it also prompts to upgrade

image-20220413151156842

Open project properties

image-20220413151424137

包含目录Add the content in your own file just nowheaders

image-20220413151747611

库目录Add in your own folder insidelib

image-20220413152036790

and then generate the solution

image-20220413152104737

It can be seen libtomcrypt.libthat it has been successfully generated

image-20220413152158754

Find this static library file and put it in libthe folder

image-20220413152319449

Go to ..\src\headersthe following to find libtomcryptthe header file of the library, and put it in that headersfolder

image-20220413152354128

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 libtomcryptthe library

Create an experimental project by yourself, mine is calledAES_Test

image-20220413152833223

Enter the project properties interface, edit 附加包含目录, headersput in

image-20220413152942281

Edit 链接器it 附加库目录, put libthe folder in it

image-20220413153124676

edited 链接器under附加依赖项

image-20220413153302088

Edit and add the file names of the two static library files libinside

image-20220413153404657

After completing the settings, you can see many header files in the library includethat

image-20220413153654062

Attach the project code (refer libtomcryptto aes.cthe 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

image-20220413154427205

おすすめ

転載: blog.csdn.net/SimoSimoSimo/article/details/125157768