[英]Unable to do RSA Encryption/Decryption using Crypto++ (isValidCoding is false)
我正在使用Crypto ++使用RSA加密字节数组。 我没有遵循Crypto ++ wiki的示例,也没有运气。 所有示例中的加密和解密都是在一个过程中完成的,但是我正在尝试解密在另一个过程中已经加密的内容。 这是我的代码:
class FixedRNG : public CryptoPP::RandomNumberGenerator
{
public:
FixedRNG(CryptoPP::BufferedTransformation &source) : m_source(source) {}
void GenerateBlock(byte *output, size_t size)
{
m_source.Get(output, size);
}
private:
CryptoPP::BufferedTransformation &m_source;
};
uint16_t Encrypt()
{
byte *oaepSeed = new byte[2048];
for (int i = 0; i < 2048; i++)
{
oaepSeed[i] = (byte)i;
}
CryptoPP::ByteQueue bq;
bq.Put(oaepSeed, 2048);
FixedRNG prng(bq);
Integer n("Value of N"),
e("11H"),
d("Value of D");
RSA::PrivateKey privKey;
privKey.Initialize(n, e, d);
RSA::PublicKey pubKey(privKey);
CryptoPP::RSAES_OAEP_SHA_Encryptor encryptor( pubKey );
assert( 0 != encryptor.FixedMaxPlaintextLength() );
byte blockSize = encryptor.FixedMaxPlaintextLength();
int divisionCount = fileSize / blockSize;
int proccessedBytes = 0;
// Create cipher text space
uint16_t cipherSize = encryptor.CiphertextLength( blockSize );
assert( 0 != cipherSize );
encryptor.Encrypt(prng, (byte*)plaintext, blockSize, (byte*)output);
return cipherSize;
}
void Decrypt(uint16_t cipherSize)
{
byte *oaepSeed = new byte[2048];
for (int i = 0; i < 2048; i++)
{
oaepSeed[i] = (byte)i;
}
CryptoPP::ByteQueue bq;
bq.Put(oaepSeed, 2048);
FixedRNG prng(bq);
Integer n("Value of N"),
e("11H"),
d("Value of D");
RSA::PrivateKey privKey;
privKey.Initialize(n, e, d);
//RSA::PublicKey pubKey(privKey);
CryptoPP::RSAES_OAEP_SHA_Decryptor decryptor( privKey );
byte blockSize = decryptor.FixedMaxPlaintextLength();
assert(blockSize != 0);
size_t maxPlainTextSize = decryptor.MaxPlaintextLength( cipherSize );
assert( 0 != maxPlainTextSize );
void* subBuffer = malloc(maxPlainTextSize);
CryptoPP::DecodingResult result = decryptor.Decrypt(prng, (byte*)cipherText, cipherSize, (byte*)subBuffer);
assert( result.isValidCoding );
assert( result.messageLength <= maxPlainTextSize );
}
不幸的是,isValidCoding的值为false。 我想我对RSA加密/解密有误解!
请注意,已使用KEY.Validate(prng,3)验证了privKey和pubKey。 我也曾尝试使用RAW RSA而不是OAEP和SHA,但是没有运气。 variable. 我试图通过crypto ++代码进行调试,但我怀疑的是变量。 我认为这有问题。 我还使用了AutoSeededRandomPool而不是FixedRNG,但这没有帮助。
值得知道的是,如果我在加密代码之后立即复制解密代码并在Encrypt()方法中执行它,那么一切都很好,并且isValidCoding是真实的!
这可能是不正确的:
byte blockSize = encryptor.FixedMaxPlaintextLength();
...
encryptor.Encrypt(prng, (byte*)plaintext, blockSize, (byte*)output);
return cipherSize;
尝试:
size_t maxLength = encryptor.FixedMaxPlaintextLength();
size_t cipherLength = encryptor.CiphertextLength( blockSize );
...
SecureByteBlock secBlock(cipherLength);
cipherLength = encryptor.Encrypt(prng, (byte*)plaintext, blockSize, secBlock);
secBlock.resize(cipherLength);
FixedMaxPlaintextLength
返回size_t
,而不是byte
。
您可能应该在plaintext
上调用CiphertextLength
。
我不太确定你是怎么从uint_t
encrypt()
返回uint_t
。
从头开始,并以Crypto ++中的示例为起点,可能会做得更好。 我不确定这种设计是否值得追求。
如果您重新开始,那么Shoup的椭圆曲线集成加密方案(ECIES)将是一个不错的选择,因为它结合了带有对称密码和身份验证标签的公钥。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.