我有一个应用程序将从第三方获取公钥。公钥是在 Perl 中使用 Crypt::RSA::Key 生成的。使用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);
大部分代码是从其他人的项目中提取的,他们声称这一切都适用于他们。不幸的是,我无法看到他们的实际输入值。
这失败了,将无效值返回给第 3 方。
第一个问题- 你看到上面的代码有什么问题吗?
其次
我尝试通过与第 3 方交谈并从他们那里获取私钥来调试它。当我尝试加载完整的私钥时失败了。我无法弄清楚 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 未处理 消息="错误数据。\r\n" 来源="mscorlib" 堆栈跟踪: 在 System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 小时) 在 System.Security.Cryptography.Utils._ImportKey(SafeProvHandle hCSP,Int32 keyNumber,CspProviderFlags 标志,对象 cspObject,SafeKeyHandle & hKey) 在 System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters 参数) 在 C:\Doug\Development\SandboxApp2\SandboxApp2\SandboxDecrypter.cs:line 101 中的 SandboxApp2.SandboxDecrypter.DecryptText(String text) 在 C:\Doug\Development\SandboxApp2\SandboxApp2\Form1.cs:line 165 中的 SandboxApp2.Form1.btnGoDecrypter_Click(Object sender, EventArgs e) 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(消息和 m,MouseButtons 按钮,Int32 点击) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.ButtonBase.WndProc(消息和 m) 在 System.Windows.Forms.Button.WndProc(消息和 m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(味精和味精) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID,Int32 原因,Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.Run(窗体 mainForm) 在 C:\Doug\Development\SandboxApp2\SandboxApp2\Program.cs:line 17 中的 SandboxApp2.Program.Main() 在 System.AppDomain._nExecuteAssembly(程序集程序集,字符串 [] 参数) 在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,String [] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart()
对这部分问题有什么想法吗?
最后,我主要是 VB.NET 开发人员,但在涉及 c# 时左右摇摆不定,我觉得我对它相当流利。然而,在加密方面,我是个新手。