サードパーティから公開鍵を取得するアプリケーションがあります。公開鍵は 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' => '私用のもの (2048)', 'private' => { '_phi' => '218..snip..380' , '_n' => '218..中略..113', '_q' => '148..中略..391', '_p' => '146..中略..343', '_u' = > '127..中略..655', '_dp' => '127..中略..093', '_dq' => '119..中略..413', '_d' => '190.. snip..533', '_e' => '65537' }, 'Cipher' => 'フグ' }, 'Crypt::RSA::Key::Private' );
RSAParameters オブジェクトへのマッピングは次のようになっていることがわかりました。
_ファイ = ??? _n = RSAParameters.モジュラス _q = RSAParameters.Q _p = RSAParameters.P _u = ??? _dp = RSAParameters.DP _dq = RSAParameters.DQ _d = RSAParameters.D _e = RSAParameters.指数 ??? = RSAParamaters.InverseQ
これらの値をロードすると (すべて上記と同じ方法で BigInteger クラスを使用します); 「Bad data」で失敗します。呼び出そうとするとエラーが発生します: rsa.ImportParameters(this.RsaParams);
このエラーのスタック トレースは次のとおりです。
System.Security.Cryptography.CryptographicException が処理されませんでした Message="不正なデータです。\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(文字列テキスト) C:\Doug\Development\SandboxApp2\SandboxApp2\Form1.cs:line 165 の SandboxApp2.Form1.btnGoDecrypter_Click (オブジェクト送信者、EventArgs e) で System.Windows.Forms.Control.OnClick (EventArgs e) で System.Windows.Forms.Button.OnClick (EventArgs e) で System.Windows.Forms.Button.OnMouseUp (MouseEventArgs mevent) で System.Windows.Forms.Control.WmMouseUp (Message& 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 (MSG& メッセージ) で 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、文字列 [] 引数) で Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() で System.Threading.ThreadHelper.ThreadStart_Context (オブジェクトの状態) で System.Threading.ExecutionContext.Run (ExecutionContext executionContext、ContextCallback コールバック、オブジェクトの状態) で System.Threading.ThreadHelper.ThreadStart() で
質問のこの部分のアイデアはありますか?
最後に、私は主に VB.NET の開発者ですが、C# に関しては両方の方法を使用しており、かなり流暢に話せるように感じています。ただし、暗号化に関しては初心者です。