CryptoPP库是一个C++书写的加密算法库,很棒。

在如今的抛却数字证书体系下,只关注公私钥对的情况下,我认为存粹的加解密算法库很有市场,虽然我以前觉得PolarSSL的加解密算法实现不错,但不影响我对CryptoPP的热情

今天,介绍一下,CryptoPP的椭圆曲线加/解密和签名/延签问题,当然是提供源码来说话了

——————————————————————————————————————————

//ecc-enc-dec.h

#ifndef ECC_ENC_DEC_H

#define ECC_ENC_DEC_H

#include <string>

void genEccEncKeys(unsigned int uiKeySize, std::string &priKey, std::string &pubKey);

void genEccSignKeys(unsigned int uiKeySize, std::string &priKey, std::string &pubKey);

std::string ecc_pri_signature(const std::string priKey, const std::string msgToSign);

std::string ecc_pub_encrypt(const std::string pubKey, const std::string msgToEncrypt);

std::string ecc_pri_decrypt(const std::string priKey, const std::string msgToDecrypt);

int ecc_pub_verify(const std::string pubKey, const std::string msgToSign, const std::string msgSigned);

#endif

//ecc-enc-dec.c

#include "ecc-enc-dec.h"

#include "eccrypto.h"

#include "osrng.h"

#include "oids.h"

#include "hex.h"

#include "filters.h"

using namespace CryptoPP;

void genEccEncKeys(unsigned int uiKeySize, std::string &priKey, std::string &pubKey)

{

AutoSeededRandomPool rnd(false, uiKeySize);

ECIES<ECP>::PrivateKey privateKey;

ECIES<ECP>::PublicKey publicKey;

privateKey.Initialize(rnd, ASN1::secp521r1());

privateKey.MakePublicKey(publicKey);

ECIES<ECP>::Encryptor encryptor(publicKey);

HexEncoder pubEncoder(new StringSink(pubKey));

encryptor.DEREncode(pubEncoder);

pubEncoder.MessageEnd();

ECIES<ECP>::Decryptor decryptor(privateKey);

HexEncoder priEncoder(new StringSink(priKey));

decryptor.DEREncode(priEncoder);

priEncoder.MessageEnd();

}

void genEccSignKeys(unsigned int uiKeySize, std::string &priKey, std::string &pubKey)

{

AutoSeededRandomPool rnd(false, uiKeySize);

ECDSA<ECP, SHA256>::PrivateKey privateKey;

ECDSA<ECP, SHA256>::PublicKey publicKey;

privateKey.Initialize(rnd, ASN1::secp521r1());

privateKey.MakePublicKey(publicKey);

StringSink priSs(priKey);

privateKey.Save(priSs);

StringSink pubSs(pubKey);

publicKey.Save(pubSs);

}

std::string ecc_pri_signature(const std::string priKey, const std::string msgToSign)

{

std::string signedText;

ECDSA<ECP, SHA256>::PrivateKey privateKey;

StringSource Ss(priKey, true);

privateKey.Load(Ss);

ECDSA<ECP, SHA256>::Signer signer(privateKey);

size_t siglen = signer.MaxSignatureLength();

signedText.resize(siglen);

RandomPool rnd;

siglen = signer.SignMessage(rnd, (byte*)msgToSign.data(), msgToSign.size(), (byte*)signedText.data());

signedText.resize(siglen);

return signedText;

}

std::string ecc_pub_encrypt(const std::string pubKey, const std::string msgToEncrypt)

{

std::string cipherText;

// if to save the key into a file, FileSource should be replace StringSource

StringSource pubString(pubKey, true, new HexDecoder);

ECIES<ECP>::Encryptor encryptor(pubString);

size_t uiCipherTextSize = encryptor.CiphertextLength(msgToEncrypt.size());

cipherText.resize(uiCipherTextSize);

RandomPool rnd;

encryptor.Encrypt(rnd, (byte*)msgToEncrypt.c_str(), msgToEncrypt.size(), (byte*)cipherText.data());

return cipherText;

}

std::string ecc_pri_decrypt(const std::string priKey, const std::string msgToDecrypt)

{

std::string decryptText;

StringSource priString(priKey, true, new HexDecoder);

ECIES<ECP>::Decryptor decryptor(priString);

size_t uiDecryptTextSize = decryptor.MaxPlaintextLength(msgToDecrypt.size());

decryptText.resize(uiDecryptTextSize);

RandomPool rnd;

decryptor.Decrypt(rnd, (byte*)msgToDecrypt.c_str(), msgToDecrypt.size(), (byte*)decryptText.data());

return decryptText;

}

int ecc_pub_verify(const std::string pubKey, const std::string msgToSign, const std::string msgSigned)

{

ECDSA<ECP, SHA256>::PublicKey publicKey;

StringSource Ss(pubKey, true);

publicKey.Load(Ss);

RandomPool rnd;

publicKey.Validate(rnd, 3);

ECDSA<ECP, SHA256>::Verifier verifier(publicKey);

bool result = verifier.VerifyMessage((byte*)msgToSign.data(), msgToSign.size(), (byte*)msgSigned.data(), msgSigned.size());

if (result)

{

return 1;

}

else

{

return 0;

}

}

//ecc-test.c

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include "ecc-enc-dec.h"

int main()

