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