Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the data encryption algorithms in asp.net core

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about the data encryption algorithms in asp.net core. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

1. Common symmetric encryption algorithms

Symmetric encryption algorithm, to put it simply, encryption and decryption use the same key for operation. For most encryption algorithms, decryption and encryption are inverse operations. The security of symmetric encryption algorithm depends on the length of the key, and the longer the key is, the more secure it is. Of course, overly long keys are not recommended.

1.1 DES and DESede algorithm

DES algorithm and DESede algorithm (also known as triple DES algorithm) are collectively referred to as DES series algorithms. DES, whose full name is Data Encryption Standard, or data encryption standard, is a block algorithm that uses key encryption. And DESede is to do three times DES encryption for the same piece of data. Instead of saying too much about the principle, let's take a look at how to implement DES encryption / decryption in. Net core.

In the Utils project, create the directory Security:

Under the Security directory, create the DESHelper class:

Namespace Utils.Security

{

Public class DesHelper

{

}

}

Encryption and decryption implementation:

Using System

Using System.IO

Using System.Security.Cryptography

Using System.Text

Namespace Utils.Security

{

Public static class DesHelper

{

Static DesHelper ()

{

DesHandler = DES.Create ("DES")

DesHandler.Key = Convert.FromBase64String ("L1yzjGB2sI4 =")

DesHandler.IV = Convert.FromBase64String ("uEcGI4JSAuY=")

}

Private static DES DesHandler {get;}

/ / /

/ / encryption character

/ / /

/ / /

/ / /

Public static string Encrypt (string source)

{

Try

{

Using (var memStream = new MemoryStream ())

Using (var cryptStream = new CryptoStream (memStream, DesHandler.CreateEncryptor (DesHandler.Key, DesHandler.IV))

CryptoStreamMode.Write))

{

Var bytes = Encoding.UTF8.GetBytes (source)

CryptStream.Write (bytes, 0, bytes.Length)

CryptStream.FlushFinalBlock ()

Return Convert.ToBase64String (memStream.ToArray ())

}

}

Catch (Exception e)

{

Console.WriteLine (e)

Return null

}

}

/ / /

/ / decrypt

/ / /

/ / /

/ / /

Public static string Decrypt (string source)

{

Try

{

Using (var mStream = new MemoryStream (Convert.FromBase64String (source)

Using (var cryptoStream =

New CryptoStream (mStream, DesHandler.CreateDecryptor (DesHandler.Key, DesHandler.IV), CryptoStreamMode.Read))

Using (var reader = new StreamReader (cryptoStream))

{

Return reader.ReadToEnd ()

}

}

Catch (Exception e)

{

Console.WriteLine (e)

Return null

}

}

}

}

Each time DesHandler = DES.Create ("DES") is called, an implementation instance of the DES algorithm is obtained again, so that the values of the Key and IV attributes in the acquired instance also change. If you directly use the data that will be encrypted this time, you will not be able to decrypt it next time. In order to reduce this situation, the Key and IV attributes are manually assigned in the code.

1.2 AES encryption algorithm

AES algorithm (Advanced Encryption Standard) is an advanced data encryption standard algorithm, which is proposed to solve the loopholes in DES algorithm. The core of current AES algorithm is Rijndael algorithm. Of course, you don't have to worry too much about this. Let's just look at how it works:

Similarly, create an AesHelper class in the Security directory:

Namespace Utils.Security

{

Public static class AesHelper

{

}

}

Specific encryption and decryption implementation:

Using System

Using System.IO

Using System.Security.Cryptography

Namespace Utils.Security

{

Public static class AesHelper

{

Static AesHelper ()

{

AesHandler = Aes.Create ()

AesHandler.Key = Convert.FromBase64String ("lB2BxrJdI4UUjK3KEZyQ0obuSgavB1SYJuAFq9oVw0Y=")

AesHandler.IV = Convert.FromBase64String ("6lra6ceX26Fazwj1R4PCOgbread =")

}

Private static Aes AesHandler {get;}

Public static string Encrypt (string source)

{

Using (var mem = new MemoryStream ())

Using (var stream = new CryptoStream (mem, AesHandler.CreateEncryptor (AesHandler.Key, AesHandler.IV))

CryptoStreamMode.Write))

{

Using (var writer = new StreamWriter (stream))

{

Writer.Write (source)

}

Return Convert.ToBase64String (mem.ToArray ())

}

}

Public static string Decrypt (string source)

{

Var data = Convert.FromBase64String (source)

Using (var mem = new MemoryStream (data))

Using (var crypto = new CryptoStream (mem, AesHandler.CreateDecryptor (AesHandler.Key, AesHandler.IV))

CryptoStreamMode.Read))

Using (var reader = new StreamReader (crypto))

{

Return reader.ReadToEnd ()

}

}

}

}

