简体   繁体   English

Perl和.NET RSA一起工作? 从Perl公钥加密.NET? 从Perl加载私钥?

[英]Perl & .NET RSA working together? Encrypting in .NET from Perl public key? Loading private key from Perl?

I've got an application that is going to be taking a public key from a 3rd party. 我有一个应用程序将从第三方获取公钥。 The public key is generated in Perl using Crypt::RSA::Key. 使用Crypt :: RSA :: Key在Perl中生成公钥。 Using the BigInteger class , I'm able to load this key and encrypt values that should be able to be decrypted by the private key. 使用BigInteger类 ,我能够加载此密钥并加密应该能够通过私钥解密的值。 My code for doing that is: 我这样做的代码是:

Setting up the properties for later usage: 设置属性以供以后使用:

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); }
}

// ... snip ... // // ...剪... //

Initializing the properties in a constructor: 在构造函数中初始化属性:

    RSAParameters rsaParameters = new RSAParameters();
    rsaParameters.Exponent = this.Exponent.getBytes();
    rsaParameters.Modulus = this.Modulus.getBytes();
    this.RsaParams = rsaParameters;

// ... snip ... // // ...剪... //

Doing the encryption. 进行加密。 Note text is my value to encrypt; 注意文本是我加密的价值; ret is my value being returned: ret是我返回的价值:

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);

The majority of this code was pulled from someone else's project who claims this all works for them. 大部分代码都来自其他人的项目,他们声称这一切都适合他们。 Unfortunately, I'm not able to see their actual input values. 不幸的是,我无法看到他们的实际输入值。

This is failing with an invalid value being returned to the 3rd party. 这是失败的,返回给第三方的值无效。

FIRST QUESTION - do you see anything amiss about the code above? 第一个问题 - 你看到上面的代码有什么不妥吗?

SECONDLY 其次

I tried debugging this by talking to the 3rd party and getting a private key from them. 我尝试通过与第三方交谈并从中获取私钥来调试此问题。 I'm failing when I try to load the full private key. 当我尝试加载完整的私钥时,我失败了。 I can't figure out the mapping between the Perl's object data and .NET RSAParameters. 我无法弄清楚Perl的对象数据和.NET RSAParameters之间的映射。 The key data that I have is: 我拥有的关键数据是:

$VAR1 = bless( {

'Version' => '1.91', 'Checked' => 0, 'Identity' => 'stuff for me (2048)', 'private' => { '_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' }, 'Cipher' => 'Blowfish' }, 'Crypt::RSA::Key::Private' ); 'Version'=>'1.91','Checked'=> 0,'Identity'=>'stuff for me(2048)','private'=> {'_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'},'Cipher'=>'Blowfish'},'Crypt :: RSA :: Key :: Private');

I have figured out that the mapping to the RSAParameters objects is as such: 我已经发现到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

When I load these values (all using the BigInteger class in the same manner as above); 当我加载这些值时(所有使用BigInteger类的方式与上面相同); I fail with a "Bad data." 我失败的是“糟糕的数据”。 error when I attempt to call: rsa.ImportParameters(this.RsaParams); 我尝试调用时出错:rsa.ImportParameters(this.RsaParams);

The stack trace of this error is: 此错误的堆栈跟踪是:

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()

Any ideas for this portion of the question? 关于这部分问题的任何想法?

Finally, I'm primarily a VB.NET developer but swing both ways when it comes to c# and I feel like I'm fairly fluent in it. 最后,我主要是一个VB.NET开发人员,但是当涉及到c#时我会两种方式,我觉得我相当流利。 I am, however, a neophyte when it comes to encryption. 然而,在加密方面,我是一个新手。

Check out the How to use RSACryptoServiceProvider.ImportParameters() thread in the MSDN forums. 查看MSDN论坛中的如何使用RSACryptoServiceProvider.ImportParameters()线程。 It addresses the same problem you are asking about here. 它解决了你在这里问的同样问题。 It might be possible that getBytes is munging your public key data. getBytes可能正在修改您的公钥数据。 One of the messages posted by Voss in the thread includes a fix to the BigInteger class for an issue with getBytes. Voss在该主题中发布的消息之一包括针对getBytes问题修复BigInteger类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM