21

を使用してテキストを復号化してRSACryptoServiceProvider.Decryptいるときに、次のエラーが発生します。

OAEP パディングのデコード中にエラーが発生しました。

これが私のコードです:

CspParameters cspParam = new CspParameters();

cspParam = new CspParameters();

cspParam.Flags = CspProviderFlags.UseMachineKeyStore;

clsCertificates cc = new clsCertificates();

string a = "";

cc.OpenStoreIE(ref a);

cc.SetProperties();

X509Certificate2 cert = new X509Certificate2();

cert = cc.x509_2Cert;

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspParam);

//to gentrate private and public keys from the certificate

rsa.FromXmlString(cert.PublicKey.Key.ToXmlString(false));


String publicKey = rsa.ToXmlString(false); // gets the public key 
String privateKey = rsa.ToXmlString(true); // gets the private key working if paramter is false if true give error key is not valid for use in specified state

Response.Write("<Textarea rows=10 cols=100>PUBLIC: " + publicKey + "</TextArea>");

Response.Write("<Textarea rows=10 cols=100>PRIVATE: " + privateKey + "</Textarea>");

Response.Write("<BR>Encrypting the string \"HelloThere\" with the public Key:<BR>");

String str = "HelloThere";

RSACryptoServiceProvider RSA2 = new RSACryptoServiceProvider(cspParam);



//---Load the Public key---

RSA2.FromXmlString(publicKey);

//working with the folowing line instead of above but i need the keys of he certificte

//RSA2.ToXmlString(true);

Byte[] EncryptedStrAsByt = RSA2.Encrypt(System.Text.Encoding.Unicode.GetBytes(str), true);

String EncryptedStr = System.Text.Encoding.Unicode.GetString(EncryptedStrAsByt);

Response.Write("<Textarea rows=10 cols=100>Encrypted String: " + EncryptedStr + "</Textarea>");

Response.Write("<BR>Decrypting the Encrypted String with the Private key:<BR>");



RSACryptoServiceProvider RSA3 = new RSACryptoServiceProvider(cspParam);



//---Load the Private key---

RSA3.FromXmlString(privateKey);

//working with the folowing line instead of above but i need the keys of he certificte

//RSA3.ToXmlString(true);

Byte[] DecryptedStrAsByt = RSA3.Decrypt(EncryptedStrAsByt, true );//Error if true then error is error occured while decoding the OAE$P padding and if false then error is bad key i am using windows xp so it should be true.

String DecryptedStr = System.Text.Encoding.Unicode.GetString(DecryptedStrAsByt);

Response.Write("<Textarea rows=10 cols=100>Decrypted String: " + DecryptedStr + "</Textarea>");

上記は、デジタル証明書のキーを使用していない場合に機能します。しかし、キーがデジタル証明書からのものである場合、OAEP パディング エラーが発生します。

注: この質問は、OAEP パディングのデコード中にエラーが発生しましたの続きです

4

8 に答える 8

23

よくある間違いは、公開鍵を使用して復号化しようとすることです。

于 2009-10-24T17:23:18.723 に答える
16

私はこの正確な問題に遭遇しました。UnicodeEncoding.GetBytesは必ずしも の逆ではありません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.

RSACryptoServiceProvider.Decryptこれが失敗する理由です。Web 上の多くの暗号化/復号化の例では、Unicode エンコーディングが使用されています。Unicode エンコードを使用しないでください。代わりにConvert.FromBase64Stringandを使用してください。Convert.ToBase64String

于 2010-01-29T18:25:10.190 に答える
4

このエラーは通常、復号化に公開鍵を使用していることを示していますが、復号化には秘密鍵を使用する必要があります。試してみる。

于 2010-09-23T22:55:40.790 に答える
2

私の場合、間違ったパディング設定が原因でエラーが発生しました。

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

PHPのデフォルト値とnode-rsaのデフォルト値を使用openssl_public_encrypt()しました。OPENSSL_PKCS1_PADDINGkeypair.decrypt()RSA_PKCS1_OAEP_PADDING

これらのオプションも忘れずにチェックしてください。

于 2012-08-13T14:09:42.397 に答える
0

確認すべきもう 1 つのこと: 暗号化操作のために公開鍵を に渡すのを忘れた結果、復号化操作でこのエラーが発生しましたRSACryptoServiceProvider

于 2015-04-24T20:28:44.100 に答える
0

復号化に間違ったキーを使用していたときに、この問題が発生していました。

于 2017-01-27T17:20:45.303 に答える