繁体   English   中英

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

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

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

设置属性以供以后使用:

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;

// ...剪... //

进行加密。 注意文本是我加密的价值; 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);

大部分代码都来自其他人的项目,他们声称这一切都适合他们。 不幸的是,我无法看到他们的实际输入值。

这是失败的,返回给第三方的值无效。

第一个问题 - 你看到上面的代码有什么不妥吗?

其次

我尝试通过与第三方交谈并从中获取私钥来调试此问题。 当我尝试加载完整的私钥时,我失败了。 我无法弄清楚Perl的对象数据和.NET RSAParameters之间的映射。 我拥有的关键数据是:

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

我已经发现到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

当我加载这些值时(所有使用BigInteger类的方式与上面相同); 我失败的是“糟糕的数据”。 我尝试调用时出错:rsa.ImportParameters(this.RsaParams);

此错误的堆栈跟踪是:

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

关于这部分问题的任何想法?

最后,我主要是一个VB.NET开发人员,但是当涉及到c#时我会两种方式,我觉得我相当流利。 然而,在加密方面,我是一个新手。

查看MSDN论坛中的如何使用RSACryptoServiceProvider.ImportParameters()线程。 它解决了你在这里问的同样问题。 getBytes可能正在修改您的公钥数据。 Voss在该主题中发布的消息之一包括针对getBytes问题修复BigInteger类。

暂无
暂无

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

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