Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Perl&。NET RSA合作?从Perl公钥在.NET中加密?从Perl加载私钥?_C#_Perl_Parameters_Rsa_Rsacryptoserviceprovider - Fatal编程技术网

C# Perl&。NET RSA合作?从Perl公钥在.NET中加密?从Perl加载私钥?

C# Perl&。NET RSA合作?从Perl公钥在.NET中加密?从Perl加载私钥?,c#,perl,parameters,rsa,rsacryptoserviceprovider,C#,Perl,Parameters,Rsa,Rsacryptoserviceprovider,我有一个应用程序将从第三方获取公钥。公钥是在Perl中使用Crypt::RSA::key生成的。使用,我能够加载这个密钥并加密应该能够由私钥解密的值。我的代码是: 设置属性以便以后使用: internal RSAParameters RsaParams { get { return this._rsaParams; } set { this._rsaParams = value; } } public BigInteger Modulus { get { return

我有一个应用程序将从第三方获取公钥。公钥是在Perl中使用Crypt::RSA::key生成的。使用,我能够加载这个密钥并加密应该能够由私钥解密的值。我的代码是:

设置属性以便以后使用:

internal RSAParameters RsaParams
{
    get { return this._rsaParams; }
    set { this._rsaParams = value; }
}

public BigInteger Modulus
{
    get { return new BigInteger(this._modulus, 10); }
}

public BigInteger Exponent
{
    get { return new BigInteger(this._exponent, 10); }
}
    RSAParameters rsaParameters = new RSAParameters();
    rsaParameters.Exponent = this.Exponent.getBytes();
    rsaParameters.Modulus = this.Modulus.getBytes();
    this.RsaParams = rsaParameters;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

rsa.ImportParameters(this.RsaParams);

Byte[] toEncode = encoding.GetBytes(text);
Byte[] encryptedVal = rsa.Encrypt(toEncode, true);

ret = Convert.ToBase64String(encryptedVal);
//。。。剪断//

初始化构造函数中的属性:

internal RSAParameters RsaParams
{
    get { return this._rsaParams; }
    set { this._rsaParams = value; }
}

public BigInteger Modulus
{
    get { return new BigInteger(this._modulus, 10); }
}

public BigInteger Exponent
{
    get { return new BigInteger(this._exponent, 10); }
}
    RSAParameters rsaParameters = new RSAParameters();
    rsaParameters.Exponent = this.Exponent.getBytes();
    rsaParameters.Modulus = this.Modulus.getBytes();
    this.RsaParams = rsaParameters;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

rsa.ImportParameters(this.RsaParams);

Byte[] toEncode = encoding.GetBytes(text);
Byte[] encryptedVal = rsa.Encrypt(toEncode, true);

ret = Convert.ToBase64String(encryptedVal);
//。。。剪断//

进行加密。注意文本是我要加密的值;ret是返回的我的值:

internal RSAParameters RsaParams
{
    get { return this._rsaParams; }
    set { this._rsaParams = value; }
}

public BigInteger Modulus
{
    get { return new BigInteger(this._modulus, 10); }
}

public BigInteger Exponent
{
    get { return new BigInteger(this._exponent, 10); }
}
    RSAParameters rsaParameters = new RSAParameters();
    rsaParameters.Exponent = this.Exponent.getBytes();
    rsaParameters.Modulus = this.Modulus.getBytes();
    this.RsaParams = rsaParameters;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

rsa.ImportParameters(this.RsaParams);

Byte[] toEncode = encoding.GetBytes(text);
Byte[] encryptedVal = rsa.Encrypt(toEncode, true);

ret = Convert.ToBase64String(encryptedVal);
这段代码的大部分是从其他人的项目中提取的,他们声称这一切都对他们有用。不幸的是,我无法看到它们的实际输入值

此操作失败,返回给第三方的值无效

第一个问题-您认为上面的代码有什么问题吗

其次

我试着通过与第三方对话并从他们那里获得私钥来调试它。尝试加载完整私钥时失败。我无法理解Perl的对象数据和.NET参数之间的映射。我掌握的关键数据是:

