Libtomcrypt密码库的使用

下载库文件

libtomcrypt下载链接

libtomath下载链接

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

image-20220413132244772

编译 libtommath

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

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

image-20220413133301804

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

image-20220413133638695

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

image-20220413133847614

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

image-20220413134027348

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

image-20220413134154154

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

image-20220413134437758

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

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 解决方案

image-20220413151135758

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

image-20220413151156842

打开项目属性

image-20220413151424137

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

image-20220413151747611

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

image-20220413152036790

然后生成解决方案

image-20220413152104737

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

image-20220413152158754

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

image-20220413152319449

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

image-20220413152354128

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

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

image-20220413152833223

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

image-20220413152942281

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

image-20220413153124676

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

image-20220413153302088

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

image-20220413153404657

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

image-20220413153654062

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

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

#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
}


测试结果

image-20220413154427205

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
LibTomCrypt is a fairly comprehensive, modular and portable cryptographic toolkit that provides developers with a vast array of well known published block ciphers, one-way hash functions, chaining modes, pseudo-random number generators, public key cryptography and a plethora of other routines. LibTomCrypt has been designed from the ground up to be very simple to use. It has a modular and standard API that allows new ciphers, hashes and PRNGs to be added or removed without change to the overall end application. It features easy to use functions and a complete user manual which has many source snippet examples. LibTomCrypt is free for all purposes under the public domain. This includes commercial use, redistribution and even branching. Sports the following Public domain and open source. Written entirely in portable ISO C source (except for things like RNGs for natural reasons) Builds out of the box on virtually every box. All that is required is GCC for the source to build. Includes a 180+ page user manual in PDF format (with working examples in it) Block Ciphers Ciphers come with an ECB encrypt/decrypt, setkey and self-test interfaces. All ciphers have the same prototype which facilitates using multiple ciphers at runtime. Some of the ciphers are flexible in terms of code size and memory usage. Ciphers Supported. Blowfish XTEA RC5 RC6 SAFER+ Rijndael (aka AES) Twofish SAFER (K64, SK64, K128, SK128) RC2 DES, 3DES CAST5 Noekeon Skipjack Anubis (with optional tweak as proposed by the developers) Khazad KASUMI SEED Chaining Modes Modes come with a start, encrypt/decrypt and set/get IV interfaces. Mode supported. ECB CBC OFB CFB CTR IEEE LRW mode F8 Chaining Mode One-Way Hash Functions Hashes come with init, process, done and self-test interfaces. All hashes use the same prototypes for the interfaces. Hashes supported. MD2 MD4 MD5 SHA-1 SHA-224/256/384/512 TIGER-192 RIPE-MD 128/160/256/320 WHIRLPOOL Message Authenticat
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值