Call the .NET component programming from the COM component

zhaozj2021-02-16  47

Author: Zhuxue Wu

In my programming practice, you need to pass encrypted string information (such as username and password, etc.) to the ASP page from the .NET web form page, and then decrypt the encrypted string on this page. If the passed is not an encrypted string, you can directly pass and receive in the ASP page, but the problem is how to decrypt the dense string in the .NET's Web Form page can be decrypted in the ASP? This is mainly because the ASP does not directly access the managers and components provided by .NET. At this time, we can only be implemented with COM components. Through COM interoperability We can generate COM components through .NET, then access the COM components in the ASP page.

This article implements the encrypted username and password pass from the .aspx page to the .asp page, here is the specific steps: these operations:

First, make a .NET assembly (VS.NET class library project) with encryption, decryption strings

This assembly will turn into COM components, use the DES symmetrical encrypted code, you can add your password, you can encrypt the decryption, support Chinese!

// File name: StringCrypt.cs

Using system;

Using system.Runtime.InteropServices;

Using system.security.cryptography;

Using system.io;

Using system.text;

Namespace Jonson

{

// Establish an interface first, this is COM must use

[GUID ("BF6F9C17-37FA-4AD9-9601-C11AD5316F2C")]

Public Interface IenCrypt

{

String Encrypt (String ptoencrypt, string skey);

String Decrypt (String Ptodecrypt, String Skey);

}

// Interface implementation

[GUID ("3fbdbb63-3c36-4602-89e1-73edb0f167d0")]

Public Class StringCrypt: icrypt

{

// encryption method

Public String Encrypt (String Ptoencrypt, String Skey)

{

DescryptoServiceProvider des = New DescryptoServiceProvider ();

// put the string in the BYTE array

BYTE [] INPUTBYTEARRAY = Encoding.default.getbytes (ptoencrypt);

// Byte [] INPUTBYTEARRAY = Encoding.unicode.getBytes (PToEncrypt);

/ / Establish the key and offset of the encryption object

// original text uses the getBytes method of the ASCIENCODING.ASCII method

// Make Enter the password must enter English text

DES.key = asciiencoding.ascii.getbytes (SKEY);

DES.IV = asciiencoding.ascii.getbytes (SKEY);

MemoryStream MS = New MemoryStream ();

CryptostReam Cs = New CryptostReam (MS, DES.CREATEENCRYPTOR (), CRYPTOSTREAMMODE.WRITE);

// Write the Byte Array Into The Crypto Street

// (IT WILL End Up in The Memory Stream)

CS.Write (InputByteArray, 0, InputByteaRray.length); cs.flushfinalblock ();

// Get The Data Back from the memory stream, and into a string

StringBuilder Ret = new stringbuilder ();

Foreach (byte b in ms.toarray ())

{

// Format As Hex

RET.APpendFormat ("{0: X2}", B);

}

Ret.toString ();

Return Ret.toString ();

}

// Decryption method

Public String Decrypt (String Ptodecrypt, String SKey)

{

DescryptoServiceProvider des = New DescryptoServiceProvider ();

// put the input string ion inte array

Byte [] InputByteArray = new byte [ptodecrypt.length / 2];

For (int x = 0; x

{

INT i = (Convert.Toint 32 (Ptodecrypt.Substring (x * 2, 2), 16);

INPUTBYTEARRAY [X] = (Byte) i;

}

/ / Establish the key and offset of the encryption object, this value is important, can not be modified

DES.key = asciiencoding.ascii.getbytes (SKEY);

DES.IV = asciiencoding.ascii.getbytes (SKEY);

MemoryStream MS = New MemoryStream ();

CryptostReam Cs = New Cryptostream (MS, DES.CREATEDECRYPTOR (), CRYPTOSTREAMMODE.WRITE);

// Flush The Data Throgh The Crypto Street Into The Memory Stream INTO THE

CS.Write (InputByteArray, 0, InputByteaRray.Length);

cs.flushfinalblock ();

// Get The Decrypted Data Back from The Memory Stream

// Establish a StringBuilder object, create the stream object, must turn the decrypted text into the flow object

StringBuilder Ret = new stringbuilder ();

Return system.text.Encoding.default.getstring (ms.toArray ());

}

}

}

Note: Note The above GUID is generated by creating a GUID tool in the VS.NET Tools menu, and this each COM component must be. When you enter a key, you must use the English characters, which are case sensitive, and the number of characters is 8, and it cannot be more, otherwise it is wrong.

Then use the VSTUAL Studio .Net Tools -> Vistual Studio .NET command prompt. Keep CD C: / in the command line

Sn -k mykey.snk

This generates a strong name file called myKey.snk in the C-drive root directory, then copy it to the above-described project directory (with the StringCrypt.cs file), close the prompt window. In the AssemblyInfo.cs file that automatically generated in the class library project of VS.NET

Change [Assembly: assemblykeyKeyFile (")] [Assembly: askEMBLYKEYFILE (" ../../ mykey.snk ")]

Change [Assembly: AssemblyVersion ("1.0. *")] [Assembly]] // Note: At this point your COM component version is 1.0.0.0

Then press the SHIFT CTRL B to generate a DLL library (using the Release mode), StringCrypt.dll. At this time, the assembly was established.

Second, register this assembly and create a type library

Still using the Visual Studio .NET command prompt in the Start menu

Enter your project catalog, assume that d: / project / bin / release

Enter in the dialog

D:

CD Project / Bin / Release

Then enter the dir command to see the StringCrypt.dll file.

Then enter: regaSM stringcrypt.dll

Then the StringCrypt.tlb type library file is generated in this directory. Do not close this prompt window.

At this time, this .dll's .NET assembly becomes a standard COM component, but it is not used, it must make it become a global COM component.

This REGASM utility will create a type library and register it in the Windows registry so that COM Services can access .NET components. After registering the .NET with REGASM, the standard Windows Customer can bind the classes in the component later. The process of registration components must be completed once. After the .NET component is registered, all COM customers can access it.

Third, add the assembly to the global assembly cache

Before using the .NET assembly, we must install the assembly to the global cache. To this end, enter the Visual Studio .NET prompt window, enter

Gacutil / I StringCrypt.dll

At this time, your DLL is copied to the global assembly cache. That is, this DLL component can be used in any hard disk of this computer.

Fourth, how to use

1. Generate a plus string in Source.aspx

Using Jonson;

...

Jonson.StringCrypt Crypt = new jonson.stringcrypt ();

String Tmpstr = UserName "^" password;

...

Strinfo = crypt.encrypt (tmpstr, "fk58fgju"); // fk58fgju is a key

Response.Redirect ("TARGET.ASP? INFO =" STRINFO);

2. Receive and decrypt strings in Target.asp page

INFO = Request.QueryString ("info")

Set obj = server.createObject ("jonson.stringcrypt")

Str1 = Obj.encrypt (Info, "FK58FGJU" // Decryption

转载请注明原文地址:https://www.9cbs.com/read-26533.html

New Post(0)