Crypto++ 加/解密算法库

编译 Crypto++ cryptlib 适合VC6 VC7 VC8 VC9 VC10

Crypto++ Library is a free C++ class library of cryptographic schemes.
可以到下面的网址下载最新源代码:

http://www.cryptopp.com/

 

Crypto++ Library 是开源的、跨平台的C++, 提供丰富的加密解密算法,包括:MD5,IDEA, Triple-DES,AES (Rijndael), RC6, MARS, Twofish, Serpent, RSA, DSA, SHA-1, SHA-2 等等。

 

支持的编译器如下:

  * MSVC 6.0 - 2010
  * GCC 3.3 - 4.5
  * C++Builder 2010
  * Intel C++ Compiler 9 - 11.1
  * Sun Studio 12u1, Express 11/08, Express 06/10

 

==============================

 

这里简单说明一下使用 MSVC2008 对Crypto++进行编译使用的方法和注意事项, 希望对大家开始学习有帮助!

下面一段的英文比较简单, 说明了Crypto++包含的工程情况, 以及使用注意事项, 一看就清楚:

 

On Windows, Crypto++ can be compiled into 3 forms:a static library
including all algorithms, a DLL with only FIPS Approved algorithms, and
a static library with only algorithms not in the DLL.

 

 

To compile Crypto++ with MSVC, open the "cryptest.dsw" (for MSVC 6 and MSVC .NET
2003) or "cryptest.sln" (for MSVC 2005 - 2010) workspace file and build one or
more of the following projects:

 

//用于生成包含所有算法的静态库

cryptlib - a static libraryincluding all algorithms

 

//用于生成仅包含FIPS作为标准的算法的dll和导入库

cryptopp - This builds the DLL. Please note that if you wish to use Crypto++
  as a FIPS validated module, you must use a pre-built DLL that has undergone
  the FIPS validation process instead of building your own.
 

//基于dll开发的例子程序
dlltest - This builds a sample application that only uses the DLL.

 

//基于cryptlib静态库开发的测试驱动程序

cryptest Non-DLL-Import Configuration - This builds the full static library
  along with a full test driver.
 

//基于dll 和不包含FIPS法静态库开发的测试驱动程序

 

 

cryptest DLL-Import Configuration - This builds a static library containing
  only algorithms not in the DLL, along with a full test driver that uses
  both the DLL and the static library.

 

To use the Crypto++ DLL in your application, #include "dll.h" before including
any other Crypto++ header files, and place the DLL in the same directory as
your .exe file. dll.h includes the line #pragma comment(lib, "cryptopp")
so you don't have to explicitly list the import library in your project
settings.

 

To use a static library form of Crypto++, make the "cryptlib"
project a dependency of your application project, or specify it as
an additional library to link with in your project settings.
In either case you should check the compiler options to
make sure that the library and your application are using the same C++
run-time libraries and calling conventions.

 

名词解释:

FIPS : Federal Information Processing Standards   (美国)联邦信息处理标准

 

 

================================================================

crypto++ Crypto++是一个C++编写的密码学类库,md5 AES DES 所有密码加密的算法都可找到,跨平台

Crypto++是一个C++编写的密码学类库。读过《过河卒》的朋友还记得作者的那个不愿意去微软工作的儿子吗,就是Crypto++的作者Wei Dai。
Crypto++是一个非常强大的密码学库,在密码学界很受欢迎,最初还是Rivest(RSA的R)门下的一个博士姐姐把这个库介绍给我的。虽然网络上可以找到很多密码学相关的代码和库,但是Crypto++有其明显的优点。主要是功能全,统一性好。例如椭圆曲线加密算法和AES在OpenSSL的crypto库中就还没最终完成,而在Crypto++中就支持的比较好,基本上密码学中需要的主要功能都可以在里面找得到。Crypto++是由标准的C++写成的,学习C++、密码学、网络安全都可以通过阅读Crypto++的源代码得到启发和提高。

Crypto++的安装
首先到http://www.cryptopp.com/
上下载最新版本的源代码,如果是windows版的,会得到一个VC的项目,直接用VC打开就可以编译了。这里建议大家使用最新版的C++编译器,因为诸如VC6的编译器是不支持C++的标准的,很多符合C++标准的代码不能编译通过。编译的时间比较长,完成后会生成cryptlib.lib这个库文件。可以将Crypto++源文件的目录命名为cryptopp,拷贝到编译器的include目录(例如:C:/VS.NET/VC7/include),将cryptlib.lib文件拷贝到编译器的lib目录。这样我们只需要说明链接cryptlib.lib即可。例如在VC7中在项目->属性->链接器->命令行->附加选项中添加“cryptlib.lib”。

Hello World
现在写一个hello world程序看看能不能编译通过。

#include <iostream>
using namespace std;

#include <cryptopp/aes.h>
using namespace CryptoPP;

int main()
{
       cout << "hello crypto++" << endl;
       cout << "Aes block size is " << AES::BLOCKSIZE << endl;

 return 0;
}

编译运行,一切OK,哈哈:D,可以用了。

 

 

