LibTomCrypts算法库基于Visual Stduio 2019

LibTomCrypts算法库基于Visual Stduio 2019

最近在编写密码学作业的过程中学习了如何使用LibTomCrypts这个密码算法库,发现许多同学往往卡死在第一步,即如何在Visual Studio 2019环境下配置LibTomCrypts库,下面是我的一些解决方案,并且也已经将编译好的文件打包上传至Gitee(为了方便下载)
链接: LibTomCrypts-VisualStudio.

一、准备工作

从Github将两个开源的代码库下载下来

git clone https://github.com/libtom/libtommath.git
git clone https://github.com/libtom/libtomcrypt.git

如果下载比较慢的话,也可以从我给的Gitee地址里面直接下载

git clone https://gitee.com/zeroaone/lib-tom-crypts-visual-studio.git

解压并得到两个源码文件夹:

  • libtomcrypt-1.18.2
  • libtommath-1.0

二、编译libtommath

libtommath是libtomcrypt所依赖的库,我们需要首先编译libtommath库,我们首先双击libtommath_VS2008.sln,用Visual Studio打开项目,之后Visual Studio会自己对项目管理进行升级

之后若我们需要编译为32位程序使用的库,只需要将编译模式选择为Release,平台选择Win32,诺需要编译64位则重新配置x64平台,现在开始编译

......
1>正在生成代码...
1>正在编译...
1>bn_s_mp_sub.c
1>正在生成代码...
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppBuild.targets(1523,5): warning MSB8012: TargetPath(D:\下载\build\libtommath-1.0\Release\libtommath.lib) 与 Library 的 OutputFile 属性值(D:\下载\build\libtommath-1.0\Release\tommath.lib)不匹配。这可能导致项目生成不正确。若要更正此问题,请确保 $(OutDir)$(TargetName)$(TargetExt) 属性值与 %(Lib.OutputFile) 中指定的值匹配。
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppBuild.targets(1525,5): warning MSB8012: TargetName(libtommath) 与 Library 的 OutputFile 属性值(tommath)不匹配。这可能导致项目生成不正确。若要更正此问题,请确保 $(OutDir)$(TargetName)$(TargetExt) 属性值与 %(Lib.OutputFile) 中指定的值匹配。
1>libtommath_VS2008.vcxproj -> D:\下载\build\libtommath-1.0\Release\libtommath.lib
1>已完成生成项目“libtommath_VS2008.vcxproj”的操作。
========== 生成: 成功 1 个,失败 0 个,最新 0 个,跳过 0 个 ==========

之后我们可以在libtommath-1.0文件夹发现release文件夹,进入之后可以找到一个文件叫tommath.lib,这就是我们需要的静态库文件夹了,至此libtommath库编译完成,我们还需要libtommath-1.0文件夹里面的几个头文件,分别是:

  • tommath.h
  • tommath_class.h
  • tommath_private.h
  • tommath_superclass.h

这里建议新建一个名叫为build文件夹,里面有两个子文件夹:

  • lib:用来存放编译好的静态库文件
  • headers:用来存放需要用到的头文件

将这几个头文件放入headers文件夹,tommath.lib放入lib文件夹

三、编译libtomcrypt

下面的重头戏是开始编译libtomcrypt库,这里比较麻烦的是需要配置Visual Stduio的包含目录库目录

这里我们进入libtomcrypt-1.18.2文件夹,然后点击libtomcrypt_VS2008.sln文件启动编译项目,在Visual Stduio里打开解决方案资源管理器,右键libtomcrypt选择属性

点击VC++ 目录,在包含目录里面编辑加入我们之前创建的headers文件夹,在库目录里面加入我们之前创建的lib文件夹,点击应用,然后开始编译

......
1>正在生成代码...
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppBuild.targets(1523,5): warning MSB8012: TargetPath(D:\下载\build\libtomcrypt-1.18.2\MSVC_Win32_Release\libtomcrypt.lib) 与 Library 的 OutputFile 属性值(D:\下载\build\libtomcrypt-1.18.2\MSVC_Win32_Release\tomcrypt.lib)不匹配。这可能导致项目生成不正确。若要更正此问题,请确保 $(OutDir)$(TargetName)$(TargetExt) 属性值与 %(Lib.OutputFile) 中指定的值匹配。
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppBuild.targets(1525,5): warning MSB8012: TargetName(libtomcrypt) 与 Library 的 OutputFile 属性值(tomcrypt)不匹配。这可能导致项目生成不正确。若要更正此问题,请确保 $(OutDir)$(TargetName)$(TargetExt) 属性值与 %(Lib.OutputFile) 中指定的值匹配。
1>libtomcrypt_VS2008.vcxproj -> D:\下载\build\libtomcrypt-1.18.2\MSVC_Win32_Release\libtomcrypt.lib
1>已完成生成项目“libtomcrypt_VS2008.vcxproj”的操作。
========== 生成: 成功 1 个,失败 0 个,最新 0 个,跳过 0 个 ==========

