Send an encryption method

Source: Internet
Author: User

Disclaimer: Recently, many websites have quoted my articles and omitted the author's information. Please refer to this article and do not omit the author's information.

// Declare the number of digits of the random number generated by the encrypted character
Public const int length = 32;
// Declare the separator length before and after encryption characters
Public const int Len = 4;

/// Method function: encrypt data based on parameters
/// Parameters:
/// String strencrypt; character to be encrypted
/// Return result: Encrypted character
Public static byte [] sourceencrypt (string strencrypt)
{
String source = encrypt (strencrypt );
// First
String first = encrypt (source. substring (0, Len ));
// Second
String second = encrypt (source. substring (LEN-1, source. Length-len ));
// Third
String third = encrypt (source. substring (source. Length-Len, Len ));
// Four
Byte [] Random = new byte [length];
Rngcryptoserviceprovider RNG = new rngcryptoserviceprovider ();
RNG. getnonzerobytes (random );
String four = encrypt (system. Text. encoding. ASCII. getstring (random ));
String all = first + second + third + four;
Return System. Text. encoding. ASCII. getbytes (all );
} // End sourceencrypt

/// Method: encrypt Parameters
/// Parameters:
/// String strencrypt: the character to be encrypted
/// Return result: Encrypted character
Public static string encrypt (string strencrypt)
{
Sha256managed Sha = new sha256managed ();
Byte [] value = Sha. computehash (system. Text. encoding. ASCII. getbytes (strencrypt ));
Return System. Text. encoding. ASCII. getstring (value );
} // End encrypt

/// Method: Compare Parameters
/// Parameters:
/// String strsource: Original Character
/// String strobject: Target character
/// Return result: whether bool is equal
Public static bool compareencrypt (byte [] strsource, string strobject)
{
Bool result = true;
Byte [] strobject = sourceencrypt (strobject );
// Strobject = sourceencrypt (strobject );
If (strsource = NULL) | (strobject = NULL) | (strsource. length! = Strobject. Length ))
Result = false;
Else
{
// Strsource = strsource. substring (0, strsource. Length-length );
// Strobject = strobject. substring (0, strobject. Length-length );
// Byte [] source = system. Text. encoding. ASCII. getbytes (strsource );
// Byte [] OBJ = system. Text. encoding. ASCII. getbytes (strobject );
Byte [] source = strsource;
Byte [] OBJ = strobject;
If (source. length! = Obj. length)
Result = false;
Else
{
For (INT I = 0; I <source. Length-length; I ++)
{
If (source [I]! = OBJ [I])
{
Result = false;
Break;
}
}
}
}

Return result;
} // End compareyencrypt

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.