Error occurred while decoding OAEP padding

别来无恙 提交于 2019-11-28 10:55:59

A common mistake is to try to decrypt using the public key.

anvilis

I ran into this exact problem. UnicodeEncoding.GetBytes is not always the inverse of UnicodeEncoding.GetString.

byte[] a = new byte[32];

RandomNumberGenerator gen = new RNGCryptoServiceProvider();
gen.GetBytes(a);

UnicodeEncoding byteConverter = new UnicodeEncoding();

byte[] b = byteConverter.GetBytes(byteConverter.GetString(a));

//byte array 'a' and byte array 'b' will not always contain the same elements.

This is why RSACryptoServiceProvider.Decrypt fails. A lot of encrypt/decrypt examples on the web use Unicode encoding. Do not use Unicode encoding. Use Convert.FromBase64String and Convert.ToBase64String instead.

user456732

This error normally indicates you are using a public key to decrypt, while you should be using a private key for decryption. Give it a try.

In my case the error has been caused by wrong padding settings.

Error: RSA decrypt: error:0407A079:rsa routines:RSA_padding_check_PKCS1_OAEP:oaep decoding error

I had openssl_public_encrypt() with OPENSSL_PKCS1_PADDING as a default value in PHP and keypair.decrypt() with the default value RSA_PKCS1_OAEP_PADDING in node-rsa.

So don't forget to check these options too.

RSA encryption may result non readable character, make sure not to cut the string due to special character indicating end of something during write/read the encryption result; e.g you must not use strlen for it will stop when encounter a '\0' in the string.

Another thing to check: it was giving me this error, on the decrypt operation, as a result of forgetting to pass the public key into the RSACryptoServiceProvider for the encrypt operation.

We were getting this issue when we were using the wrong key for decryption.

FYI, you can still be (en/de)crypting in the right key sequence (encr:pub key, decr:priv key), just that you mixed up the keys/decrypting using the private key from another cert/key pair, and not the one paired w/ the pub key with which u encrypted initially. If u turn off OAEP padding and get a "bad data" exception, that's another indication.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!