然后在libtomcrypt-1.18.2文件夹里面同样可以找到MSVC_Win32_Release文件夹,将里面的tomcrypt.lib文件放入我们之前创建的lib文件夹里面,然后在libtomcrypt-1.18.2文件夹里的src文件夹里的headers文件夹里可以取得libtomcrypt库对应的头文件,将这些头文件放入build里的headers文件夹里

至此完成对libtomcrypt库的编译工作

四、使用libtomcrypt库

在自己的工程文件里的需要配置VC++目录,在包含目录里面编辑加入我们之前创建的headers文件夹,在库目录里面加入我们之前创建的lib文件夹

添加第三方库的头文件:

在弹出的工程属性对话框中,左侧选择C/C++下的General,在右侧找到Additional Include Directories,将第三方库头文件即我们之前创建的headers文件夹添加进去

添加库文件所在目录:

工程属性对话框,左侧选择Linker:General,右侧找到Additional Library Directories,将第三方库的lib文件所在目录即我们之前创建的lib文件夹添加进去

将第三方库的lib文件指定为工程依赖库:

工程属性对话框,左侧选择Linker:Input,右侧找到Additional Dependencies,将所用到的第三方库的lib文件名添加进去:

  • tomcrypt.lib
  • tommath.lib

error C2065: “tfm_desc”: 未声明的标识符

  • 引用库的头文件之前先定义宏LTM_DESC
  • main函数中添加一句ltc_mp = ltm_desc;
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
#ifndef TOMCRYPT_H_ #define TOMCRYPT_H_ #include <assert.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <ctype.h> #include <limits.h> /* use configuration data */ #include <tomcrypt_custom.h> #ifdef __cplusplus extern "C" { #endif /* version */ #define CRYPT 0x0116 #define SCRYPT "1.16" /* max size of either a cipher/hash block or symmetric key [largest of the two] */ #define MAXBLOCKSIZE 128 /* descriptor table size */ /* Dropbear change - this should be smaller, saves some size */ #define TAB_SIZE 4 /* error codes [will be expanded in future releases] */ enum { CRYPT_OK=0, /* Result OK */ CRYPT_ERROR, /* Generic Error */ CRYPT_NOP, /* Not a failure but no operation was performed */ CRYPT_INVALID_KEYSIZE, /* Invalid key size given */ CRYPT_INVALID_ROUNDS, /* Invalid number of rounds */ CRYPT_FAIL_TESTVECTOR, /* Algorithm failed test vectors */ CRYPT_BUFFER_OVERFLOW, /* Not enough space for output */ CRYPT_INVALID_PACKET, /* Invalid input packet given */ CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */ CRYPT_ERROR_READPRNG, /* Could not read enough from PRNG */ CRYPT_INVALID_CIPHER, /* Invalid cipher specified */ CRYPT_INVALID_HASH, /* Invalid hash specified */ CRYPT_INVALID_PRNG, /* Invalid PRNG specified */ CRYPT_MEM, /* Out of memory */ CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */ CRYPT_PK_NOT_PRIVATE, /* Requires a private PK key */ CRYPT_INVALID_ARG, /* Generic invalid argument */ CRYPT_FILE_NOTFOUND, /* File Not Found */ CRYPT_PK_INVALID_TYPE, /* Invalid type of PK key */ CRYPT_PK_INVALID_SYSTEM,/* Invalid PK system specified */ CRYPT_PK_DUP, /* Duplicate key already in key ring */ CRYPT_PK_NOT_FOUND, /* Key not found in keyring */ CRYPT_PK_INVALID_SIZE, /* Invalid size input for PK parameters */ CRYPT_INVALID_PRIME_SIZE,/* Invalid size of prime requested */ CRYPT_PK_INVALID_PADDING /* Invalid padding on input */ }; #include <tomcrypt_cfg.h> #include <tomcrypt_macros.h> #include <tomcrypt_cipher.h> #include <tomcrypt_hash.h> #include <tomcrypt_mac.h> #include <tomcrypt_prng.h> #include <tomcrypt_pk.h> #include <tomcrypt_math.h> #include <tomcrypt_misc.h> #include <tomcrypt_argchk.h> #include <tomcrypt_pkcs.h> #ifdef __cplusplus } #endif #endif /* TOMCRYPT_H_ */ /* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt.h,v $ */ /* $Revision: 1.20 $ */ /* $Date: 2006/11/26 01:45:14 $ */
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、付费专栏及课程。

余额充值