Asymmetric encryption, decryption, validation helper class Rsasecurityhelper

Source: Internet
Author: User
Tags asymmetric encryption

<summary>

Private key and public key generated by asymmetric encryption

</summary>

<param name= "Privatekey" > Private key </param>

<param name= "PublicKey" > Public key </param>

public static void Generatersakey (out string privatekey, out string publickey)

#region Asymmetric Data encryption (public key cryptography)

<summary>

Asymmetric encryption of string data, returning encrypted data

</summary>

<param name= "PublicKey" > Public key </param>

<param name= "originalstring" > Strings to be encrypted </param>

<returns> Data after encryption </returns>

public static string Rsaencrypt (String publickey, String originalstring)

<summary>

An asymmetric encrypted byte array that returns the encrypted data

</summary>

<param name= "PublicKey" > Public key </param>

<param name= "originalbytes" > byte array to encrypt </param>

<returns> returns the encrypted data </returns>

public static string Rsaencrypt (String publickey, byte[] originalbytes)

#endregion

#region Asymmetric decryption (private key decryption)

<summary>

Asymmetric decrypted string that returns the decrypted data

</summary>

<param name= "Privatekey" > Private key </param>

<param name= "encryptedstring" > Data to Decrypt </param>

<returns> returns the decrypted data </returns>

public static string Rsadecrypt (String Privatekey, String encryptedstring)

<summary>

An asymmetric decrypted byte array that returns the decrypted data

</summary>

<param name= "Privatekey" > Private key </param>

<param name= "Encryptedbytes" > Data to Decrypt </param>

<returns></returns>

public static string Rsadecrypt (String Privatekey, byte[] encryptedbytes)

#endregion

#region Asymmetric cryptographic signature, authentication

<summary>

Signing data with asymmetric encryption

</summary>

<param name= "Privatekey" > Private key </param>

<param name= "originalstring" > Strings to be encrypted </param>

<returns> Data after encryption </returns>

public static string Rsaencrypsignature (String Privatekey, String originalstring)

<summary>

A string that encrypts the signature on the private key and validates it with the public key

</summary>

<param name= "originalstring" > unencrypted text, such as machine code </param>

<param name= "encrytedstring" > encrypted text, such as registering serial number </param>

<returns> If validation returns True, otherwise false</returns>

public static bool Validate (string originalstring, String encrytedstring)

<summary>

A string that is encrypted with the private key, which is validated with the public key

</summary>

<param name= "originalstring" > unencrypted text, such as machine code </param>

<param name= "encrytedstring" > encrypted text, such as registering serial number </param>

<param name= "PublicKey" > Asymmetric Encryption Public key </param>

<returns> If validation returns True, otherwise false</returns>

public static bool Validate (string originalstring, String encrytedstring, String publickey)

2

Generate encryption and decryption private key, public key

String publickey = "";

String privatekey = "";

Rsasecurityhelper.generatersakey (out Privatekey, out publickey);

String originalstring = "TestData";

String encryptedstring = Rsasecurityhelper.rsaencrypt (PublicKey, originalstring);

String originalString2 = Rsasecurityhelper.rsadecrypt (Privatekey, encryptedstring);

if (originalstring = = originalString2)

{

Messageutil.showtips ("Decryption is completely correct");

}

Else

{

Messageutil.showwarning ("Decryption failed");

}

String regcode = Rsasecurityhelper.rsaencrypsignature (Privatekey, originalstring);

BOOL validated = Rsasecurityhelper.validate (originalstring, Regcode, PublicKey);
Messageutil.showtips (validated? "Validation succeeded": "Validation Failed");

3

<summary>

Check that the user is registered every time the program runs

</summary>

<returns> returns True if the user is already registered, otherwise false</returns>

public bool Checkregister ()

{

First get the user's registration code for comparison

String serialnumber = String. Empty; Registration code

RegistryKey reg = Registry.CurrentUser.OpenSubKey (Uiconstants.softwareregistrykey, true);

if (null! = reg)

{

SerialNumber = (string) Reg. GetValue ("SerialNumber");

Portal.gc.bRegisted = Portal.gc.Register (serialnumber);

}

return Portal.gc.bRegisted;

}

<summary>

Call the asymmetric encryption method to verify the serial number

</summary>

<param name= "SerialNumber" > correct serial number </param>

<returns> false</returns> If successful returns true

public bool Register (String serialnumber)

{

String hardnumber = Hardwareinfohelper.getcpuid ();

Return Rsasecurityhelper.validate (Hardnumber, serialnumber);

}

public string Gethardnumber ()

{

return Hardwareinfohelper.getcpuid ();
}

Asymmetric encryption, decryption, validation helper class Rsasecurityhelper

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.