Crypto++首页上提供了Crypto++ User Guide这个入门指南的链接,这本指南属于一定要看的资料,不过可惜的是这也是找到的唯一一本指南了:(。User Guide上面的有一些例子,其中对AES用法的说明不太直接,而AES之类对称加密算法又比较常用,所以我这里写了一个AES的例子。

例子是直接用AES加密一个块,AES的数据块大小为128位,密钥长度可选择128位、192位或256位。直接用AES加密一个块很少用,因为我们平常都是加密任意长度的数据,需要选择CFB等加密模式。但是直接的块加密是对称加密的基础。

#include <iostream>
using namespace std;

#include <cryptopp/aes.h>
using namespace CryptoPP;

int main()
{

        //AES中使用的固定参数是以类AES中定义的enum数据类型出现的,而不是成员函数或变量
        //因此需要用::符号来索引

        cout << "AES Parameters: " << endl;
        cout << "Algorithm name : " << AES::StaticAlgorithmName() << endl;     

        //Crypto++库中一般用字节数来表示长度,而不是常用的字节数
        cout << "Block size     : " << AES::BLOCKSIZE * 8 << endl;
        cout << "Min key length : " << AES::MIN_KEYLENGTH * 8 << endl;
        cout << "Max key length : " << AES::MAX_KEYLENGTH * 8 << endl;

        //AES中只包含一些固定的数据,而加密解密的功能由AESEncryption和AESDecryption来完成
        //加密过程
        AESEncryption aesEncryptor; //加密器

 

        unsigned char aesKey[AES::DEFAULT_KEYLENGTH];                   //密钥

        unsigned char inBlock[AES::BLOCKSIZE] = "123456789";    //要加密的数据块

        unsigned char outBlock[AES::BLOCKSIZE];                                 //加密后的密文块

        unsigned char xorBlock[AES::BLOCKSIZE];                                 //必须设定为全零

        memset( xorBlock, 0, AES::BLOCKSIZE );                                 //置零

 

        aesEncryptor.SetKey( aesKey, AES::DEFAULT_KEYLENGTH );  //设定加密密钥

        aesEncryptor.ProcessAndXorBlock( inBlock, xorBlock, outBlock ); //加密

 

        //以16进制显示加密后的数据

        for( int i=0; i<16; i++ ) {

                cout << hex << (int)outBlock[i] << " ";

        }

        cout << endl;

 

        //解密

        AESDecryption aesDecryptor;

        unsigned char plainText[AES::BLOCKSIZE];

        aesDecryptor.SetKey( aesKey, AES::DEFAULT_KEYLENGTH );

        aesDecryptor.ProcessAndXorBlock( outBlock, xorBlock, plainText );

 

        for( int i=0; i<16; i++ ) {      cout << plainText[i];    }
        cout << endl;

        return 0;
}


这里面有几个地方要注意一下:
AES并不是一个类,而是类Rijndael的一个typedef。
Rijndael虽然是一个类,但是其用法和namespace很像,本身没有什么成员函数和成员变量,只是在类体里面定义了一系列的类和数据类型(enum),真正能够进行加密解密的AESEncryption和AESDecryption都是定义在这个类内部的类。
AESEncryption和AESDecryption除了可以用SetKey()这个函数设置密钥,在构造函数中也能设置密钥,参数和SetKey()是一样的。
ProcessAndXorBlock()可能会让人比较疑惑,函数名的意思是ProcessBlock and XorBlock,ProcessBlock就是对块进行加密或解密,XorBlock在各种加密模式中有用,这里我们不需要应用模式,因此把用来Xor操作的xorBlock置为0,那么Xor操作就不起作用了。

转载:点击打开链接

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JS Crypto SM4密是指使用JavaScript编写的SM4算法来对数据进行密。SM4是一种对称算法,也是中国国家密码局推荐的国密算法之一。 在JavaScript中,可以使用第三方来实现SM4算法,比如引用中提到的"sm-crypto-js"。这个可以通过npm安装并引入到你的项目中。 具体的使用步骤如下: 1. 首先,你需要安装"sm-crypto-js",可以通过命令`npm install --save git https://github.com/bianjieai/sm-crypto-js.git sm2`来进行安装。 2. 然后,在你的代码中引入SM4算法模块,可以使用类似`import { SM2, SM3, SM4 } from './index.js'`的语句来引入。 3. 接下来,你可以使用SM4模块提供的方法来进行密操作。具体的密函数和参数可以根据具体的需求而定,例如可以使用`SM4.encrypt(key, data)`来进行密,其中`key`是密的密钥,`data`是待密的数据。 需要注意的是,SM4算法需要提供一个密钥来进行密和解密操作。你可以使用随机生成的密钥或者自定义的密钥,具体取决于你的需求。 总结起来,JS Crypto SM4密需要使用第三方来实现,具体的操作包括安装、引入模块和调用密函数。希望以上信息对你有帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [js 实现国密sm2、sm3、sm4 解密demo](https://download.csdn.net/download/qq_36654629/86747065)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [sm-crypto-js:用于 SM 算法的 JavaScript ](https://download.csdn.net/download/weixin_42131367/19252108)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [crypto-sm:国密密,sm2 sm3 sm4](https://download.csdn.net/download/weixin_42097508/19139893)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值