gpt4 book ai didi

c# - "Bad Data"使用 RSACryptoProvider 从在 PHP 中加密的字符串解密时

转载 作者:太空宇宙 更新时间:2023-11-03 14:20:46 26 4
gpt4 key购买 nike

我正在尝试用 C# 解密一个在 PHP 中使用 RSA 加密的字符串,但我一直都做不好。我已将问题浓缩为 .NET 和 PHP 中的两个示例测试应用程序:

在 C# 中:

class Program
{
static void Main(string[] args)
{

RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
string fileName = @"C:\privateKey";
if (File.Exists(fileName))
{
provider.FromXmlString(File.ReadAllText(fileName));
}
else
{
File.WriteAllText(fileName, provider.ToXmlString(true));
}
if(args.Length > 0)
{
string decrypted = Encoding.UTF8.GetString(provider.Decrypt(Convert.FromBase64String(args[0]), false));
Console.WriteLine("decrypted: {0}", decrypted);
}
else
{
Console.WriteLine(provider.ToXmlString(false));
}
Console.ReadLine();
}
}

我第一次运行它时,它输出(格式很好):

<RSAKeyValue>
<Modulus>
2P8G+ospDboN87+FIWSzFA9E1QVXCDy4bO9YZPe1MTcxO7VhlBYmLy1J7b
rSheaTa8RamrdeXe2Ev0y5O7zyxo/Bxw4W2UfnVAAFK+bgb8f0FicnSosAlGC95m5Goid9wWPf/QtqDa
NxSMBBjULA1WKzA7+3HQncAenODYypOiE=
</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>

我已验证 key 在运行之间正确保存和加载。

然后我使用 Crypt_RSA Package 在 PHP 中执行此操作:

include('Crypt/RSA.php');
$keyXML = <<<EOS
<RSAKeyValue>
<Modulus>2P8G+ospDboN87+FIWSzFA9E1QVXCDy4bO9YZPe1MTcxO7VhlBYmLy1J7brSheaTa8RamrdeXe2Ev0y5O7zyxo/Bxw4W2UfnVAAFK+bgb8f0FicnSosAlGC95m5Goid9wWPf/QtqDaNxSMBBjULA1WKzA7+3HQncAenODYypOiE=</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
EOS;
$doc = DOMDocument::loadXML($keyXML);
$modulus = $doc->getElementsByTagName("Modulus")->item(0)->nodeValue;
$exponent = $doc->getElementsByTagName("Exponent")->item(0)->nodeValue;
$key = Crypt_RSA_Key::factory(base64_decode($modulus), base64_decode($exponent), 'public');

$crypt = new Crypt_RSA();

echo $crypt->encrypt("hello", $key)

这会输出我的解密字符串:

CBGFdK+NXq39ZigXkW6JChHHCCUTzHzjsEPqdoaNJtZgTCRQfIIR6IU4m5LOHGidLaLWN7cIM9ImNdSsAsMH9awkZ7uW0KXdqQjhVGeXzmC6EuSHqrSzYiIZp+1F5Rxhg/JFD/Or3eQ+UIHm/4YtNTyys6S3maClitk5KV68lhs=

最后,当我将此作为输入再次运行我的 C# 应用程序时,调用 Decrypt 方法时出现以下异常:

System.Security.Cryptography.CryptographicException was unhandled
Message=Bad Data.
Source=mscorlib
StackTrace:
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.RSACryptoServiceProvider.DecryptKey(SafeKeyHandle pKeyContext, Byte[] pbEncryptedKey, Int32 cbEncryptedKey, Boolean fOAEP, ObjectHandleOnStack ohRetDecryptedKey)
at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP)
at ConsoleApplication1.Program.Main(String[] args) in C:\development\FundCentric\src\Utilities\ConsoleApplication1\ConsoleApplication1\Program.cs:line 27
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

坦率地说,我现在不知道还能尝试什么。非常欢迎任何关于可能出错的建议或我可以尝试的其他建议。

最佳答案

已知问题。恢复 block 中的字节顺序。

关于c# - "Bad Data"使用 RSACryptoProvider 从在 PHP 中加密的字符串解密时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5474234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com