簡體   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