| Start |
|---|
| Introduction to Symmetric and Asymmetric Encryption You can improve the security for data transit or storing by implementing encrypting techniques. Basically there are two approaches when using System.Security.Cryptography: symmetric and asymmetric. Symmetric Encryption (opens new window)This method uses a private key in order to perform the data transformation. Pros:
Cons:
Under System.Security.Cryptography you have different classes that perform symmetric encryption, they are known as block ciphers (opens new window):
Asymmetric Encryption (opens new window)This method uses a combination of public and private keys in order to perform the data transformation. Pros:
Cons:
Under System.Security.Cryptography you have access to different classes that perform asymmetric encryption: |
| Password Hashing Passwords should never be stored as plain text! They should be hashed with a randomly generated salt (to defend against rainbow table attacks) using a slow password hashing algorithm. A high number of iterations (> 10k) can be used to slow down brute force attacks. A delay of ~100ms is acceptable to a user logging in, but makes breaking a long password difficult. When choosing a number of iterations you should use the maximum tolerable value for your application and increase it as computer performance improves. You will also need to consider stopping repeated requests which could be used as a DoS attack. When hashing for the first time a salt can be generated for you, the resulting hash and salt can then be stored to a file. Checking an existing users password, read their hash and salt from a file and compare to the hash of the entered password |
| Simple Symmetric File Encryption The following code sample demonstrates a quick and easy means of encrypting and decrypting files using the AES symmetric encryption algorithm. The code randomly generates the Salt and Initialization Vectors each time a file is encrypted, meaning that encrypting the same file with the same password will always lead to different output. The salt and IV are written to the output file so that only the password is required to decrypt it. |
| Cryptographically Secure Random Data There are times when the framework's Random() class may not be considered random enough, given that it is based on a psuedo-random number generator. The framework's Crypto classes do, however, provide something more robust in the form of RNGCryptoServiceProvider. The following code samples demonstrate how to generate Cryptographically Secure byte arrays, strings and numbers. Random Byte Array Random Integer (with even distribution) Random String |
| Fast Asymmetric File Encryption Asymmetric encryption is often regarded as preferable to Symmetric encryption for transferring messages to other parties. This is mainly because it negates many of the risks related to the exchange of a shared key and ensures that whilst anyone with the public key can encrypt a message for the intended recipient, only that recipient can decrypt it. Unfortunately the major down-side of asymmetric encryption algorithms is that they are significantly slower than their symmetric cousins. As such the asymmetric encryption of files, especially large ones, can often be a very computationally intensive process. In order to provide both security AND performance, a hybrid approach can be taken. This entails the cryptographically random generation of a key and initialization vector for Symmetric encryption. These values are then encrypted using an Asymmetric algorithm and written to the output file, before being used to encrypt the source data Symmetrically and appending it to the output. This approach provides a high degree of both performance and security, in that the data is encrypted using a symmetric algorithm (fast) and the key and iv, both randomly generated (secure) are encrypted by an asymmetric algorithm (secure). It also has the added advantage that the same payload encrypted on different occasions will have very different cyphertext, because the symmetric keys are randomly generated each time. The following class demonstrates asymmetric encryption of strings and byte arrays, as well as hybrid file encryption. Example of use: |
Read Article
https://develop.horje.com/learn/1434/reference