rsa 加密 密文压缩,加密++ RSA和“无效密文”

Well, I've been going through my personal hell these days

I am having some trouble decrypting a message that was encrypted using

RSA and I'm always failing with a "RSA/OAEP-MGF1(SHA-1): invalid

ciphertext"

I have a private key encoded in base64 and I load it:

RSA::PrivateKey private_key;

StringSource file_pk(PK,true,new Base64Decoder);

private_key.Load( file_pk );

I then proceed to decode the message by doing:

RSAES_OAEP_SHA_Decryptor decryptor(private_key);

AutoSeededRandomPool rng;

string result;

StringSource(ciphertext, true,

new PK_DecryptorFilter(rng, decryptor,

new StringSink(result)

)

);

As far as I can tell, the message should be being parsed without any

problems. ciphertext is an std::string, so no \0 at the end that could

do something unexpected.

I just though of something, and what if the private key is incorrect

but can be loaded anyway without throwing a BER decode error. What

would that throw when decrypting?

Hope that anyone can shed some light on this.

Cheers

解决方案

If the key was actually corrupted, the Load function should have failed. However you can ask the key to self-test itself, which should detect any corruption, by calling Validate, like:

bool key_ok = private_key.Validate(rng, 3);

The second parameter (here, 3) specifies how much checking to be done. For RSA, this will cause it to run all available tests, even the slow/expensive ones.

Another reason the decoding might fail is if the key simply is not the one that was used to encrypt the original message.

Obviously the ciphertext input must be completely identical to what was originally produced on the encrypting side. For debugging, one good way to check this would be to feed the ciphertext at both sides into a hash function (conveniently already available to you, of course) and comparing the outputs. If you hex or base64 encoded the ciphertext for transmission you must undo that before you give it to the RSA decryptor.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值