我想使用RSA加密许可我的软件。我的软件有几个可执行文件,并且我打算在每个执行文件执行之前检查一个通用许可文件的签名。我的目标不是要规避许可保护,而是要让它变得非常困难。我知道没有人能让它变得不可能。crypto ++将密钥保存到字节队列并返回到密钥
可执行文件当前在Windows环境中运行,但它们只能在Linux环境中发布。
我现在的计划是把公钥放在每个可执行文件中进行签名验证。这些程序已经有一个“安全”加密区域,用于放置密钥。
我对这篇文章的问题是,我的实现方法是否有意义?有另外一种选择吗?将公钥存放在单独文件中的替代方法将允许黑客替换该文件并使用其自己的许可证文件和签名。这似乎并不安全。
此外,我一直在阅读crypto ++文档并运行示例代码来尝试完成此任务。我无法获得任何将密钥放入非文件接收器并再次返回的代码。所有示例都会写入文件并读取。我需要能够保存和加载字符串或字节队列。下面是一个简单的尝试,但是当它运行时,当r2.BERDecodePrivateKey()执行时出现此错误:
MyRSA.exe中的0x7630c41f未处理的异常:Microsoft C++异常:内存位置的CryptoPP :: BERDecodeErr 0x002ef32c ..
#include <osrng.h>
#include "rsa.h"
using CryptoPP::RSA;
#include <queue.h>
using CryptoPP::ByteQueue;
int myCode_key2ByteQueueToKey(void)
{
////////////////////////////////////////////////////////////////////////
// Generate the keys
AutoSeededRandomPool rnd;
CryptoPP::RSA::PrivateKey r1;
r1.GenerateRandomWithKeySize(rnd, 2048);
CryptoPP::RSA::PublicKey rsaPublic(r1);
////////////////////////////////////////////////////////////////////////
// Put the 'inner' part of the key into a ByteQueue - whatever that is.
ByteQueue queue;
r1.DEREncodePublicKey(queue);
////////////////////////////////////////////////////////////////////////
// Copy the byte queued inner key into another key
CryptoPP::RSA::PrivateKey r2;
r2.BERDecodePrivateKey(queue, false /*optParams*/, queue.MaxRetrievable());
////////////////////////////////////////////////////////////////////////
// Validate the key made the trip in and out of a byte queue ok.
if(!r1.Validate(rnd, 3)) printf("Validation of oringal key failed.\n");
if(!r2.Validate(rnd, 3)) printf("Validation of reloaded key failed.\n");
if(r1.GetModulus() != r2.GetModulus() ||
r1.GetPublicExponent() != r2.GetPublicExponent() ||
r1.GetPrivateExponent() != r2.GetPrivateExponent())
{
printf("Key didn't survive round trip in and out of a byte queue.");
}
return 0;
}
我没有上面的代码修复。有些东西我对图书馆不了解,结果有些东西不见了,但我得走一步。
我以为我会发布我找到的替代方案。它是Crypto ++ wiki上的一个例子,它将密钥放入字符串(而不是文件)并重新放回。往返旅程显示工作。
http://www.cryptopp.com/wiki/BERDecode
而不是
CryptoPP::RSA::PrivateKey
它采用
CryptoPP::RSAES_OAEP_SHA_Decryptor
,同样的公共密钥。这允许使用不适用于PrivateKey
类的成员函数AccessKey()
。
如果任何人有原始代码的修复,我敦促您请张贴它,因为它会帮助我更好地了解这个库。
嗯,哪里了'r2'私钥从何而来?稀薄的空气? –
由行声明:CryptoPP :: RSA :: PrivateKey r2;在下一行中,我试图将它的值赋给字节队列的值。 – user3341576
把它扔到try catch中,看看例外情况如何。 –