[英]Decrypting Crypto++ RSA cipher text in C# causes exception
我已經使用Crypto ++在C ++中編寫了3個函數來生成密鑰對,加密和解密字符串。 加密++方面 :
//Decode public key
RSA::PublicKey pbKeyDecoded;
StringSource ss2(publicKey, true, new Base64Decoder);
pbKeyDecoded.BERDecode(ss2);
Integer m = Integer((const byte*)plaintext.data(), plaintext.size());
Integer crypted = pbKeyDecoded.ApplyFunction(m);
...
我要做的是生成密鑰,對它進行DER編碼,然后將其編碼為Base64。 之后,我將通過公用密鑰對明文進行加密,然后將私鑰和加密均作為base64編碼的字符串保存在兩個單獨的文件中。
現在到C#。 我正在讀取base64字符串,對其進行解碼並通過AsnParser加載它們,看起來似乎加載得很好。 然后我打電話給Decrypt
。 C#端 :
AsnKeyParser keyParser = new AsnKeyParser("rsa-public.der");
RSAParameters publicKey = keyParser.ParseRSAPublicKey();
CspParameters csp = new CspParameters;
csp.KeyContainerName = "RSA Test (OK to Delete)";
csp.ProviderType = PROV_RSA_FULL; // 1
csp.KeyNumber = AT_KEYEXCHANGE; // 1
RSACryptoServiceProvider rsp = new RSACryptoServiceProvider(csp);
rsp.PersistKeyInCsp = false;
rsp.ImportParameters(privateKey);
//Causes exception here..
var data = rsp.Decrypt(cipherArr, true);
...
但是,當我嘗試使用fOAEP = true
解密它時, fOAEP = true
異常錯誤: CryptographicException:解碼OAEP填充時發生錯誤。 如果我傳遞的fOAEP = false
則會得到CryptographicException:該參數不正確。
為什么在嘗試解密Crypto ++密文時C#中出現異常?
...當我嘗試對其解密時,出現異常錯誤:CryptographicException:解碼OAEP填充時發生錯誤。 那就是如果我為fOAEP bool傳遞true,如果我傳遞false則得到CryptographicException:參數不正確。
您遇到與在Crypto ++中使用原始RSA算法加密和解密消息相同的問題嗎? 以及如何將Crypto ++ RSA與C#RSA加密服務提供商同步? “原始RSA”計划必須是我們月份。
在等式的Crypto ++方面, 您正在執行原始RSA 。 您只是在應用轉發功能(即冪運算),而未格式化消息:
//Decode public key
RSA::PublicKey pbKeyDecoded;
StringSource ss2(publicKey, true, new Base64Decoder);
pbKeyDecoded.BERDecode(ss2);
Integer m = Integer((const byte*)plaintext.data(), plaintext.size());
Integer crypted = pbKeyDecoded.ApplyFunction(m);
...
在C#方面, 您正在使用PKCS#1.5填充或OAEP填充使用PKCS#1 執行RSA解密 :
RSACryptoServiceProvider rsp = new RSACryptoServiceProvider(csp);
rsp.PersistKeyInCsp = false;
rsp.ImportParameters(privateKey);
//Causes exception here..
var data = rsp.Decrypt(cipherArr, true);
我不清楚您的代碼的C#版本是否可以執行OAEP填充,因為它需要特定版本的CLR。 您可能只有PKCS填充可用。
我相信您有兩種選擇。 首先,您可以在Crypto ++中使用標准的RSA加密方法。 Crypto ++ Wiki在RSA密碼學和RSA加密方案中列出了它們:
typedef RSAES<PKCS1v15>::Decryptor RSAES_PKCS1v15_Decryptor;
typedef RSAES<PKCS1v15>::Encryptor RSAES_PKCS1v15_Encryptor;
typedef RSAES<OAEP<SHA> >::Decryptor RSAES_OAEP_SHA_Decryptor;
typedef RSAES<OAEP<SHA> >::Encryptor RSAES_OAEP_SHA_Encryptor;
其次,您需要在C#中執行Raw RSA。 要在C#中執行Raw RSA,您將需要獲取BigInteger類並手動應用逆函數。
我鼓勵您將RSA加密與OAEP填充一起使用。 如果OAEP不可用,則第二個選擇是PKCS填充。 最后,如果您所擁有的只是Raw RSA,那么我會尋找另一個加密系統,因為Raw RSA是如此不安全。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.