two。 Common asymmetric encryption algorithms

Asymmetric encryption algorithm means that the encryption key and decryption key are not the same. The secret keys of asymmetric encryption algorithms usually appear in pairs, which are divided into public keys and private keys. Public keys can be sent to data interactors in a public form without the risk of disclosure. Because of asymmetric encryption algorithms, private keys cannot be calculated from public keys, and vice versa.

Usually, asymmetric encryption algorithms are encrypted with a public key and decrypted with a private key.

2.1 RSA algorithm

The RSA algorithm is a standard asymmetric encryption algorithm whose first name is derived from the initials of the three inventors. RSA public key cryptosystem is a cryptosystem that uses different encryption keys and decryption keys, and "it is not feasible to derive the decryption key from the known encryption key". Its security depends on the length of the key, and a 1024-bit key is almost impossible to crack.

Similarly, create the RSAHelper class under Utils.Security:

Namespace Utils.Security

{

Public static class RsaHelper

{

}

}

Specific implementation:

Using System

Using System.Security.Cryptography

Namespace Utils.Security

{

Public static class RsaHelper

{

Public static RSAParameters PublicKey {get; private set;}

Public static RSAParameters PrivateKey {get; private set;}

Static RsaHelper ()

{

}

Public static void InitWindows ()

{

Var parameters = new CspParameters ()

{

KeyContainerName = "RSAHELPER" / / default container name for RSA to save the key

}

Var handle = new RSACryptoServiceProvider (parameters)

PublicKey = handle.ExportParameters (false)

PrivateKey = handle.ExportParameters (true)

}

Public static void ExportKeyPair (string publicKeyXmlString, string privateKeyXmlString)

{

Var handle = new RSACryptoServiceProvider ()

Handle.FromXmlString (privateKeyXmlString)

PrivateKey = handle.ExportParameters (true)

Handle.FromXmlString (publicKeyXmlString)

PublicKey = handle.ExportParameters (false)

}

Public static byte [] Encrypt (byte [] dataToEncrypt)

{

Try

{

Byte [] encryptedData

Using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider ())

{

RSA.ImportParameters (PublicKey)

EncryptedData = RSA.Encrypt (dataToEncrypt, true)

}

Return encryptedData

}

Catch (CryptographicException e)

{

Console.WriteLine (e.Message)

Return null

}

}

Public static byte [] Decrypt (byte [] dataToDecrypt)

{

Try

{

Byte [] decryptedData

Using (var rsa = new RSACryptoServiceProvider ())

{

Rsa.ImportParameters (PrivateKey)

DecryptedData = rsa.Decrypt (dataToDecrypt, true)

}

Return decryptedData

}

Catch (CryptographicException e)

{

Console.WriteLine (e.ToString ())

Return null

}

}

}

}

Because of the particularity of RSA, the public key and private key need to be set in advance. C# supports multiple ways to import keys, so I won't talk too much about it here.

3. Information summary algorithm

This algorithm is not strictly an encryption algorithm because it is completely irreversible. That is, once encrypted using this type of algorithm, the data cannot be decrypted and restored. Of course, it is precisely because this feature is often used to save passwords. Because this can prevent some people from getting the database and code, they can simply reverse the user's password.

3.1 MD5 algorithm

The most commonly used information digest algorithm is the MD5 encryption algorithm, MD5 Information Digest algorithm (English: MD5 Message-Digest Algorithm), a widely used cryptographic hash function that produces a 128bit (16-byte) hash value (hash value) to ensure complete and consistent information transmission.

If the principle is not explained, let's take a look at how to implement it. As usual, create a MD5Helper under Security:

Namespace Utils.Security

{

Public static class Md5Helper

{

}

}

Specific implementation:

Using System.Security.Cryptography

Using System.Text

Namespace Utils.Security

{

Public static class Md5Helper

{

Private static MD5 Hanlder {get;} = new MD5CryptoServiceProvider ()

Public static string GetMd5Str (string source)

{

Var data = Encoding.UTF8.GetBytes (source)

Var security = Hanlder.ComputeHash (data)

Var sb = new StringBuilder ()

Foreach (var b in security)

{

Sb.Append (b.ToString ("X2"))

}

Return sb.ToString ()

}

}

}

These are the data encryption algorithms in asp.net core, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report