簡體   English   中英

解碼 OAEP 填充時出錯

[英]Error occurred while decoding OAEP padding

使用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 填充問題時發生錯誤的延續

一個常見的錯誤是嘗試使用公鑰解密。

我遇到了這個確切的問題。 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失敗的原因。 網絡上的許多加密/解密示例都使用 Unicode 編碼。 不要使用 Unicode 編碼。 請改用Convert.FromBase64StringConvert.ToBase64String

此錯誤通常表示您正在使用公鑰進行解密,而您應該使用私鑰進行解密。 試一試。

在我的情況下,錯誤是由錯誤的填充設置引起的。

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

我將openssl_public_encrypt()OPENSSL_PKCS1_PADDING作為PHP 中的默認值和keypair.decrypt()node-rsa 中的默認值RSA_PKCS1_OAEP_PADDING

所以不要忘記檢查這些選項。

僅供參考,您仍然可以以正確的密鑰序列(encr:pub key, decr:priv key)進行(en/de)加密 - 即仍然可以使用私鑰解密此錯誤 - 它可能是錯誤的私鑰(即來自另一個證書/密鑰對),而不是與您最初加密的公鑰配對的那個。 如果您關閉 OAEP 填充並獲得“錯誤數據”異常,則這是另一個跡象。

RSA 加密可能會導致不可讀的字符,請確保在寫入/讀取加密結果時不要因為特殊字符指示某事結束而將字符串剪掉; 例如,您不能使用 strlen,因為它會在遇到字符串中的 '\\0' 時停止。

另一件事要檢查:由於忘記將公鑰傳遞到RSACryptoServiceProvider進行加密操作,它在解密操作中給了我這個錯誤。

當我們使用錯誤的密鑰進行解密時,我們遇到了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM