2010-12-14 15 views
3

我有一個將從第三方獲取公鑰的應用程序。公鑰使用Crypt :: RSA :: Key在Perl中生成。使用BigInteger class,我可以加載此密鑰並加密應該能夠被私鑰解密的值。我做這件事的代碼是:Perl和.NET RSA一起工作?用Perl公鑰加密.NET?從Perl加載私鑰?

設置的屬性,以後使用:

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

'版本'=> '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' }, '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#,我覺得我在裏面還算流暢。然而,在加密方面,我是一位新手。

回答