Crypto++使用方法

0 引言

为阅读本文,读者需要具备密码学最基本的知识,如:对称加密和非对称 加密、数字签名等。还好,准备这些知识,一个下午的时间就足够了。

许多朋友问我 如何使用CryptoPP(目前最新版本为5.4),我以前也没用过,但一直觉得是个好东西,属于 经典的C++库之一。因此,有必要把它作为我的软件基石之一。我以前是用Windows的Crypt API的,ATL有对应的封装类。但是,我遇到了一个问题之后,决定放弃Crypt API。原因是, 我使用Win2003加密的东西,在Win2000上解密失败。很火大。水平有限,时间有限,就不深 入寻找原因了。

改投开源的CryptoPP C++库门下,发现CryptoPP的用法诡异,到处是 陷阱一样的模板,对于我这个对密码学本身略知皮毛的人来说,无疑是雪上加霜。头一次使 用,以失败而铩羽。但是,CryptoPP在业界的好名声,使我不忍放弃,时隔半年之后,我终 于重读CryptoPP的例子,摸索出使用方法。但是,直接使用CryptoPP真的很烦琐,索性包装 为DLL,我把它取名字叫:CryptoPP32.dll。直接使用CryptoPP32.dll,不需要任何 cryptopp5.4的库,因为我已经把cryptopp5.4静态编译进去了。

我在下面的地址:

http://www.3snews.net/index.php/5890/action_viewspace_itemid_9580.html

提供了整个工程的文件下载,包括cryptopp5.4、CryptoPP32和测试项目。用户必须使用 VC7.1打开CryptoPP32_DLL目录下的sln文件。第一次编译,必须在生成管理器中选中 cryptlib。