$VAR1 = bless( {
'版本'=>'1.91', “选中”=>0, “身份”=>“我的东西(2048)”, “私人”=>{ “_phi”=>“218..snip..380”, “_n”=>“218..snip..113”, “_q”=>“148..snip..391”, “_p'=>”146..snip..343', “_u=>”127..snip..655', “_dp”=>“127..snip..093”, “_dq”=>“119..snip..413”, “_d”=>“190..snip..533”, “_e”=>“65537” }, “密码”=>“河豚” },'Crypt::RSA::Key::Private')

我已经了解到,到RSAParameters对象的映射如下所示:

_phi = ??? _n = RSAParameters.Modulus _q = RSAParameters.Q _p = RSAParameters.P _u = ??? _dp = RSAParameters.DP _dq = RSAParameters.DQ _d = RSAParameters.D _e = RSAParameters.Exponent ??? = RSAParamaters.InverseQ _φ=??? _n=参数。模数 _q=RSA参数。q _p=rsa参数。p _u=??? _dp=rsa参数.dp _dq=RSA参数。dq _d=RSA参数。d _e=RSA参数指数 ??? = rsaparters.InverseQ 当我加载这些值时(所有这些值都以与上面相同的方式使用BigInteger类);我失败,出现“错误数据”。尝试调用时出错: rsa.ImportParameters(this.rsaparms)

此错误的堆栈跟踪是:

System.Security.Cryptography.CryptographicException was unhandled Message="Bad Data.\r\n" Source="mscorlib" StackTrace: at System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr) at System.Security.Cryptography.Utils._ImportKey(SafeProvHandle hCSP, Int32 keyNumber, CspProviderFlags flags, Object cspObject, SafeKeyHandle& hKey) at System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters parameters) at SandboxApp2.SandboxDecrypter.DecryptText(String text) in C:\Doug\Development\SandboxApp2\SandboxApp2\SandboxDecrypter.cs:line 101 at SandboxApp2.Form1.btnGoDecrypter_Click(Object sender, EventArgs e) in C:\Doug\Development\SandboxApp2\SandboxApp2\Form1.cs:line 165 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at SandboxApp2.Program.Main() in C:\Doug\Development\SandboxApp2\SandboxApp2\Program.cs:line 17 at System.AppDomain._nExecuteAssembly(Assembly 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) at System.Threading.ThreadHelper.ThreadStart() System.Security.Cryptography.CryptographyException未处理 Message=“错误数据。\r\n” Source=“mscorlib” 堆栈跟踪: 在System.Security.Cryptography.CryptographyException.ThrowCryptogaphicException(Int32 hr)中 在System.Security.Cryptography.Utils.\u ImportKey(SafeProvHandle hCSP、Int32 keyNumber、CsProviderFlags标志、对象CsObject、SafeKeyHandle和hKey) at System.Security.Cryptography.rsacryptserviceprovider.ImportParameters(rsapartameters) 在C:\Doug\Development\SandboxApp2\SandboxApp2\SandboxDecrypter.cs中的SandboxApp2.SandboxDecrypter.DecryptText(字符串文本)处:第101行 在SandboxApp2.Form1.btnGoDecrypter\中,单击C:\Doug\Development\SandboxApp2\SandboxApp2\Form1.cs中的(对象发送方,事件参数e):第165行 在System.Windows.Forms.Control.OnClick(EventArgs e)中 在System.Windows.Forms.Button.OnClick(EventArgs e)中 在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs-mevent)上 在System.Windows.Forms.Control.WmMouseUp(Message&m、MouseButtons按钮、Int32单击) 位于System.Windows.Forms.Control.WndProc(Message&m) 位于System.Windows.Forms.ButtonBase.WndProc(Message&m) 在System.Windows.Forms.Button.WndProc(Message&m)中 在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)中 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)中 在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd、Int32 msg、IntPtr wparam、IntPtr lparam) 在System.Windows.Forms.UnsafentiveMethods.DispatchMessageW(MSG&MSG)中 位于System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafentiveMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID、Int32 reason、Int32 pvLoopData) 位于System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文) 位于System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文) 在System.Windows.Forms.Application.Run(Form mainForm)中 在C:\Doug\Development\SandboxApp2\SandboxApp2\Program.cs中的SandboxApp2.Program.Main()处:第17行 位于System.AppDomain.\u nexecutestAssembly(程序集,字符串[]args) 位于System.AppDomain.ExecuteAssembly(字符串汇编文件、证据汇编安全性、字符串[]args) 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上 位于System.Threading.ThreadHelper.ThreadStart\u上下文(对象状态) 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态) 位于System.Threading.ThreadHelper.ThreadStart()处 这部分问题有什么想法吗?

最后,我主要是一名VB.NET开发人员,但在c#方面,我感觉自己相当流利。然而,我对加密还是个新手。

请查看MSDN论坛中的帖子。它解决了你在这里所问的同样的问题。getBytes可能正在读取您的公钥数据。Voss在线程中发布的其中一条消息包含对BigInteger类的修复,以解决getBytes问题