What is Cryptography?
Cryptography is the process of securing data through encryption, hashing, digital signatures. It helps protect communication messages transmitted over non-secure networks, ensuring that only authorized parties can read the information.
Encryption and Decryption
Encryption is the process of converting plaintext data into ciphertext, which appears random and meaningless to anyone who does not have the key. Decryption is the reverse process — converting ciphertext back into readable plaintext.
The strength of encryption depends on several factors, including the algorithm used and the length of the encryption key. Generally, the longer the key, the harder it is to decrypt the ciphertext without authorization.
When choosing an encryption algorithm, it’s best to select one that has been thoroughly tested and widely used over time, as new or unproven algorithms may have hidden weaknesses.
Cryptographic Keys
At the heart of every cryptographic operation lies a cryptographic key, which is essential for both encryption and decryption. Without the correct key, the encrypted message remains unreadable. There are 4 types of keys:
- Symmetric key: Same key used for both encryption and decryption (e.g., AES, DES). It is an efficent way to encrypt large amount of data but possess a security risk when sharing the same key between sender and receiver.
- Asymmetric key: Uses a public key to encrypt and a private key to decrypt (e.g., RSA).
- Session key: A temporary, symmetric key used for a single session, and often in secure communication between two parties, such as browser and server
- Hash key: Used in HMACs to verify data integrity, not for encryption/decryption.
Data Protection in .NET
One practical way to encrypt sensitive data in software development is by using the Data Protection API in .NET. The Data Protection system provides a set of cryptographic APIs designed to protect application data easily and securely.
It uses symmetric key encryption under the hood, where the key is randomly generated and automatically managed by the system, so that developers don't have to manage secret keys manually.
To use data protection, there're 3 steps:
1. Create a data protector from data protection provider
A purpose string is needed. It defines the intended use of a data protector, and differentiate among consumers. For example, a protector with purpose string as 'red' won't be able to decrypt data with purpose string as 'blue'.
public class SecureData
{
IDataProtector _protector;
public SecureData(IDataProtectionProvider provider)
{
_protector = provider.CreateProtector("purpose string here");
}
2. Call Protect method to encrypt your data
public void EncryptData()
{
string encryptedResult = _protector.Protect(input);
}
3. Call Unprotect to decrypt data
public void DecryptData()
{
string decryptedData = _protector.Unprotect(encryptedResult );
}
IDataProtectionProvider is an interface that is needed to create a data protector (IDataProtector) by calling CreateProtector(). Then IDataProtector is used to have access to Protect and Unprotect method.
Source:
https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/using-data-protection?view=aspnetcore-9.0
https://www.c-sharpcorner.com/article/encrypting-sensitive-data-in-asp-net-core-applications/
Comments
Post a Comment