(我不知道CSDN如何上传文件。本人提供的下载文件,可以任意散发,复 制,但不得改变作者声明。CSDN有很多地方做的不够友好,希望改进!希望CSDN的BLOG编写 组学学http://www.3snews.net的BLOG,比你们的强很多啊!)

1 CryptoPP54与 CryptoPP32

下面是CryptoPP32.h接口文件的方法:

// CryptoPP32.DLL接口方法
...
namespace CryptoPP32
{
bool CRYPTOPP32_DLL RSAES_OAEP_GenerateKeys(const char *privFilename, const char *pubFilename, unsigned int keyLength=512, const char *seed=0);
bool CRYPTOPP32_DLL RSAES_OAEP_GenerateKeys(string& strPrivKey, string& strPubKey, unsigned int keyLength=512, const char *seed=0);

bool CRYPTOPP32_DLL RSAES_OAEP_EncryptString(const char *pubFilename, const char *message, string& cipher, const char *seed=0);
bool CRYPTOPP32_DLL RSAES_OAEP_DecryptString(const char *privFilename, const char *cipher, string& message);

bool CRYPTOPP32_DLL RSAES_OAEP_EncryptString(const string& strPubKey, const char *message, string& cipher, const char *seed=0);
bool CRYPTOPP32_DLL RSAES_OAEP_DecryptString(const string& strPrivKey, const char *cipher, string& plain);

bool CRYPTOPP32_DLL RSAES_OAEP_EncryptString(const char* N, const char* E, const char* message, string& cipher, const char* seed=0);
bool CRYPTOPP32_DLL RSAES_OAEP_DecryptString(const char* N, const char* E, const char* D, const char* P, const char* Q, const char* dP, const char* dQ, const char* U, const char* cipher, string& plain);

bool CRYPTOPP32_DLL RSAES_PKCS_GenerateKeys(const char *privFilename, const char *pubFilename, unsigned int keyLength=512, const char *seed=0);
bool CRYPTOPP32_DLL RSAES_PKCS_GenerateKeys(string& strPrivKey, string& strPubKey, unsigned int keyLength=512, const char *seed=0);

bool CRYPTOPP32_DLL RSAES_PKCS_EncryptString(const char *pubFilename, const char *message, string& cipher, const char *seed=0);
bool CRYPTOPP32_DLL RSAES_PKCS_EncryptString(const string& strPubKey, const char *message, string& cipher, const char *seed=0);

bool CRYPTOPP32_DLL RSAES_PKCS_DecryptString(const char *privFilename, const char *cipher, string& message);
bool CRYPTOPP32_DLL RSAES_PKCS_DecryptString(const string& strPrivKey, const char *cipher, string& plain);

bool CRYPTOPP32_DLL RSAES_PKCS_EncryptString(const char* N, const char* E, const char* message, string& cipher, const char* seed=0);
bool CRYPTOPP32_DLL RSAES_PKCS_DecryptString(const char* N, const char* E, const char* D, const char* P, const char* Q, const char* dP, const char* dQ, const char* U, const char* cipher, string& plain);

bool CRYPTOPP32_DLL RSASS_PKCS_Sign(const char *privFilename, const char *msgFilename, const char *signFilename, const char *hashFunc="SHA");
bool CRYPTOPP32_DLL RSASS_PKCS_Verify(const char *pubFilename, const char *msgFilename, const char *signFilename, const char *hashFunc="SHA");

bool CRYPTOPP32_DLL RSASS_PKCS_Sign(const string& strPrivKey, const char *message, string& signature, const char *hashFunc="SHA");
bool CRYPTOPP32_DLL RSASS_PKCS_Verify(const string& strPubKey, const char *message, const string& signature, const char *hashFunc="SHA");

bool CRYPTOPP32_DLL HMAC_SHA1_EncryptString(const char *inString, const char *passPhrase, string& outString);
bool CRYPTOPP32_DLL HMAC_SHA1_DecryptString(const char *inString, const char *passPhrase, string& outString);

bool CRYPTOPP32_DLL HMAC_SHA1_EncryptFile(const char *inFilename, const char *outFilename, const char *passPhrase);
bool CRYPTOPP32_DLL HMAC_SHA1_DecryptFile(const char *inFilename, const char *outFilename, const char *passPhrase);

bool CRYPTOPP32_DLL GzipFile(const char *inFilename, const char *outFilename, int deflateLevel);
bool CRYPTOPP32_DLL GunzipFile(const char *inFilename, const char *outFilename);

bool CRYPTOPP32_DLL Base64Encode(const char *inFilename, const char *outFilename);
bool CRYPTOPP32_DLL Base64Decode(const char *inFilename, const char *outFilename);

bool CRYPTOPP32_DLL Base64Encode(const char *plain, string& encoded);
bool CRYPTOPP32_DLL Base64Decode(const char *encoded, string& plain);

bool CRYPTOPP32_DLL HexEncode(const char *inFilename, const char *outFilename);
bool CRYPTOPP32_DLL HexDecode(const char *inFilename, const char *outFilename);

bool CRYPTOPP32_DLL HexEncode(const char *plain, string& encoded);
bool CRYPTOPP32_DLL HexDecode(const char *encoded, string& plain);
};

时间: 2024-08-04 09:23:30

Crypto++使用方法的相关文章

jdk-求教-JDK6+TLSv1.2+Bouncy Castle Crypto API的具体实现方法

问题描述 求教-JDK6+TLSv1.2+Bouncy Castle Crypto API的具体实现方法 系统是JDK6,目前不太愿意升级JDK7,同时想支持tlsv1.2, 目前知道BC能支持,但是文档太少,求有类似经历的大神指点,需要 bc client连接 指定tlsv1.2的服务端实现案例 解决方案 https://www.bouncycastle.org/

ASP.NET中几种加密方法

MD5的全称是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由Mit Laboratory for Computer Science和Rsa data security inc的Ronald l. rivest开发出来,经md2.md3和md4发展而来.它的作用是让大容量信息在用数字签名软件签署私人密匙前被"压缩"成一种保密的格式(就是把一个任意长度的字节串变换成一定长的大整数).不管是md2.md4还是md5,它们都需要获得一个随机长度的信息并产

ASP.NET中MD5和SHA1加密的几种方法

MD5的全称是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由Mit Laboratory for Computer Science和Rsa data security inc的Ronald l. rivest开发出来,经md2.md3和md4发展而来.它的作用是让大容量信息在用数字签名软件签署私人密匙前被"压缩"成一种保密的格式(就是把一个任意长度的字节串变换成一定长的大整数).不管是md2.md4还是md5,它们都需要获得一个随机长度的信息并产

ASP.NET中MD5与SHA1加密的几种方法

MD5的全称是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由Mit Laboratory for Computer Science和Rsa data security inc的Ronald l. rivest开发出来,经md2.md3和md4发展而来.它的作用是让大容量信息在用数字签名软件签署私人密匙前被"压缩"成一种保密的格式(就是把一个任意长度的字节串变换成一定长的大整数).不管是md2.md4还是md5,它们都需要获得一个随机长度的信息并产

在Node.js中使用HTTP上传文件的方法

  这篇文章主要介绍了在Node.js中使用HTTP上传文件的方法,作者以windows下的visual studio作为操作node的环境,推荐阅读!需要的朋友可以参考下 开发环境 我们将使用 Visual Studio Express 2013 for Web 作为开发环境, 不过它还不能被用来做 Node.js 开发.为此我们需要安装 Node.js Tools for Visual Studio. 装好后 Visual Studio Express 2013 for Web 就会转变成一

从Java的jar文件中读取数据的方法

  这篇文章主要介绍了从Java的jar文件中读取数据的方法,实例分析了java档案文件的相关操作技巧,需要的朋友可以参考下 本文实例讲述了从Java的jar文件中读取数据的方法.分享给大家供大家参考.具体如下: Java 档案 (Java Archive, JAR) 文件是基于 Java 技术的打包方案.它们允许开发人员把所有相关的内容 (.class.图片.声音和支持文件等) 打包到一个单一的文件中.JAR 文件格式支持压缩.身份验证和版本,以及许多其它特性. 从 JAR 文件中得到它所包含

密码类库Crypto++™ Library 5.1的研究与应用

在计算机被广泛应用的信息时代,信息本身就是时间,就是财富.大量信息用数据形式存放在计算机系统里.信息的传输则通过公共信道.这些计算机系统和公共信道是不设防的,是很脆弱的,容易受到攻击和破坏,信息的丢失不容易被发现,而且后果是极其严重.如何保护信息的安全已不仅仅是军事和政府部门感兴趣的问题,其他企事业单位也愈感迫切.因为在网络化的今天,计算机犯罪每年使他们遭受的损失极其巨大,而且还在发展中.密码是有效而且可行的保护信息安全的办法.随着计算机网络不断渗透到各个领域,密码学的应用也随着扩大.数字签名.

Java中常用的加密方法(JDK)

加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容.大体上分为双向加密和单向加密,而双向加密又分为对称加密和非对称加密(有些资料将加密直接分为对称加密和非对称加密). 双向加密大体意思就是明文加密后形成密文,可以通过算法还原成明文.而单向加密只是对信息进行了摘要计算,不能通过算法生成明文,单向加密从严格意思上说不能算是加密的一种,应该算是摘要算法吧.具体区分可以参考: (本人解释不清呢 -- ) http://secur

加密相关的一些方法

(1)求两个字节数组的异或 Java代码   /***       * 求异或.       *        * @param strOldHex  : hex string       * @param strKeyHex  : hex string       * @return       */       public static byte[] xOR(String strOldHex, String strKeyHex) {           byte[] oldBytes =