Do Perl & .NET RSA work together? Encryption in .NET from Perl public key? Downloading a secret key from Perl?

I have an application that will use a third-party public key. The public key is created in Perl using Crypt :: RSA :: Key. Using the BigInteger class , I can load this key and encrypt the values ​​that should be decrypted with the private key. My code for this:

Setting properties for future use:

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); }
}

// ... snip ... //

Initializing properties in the constructor:

    RSAParameters rsaParameters = new RSAParameters();
    rsaParameters.Exponent = this.Exponent.getBytes();
    rsaParameters.Modulus = this.Modulus.getBytes();
    this.RsaParams = rsaParameters;

// ... snip ... //

Perform encryption. The text of the note is my value for encryption; ret returns my value:

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

Most of this code was pulled from another project that claims everything works for them. Unfortunately, I cannot see their actual input values.

, .

- - ?

, . , . Perl .NET RSAParameters. , :

$VAR1 = bless( {

'' = > '1.91', 'Checked' = > 0, 'Identity' = > ' (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, , #, , . , , , .

+3
1

RSACryptoServiceProvider.ImportParameters() MSDN. , . , getBytes . , Voss , BigInteger getBytes.

0

Source: https://habr.com/ru/post/1780121/


All Articles