How to protect data in .NET framework (on)

zhaozj2021-02-16  52

We all know that the data makes our computer processing basic elements, now the information age, so the data is also exploding, and our computer is handling this data every moment, for a company, maybe the information is their life, For example: a company's customer resource accident leaks then I want to come to the company to be the biggest loss. Therefore, the safety of the computer is especially important. Here we don't rate the security of the OS or the security of OS, these left Black can go to research. Today I want to introduce. How do we protect our data in .NET, so that we first think of it is encrypted data, it is the most effective way to protect data. If you have a nonsense, don't say much. Here we use the .NET's namespace under the namespace .Security.cryptography If you want to know more information about the namespace, please visit MSDN Help.

In general, we have a database in the database, the login database is a password, so we think our data (username and password) is safe, is this? I think it is basically like this, but if there is some It is a way to solve this problem with this password, and the method of solving this problem. We use the Hash encryption method. This encrypted approach is not coming back. That is to say If you can't get a clear text, we will save this secret in the database. Even if you open it, you can make the program to identify the correct user, we use the same way to entered the password (clear text) encryption. The password that again and the database is compared to determine whether it is a legitimate user. The previous is a simple example. Let's take a look at how to achieve this encryption method in .NET. First of all, we must thank Microsoft to provide us The framework, it almost thought of everything we have to do, in the namespace mentioned above, there is the class we need to complete the features I said above.

Inverse encryption algorithm (haveh algorithm):

We can see a class of SHA1 and MD5 in this namespace. This is also the two irreversible encrypted haveh algorithms we use, first of all, let's introduce SHA1 next is MD5.

SHA1:

First, we look at his description: The hash is used as a unique value of fixed size representing a large amount of data Hashes of two sets of data should match if the corresponding data also matches Small changes to the data result in large,.. Unpredictable changes in the hash.

The Hash Size for the sha1 algorithm is 160 bits.

Second, we will use SHA1CRYPTOSERVICEPROVIDER to instantiate an instance of SHA1, and then use the computehash method to calculate the code of his Hash code as follows:

Private hashalgorithm mhash;

Private string hashstring (string value, string hashdes) {

BYTE [] BYTVALUE;

Byte [] bythash;

Mhash = sethash (hashdes);

// Transform the original string into bytes

Bytvalue = system.text.Encoding.utf8.getbytes (value);

// Calculate the result of the HASH summary to return to bythash

Bythash = mhash.computehash (BytValue);

Mhash.clear ();

/ / Return to the Base64 encoding of the result

Return Convert.TOBASE64String (bythash);

}

The above use of a factory method setHash, the specific content of the replacement is as follows:

Private hashalgorithm sethash (String Hashscription) {

Hashalgorithm Rethash;

Switch (hashscription) {

Case "sha1": {

Rethash = new sha1cryptoserviceProvider (); Break;

}

Case "MD5": {

Rethash = new md5cryptoServiceProvider ();

Break;

}

DEFAULT: {

Rethash = new sha1cryptoserviceProvider ();

Break;

}

}

Return Rethash;

}

Because we define a global object Mhash so we can directly assign a value directly in the HashString method. This factory method allows us to get a Hashalgorithm object, which will delay the object instantiated time, so that the program is more Detailed information on flexibility See (http://www.9cbs.net/develop/read_article.asp?id=21036). Using this factory approach We not only get to SHA1 objects can also get MD5 objects (depending on Subclass). We will determine the instantiated class based on the input description information. If you do not enter, the SHA1 object is not entered.

It can be seen that it is easy to use the .NET framework to achieve the encryption of SHA1 and MD5 (.NET is getting more and more rich, we are getting less and less, is there anything we do? J) above The method has a very deadly weak point, that is, if someone uses an algorithm to exhaust the character, the character encrypted by the same method can not be verified by the system? After all, this is a public encryption method. Solve this issue and some Method, such as we are more than Hash passwords when we are in Hash, we can add some other information to your password, such as: note number, address, etc., so even if the algorithm and password do not know that additional information is still white. Of course If you still want to use the .NET framework, it is easy. Microsoft thought about it, we can use System.Security.cryptography.rngcryptoserviceProvider to do it, it is a random digital generator, of course, in order to allow legal people Login system, we need to deposit these random numbers to the database, as for where there is, of course, is the most inconspicuous place. There is also a way to encrypt the cryptle or again, etc., as long as you can think of it Can.

The above introduction is the Hash encryption algorithm and a simple implementation method. Here, the encryption algorithm that can restore (both two-way Two-Way). (Not complete ...)

Reference: http://msdn.microsoft.com/

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

New Post(0)