{

std::string srcText = std::string("Hello world.");

std::string enc_priKey, enc_pubKey;

genEccEncKeys(1024, enc_priKey, enc_pubKey);

std::string encryptResult = ecc_pub_encrypt(enc_pubKey, srcText);

printf("public key enc result: %s--\nlength: %d\n", encryptResult.c_str(), (int)encryptResult.size());

std::string decryptResult = ecc_pri_decrypt(enc_priKey, encryptResult);

printf("private key dec result: %s--\nlength: %d\n", decryptResult.c_str(), (int)decryptResult.size());

std::string sign_priKey, sign_pubKey;

genEccSignKeys(1024, sign_priKey, sign_pubKey);

std::string signedResult = ecc_pri_signature(sign_priKey, srcText);

printf("private key sign result: %s--\nlength: %d\n", signedResult.c_str(), (int)signedResult.size());

int bVerify = ecc_pub_verify(sign_pubKey, srcText, signedResult);

printf("public key verify result: %d\n", 0==bVerify?false:true);

return 0;

}

//result

# ./ecc-test
public key enc result:

crypto++的更多相关文章

  1. javax.crypto.BadPaddingException: Given final block not properly padded 解决方法

    下面的 Des 加密解密代码,在加密时正常,但是在解密是抛出错误: javax.crypto.BadPaddingException: Given final block not properly p ...

  2. 使用crypto模块实现md5加密功能(解决中文加密前后端不一致的问题)

    正常情况下使用md5加密 var crypto = require('crypto'); var md5Sign = function (data) { var md5 = crypto.create ...

  3. javax.crypto.BadPaddingException: Given final block not properly padded

    一.报错 写了一个加密方法,在Windows上运行没有问题,在Linux上运行时提示如下错误: javax.crypto.BadPaddingException: Given final block ...

  4. Liunx-https-java.lang.NoClassDefFoundError: javax/crypto/SunJCE_b

    错误信息: java.lang.NoClassDefFoundError: javax/crypto/SunJCE_b at javax.crypto.KeyGenerator.a(DashoA13* ...

  5. node crypto md5加密,并解决中文不相同的问题

    在用crypto模块时碰到了加密中文不相同的问题,多谢群里面@蚂蚁指定 1:解决中文不同的问题 function md5Pay(str) { str = (new Buffer(str)).toStr ...

  6. Crypto++ 动态链接编译与实例测试

    测试用例的来源<Crypto++入门学习笔记(DES.AES.RSA.SHA-256)> 解决在初始化加密器对象时触发异常的问题: CryptoPP::AESEncryption aesE ...

  7. python3 crypto winrandom import error

    早就听说3的包很成熟了,自从从2.7过渡上来后还是碰到各种不适应,可以想象更早的时候问题该要多么多,特别一些必备库经典库如果没有跟进得多痛苦. [code lang="python" ...

  8. Crypto++入门学习笔记(DES、AES、RSA、SHA-256)(加解密)

    转自http://www.cppblog.com/ArthasLee/archive/2010/12/01/135186.html 最近,基于某些原因和需要,笔者需要去了解一下Crypto++库,然后 ...

  9. nodejs 核心模块crypto

    crypto用于加密解密 'use strict' var crypto=require('crypto'); var data={age:18} var key='dt';//定义一个钥匙 var ...

  10. maven install 时提示“程序包 javax.crypto不存在”

    但是javax.crypto是在jdk的jre\lib目录下的 解决方案: <compilerArguments> <bootclasspath>${java.home}/li ...

随机推荐

  1. python实现简单的购物车

    import json,timeuserinfo={"lanfei": { "passwd":"lanfei", "salary& ...

  2. process.argv

    返回进程启动时的命令行参数. 第一个元素是 process.execPath. 使用 process.argv0 可以获取 argv[0] 原始的值. 第二个元素是当前执行的 JavaScript 文 ...

  3. 使用Type.MakeGenericType,反射构造泛型类型

    有时我们会有通过反射来动态构造泛型类型的需求,该如何实现呢?举个栗子,比如我们常常定义的泛型委托Func<in T, out TResult>,当T或TResult的类型需要根据程序上下文 ...

  4. 读文件/写文件。http请求。读取文件列表。

    package transfor; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import j ...

  5. 【原创】MySQL(Innodb)索引的原理

    引言 回想四年前,我在学习mysql的索引这块的时候,老师在讲索引的时候,是像下面这么说的 索引就像一本书的目录.而当用户通过索引查找数据时,就好比用户通过目录查询某章节的某个知识点.这样就帮助用户有 ...

  6. 解决Mysql命令行输入密码闪退问题

    输入密码闪退是因为后台Mysql服务没有启动. 解决办法:我的电脑,右键管理,服务,查看服务里面Mysql是否在运行.如果没有在运行那么可以右键启动,最好属性中设置为自动启动.

  7. 2018-2019-1 20189201 《LInux内核原理与分析》第五周作业

    甜死人的图片 一.书本第四章知识总结[系统调用的三层机制(上)] 无参数系统调用 依次通过c语言和内嵌汇编的c语言实现time()函数中封装的系统调用. 用户态.内核态和中断 用户态:在低的执行级别下 ...

  8. JS浅谈原始值与引用值操作

    值的操作分为三大类:复制,传递,比较 一:复制 原始值 let a = 10; let b = a; 注释:2018-7-30 17:33:49 1 原始类型的值都是存放在栈内存当中,所以他们的赋值操 ...

  9. GUI Design Studio的使用方法

    一.GUI Design Studio的介绍 GUI DesignStudio 是一个给应用软件设计图形用户界面的专业工具,它可在画基于web形态的原型时,可以用 Axure RP. Balsamiq ...

  10. PHP服务器时差8小时的解决办法

    PHP服务器时差8小时的解决办法 <?php date_default_timezone_set('Asia/Shanghai');  echo date("Y-m-d")? ...