VS2008下LibTomCrypt 1.17的编译和使用

本文详细介绍了如何使用tomcrypt库实现AES加密的过程,包括下载、编译、使用tomcrypt库在自己的工程中进行加密解密操作,并通过示例代码演示了加密与解密的具体步骤。

文章出处:http://blog.youkuaiyun.com/delphiwcdj/article/details/6298820

 

具体步骤如下:

(1) 下载tomcrypt 
tomcrypt 1.17 VS2008+intel C++工程
http://d.download.youkuaiyun.com/down/1783339/jackyjkchen 
crypt-1.17.rar (11.03 MB) 源代码
http://www.hackchina.com/cont/118501 
Tomcrypt 1.16 and manual
http://download.youkuaiyun.com/source/981828 

(2) 编译tomcrypt 
用VS2008编译tomcrypt 1.17 (第一个下载),然后生成 crypt_s.dll 和 crypt_s.lib 文件。

(3) 在自己的工程中使用tomcrypt 
在我们自己的工程中,将 crypt_s.dll 和 crypt_s.lib 文件 分别添加到我们的工程中:
1) 将crypt_s.dll文件直接放在debug目录下,即和工程所生成的可执行文件放在一起。
2) 将crypt_s.lib加入到工程中,方法有三种,这里使用通过代码的方式加入。
3) 将所需要的头文件包含到当前工程中,工程上右键->属性->配置属性->C/C++->附加包含目录 
4) 修改编译器设置,工程上右键->属性->配置属性->C/C++->代码生成->运行库,设置为:多线程 DLL (/MD),否则会提示:编译可以通过,但是运行报“找不到MSVCR90.dll”的错误。

(4) 测试:AES加密

 1 /*   
 2     下面的程序实现的功能:  
 3     将字符串c1用AES方法加密放在c2中,再解密放在c3中,密钥是myKey   
 4 */     
 5 #include "tomcrypt.h"  
 6 #pragma comment (lib,"crypt_s.lib")  
 7 int main(int argc, char* argv[])  
 8 {     
 9     unsigned char myKey[32]= "12345";      // 加密密钥    
10     symmetric_key skey;              // 加密状态  
11      
12     aes_setup(myKey, 32, 0, &skey);        
    // 加密前初始化状态(密钥,密钥长度,迭代次数(0: 使用推荐值),加密状态) 13 14 15 unsigned char c1[32]="csdn";         // 需要加密的字符串长度任意 16 unsigned char c2[32];           
    // 输出长度>= 输入长度,分段长度的倍数(这里就是16的倍数) 17 18 // 分段加密,每段长度为16 (一般不可能只有2段,自己写循环) 19 aes_ecb_encrypt(&c1[0], &c2[0],&skey); 20 aes_ecb_encrypt(&c1[16], &c2[16],&skey); 21 22 unsigned char c3[32]; 23 aes_setup(myKey, 32, 0, &skey);      
    // 解密前初始化状态,其实和加密的状态是完全一致的 24 25 // 分段解密 26 aes_ecb_decrypt(&c2[0], &c3[0],&skey); 27 aes_ecb_decrypt(&c2[16], &c3[16],&skey); 28 return 0; 29 }

参考: 
调用DLL有两种方法:静态调用和动态调用
http://www.cnblogs.com/c1230v/articles/1401448.html 
找不到MSVCR90.dll、Debug vs Release及cppLapack
http://hi.baidu.com/wpzhao/blog/item/72dc08f77ce9be2a730eeca7.html

转载于:https://www.cnblogs.com/zhangxiaosong/p/3463901.html

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值