# 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**

This method uses a private key in order to perform the data transformation.

Pros:

- Symmetric algorithms consume less resources and are faster than asymmetric ones.
- The amount of data you can encrypt is unlimited.

Cons:

- Encryption and decryption use the same key. Someone will be able to decrypt your data if the key is compromised.
- You could end up with many different secret keys to manage if you choose to use a different secret key for different data.

Under System.Security.Cryptography you have different classes that perform symmetric encryption, they are known as block ciphers:

- AesManaged (AES algorithm).
- AesCryptoServiceProvider (AES algorithm FIPS 140-2 complaint).
- DESCryptoServiceProvider (DES algorithm).
- RC2CryptoServiceProvider (Rivest Cipher 2 algorithm).
- RijndaelManaged (AES algorithm).
*Note*: RijndaelManaged is**not**FIPS-197 complaint. - TripleDES (TripleDES algorithm).

**Asymmetric Encryption**

This method uses a combination of public and private keys in order to perform the data transformation.

Pros:

- It uses larger keys than symmetric algorithms, thus they are less susceptible to being cracked by using brute force.
- It is easier to guarantee who is able to encrypt and decrypt the data because it relies on two keys (public and private).

Cons:

- There is a limit on the amount of data that you can encrypt. The limit is different for each algorithm and is typically proportional with the key size of the algorithm. For example, an RSACryptoServiceProvider object with a key length of 1,024 bits can only encrypt a message that is smaller than 128 bytes.
- Asymmetric algorithms are very slow in comparison to symmetric algorithms.

Under System.Security.Cryptography you have access to different classes that perform asymmetric encryption:

- DSACryptoServiceProvider (Digital Signature Algorithm algorithm)
- RSACryptoServiceProvider (RSA Algorithm algorithm)

Introduction to Symmetric and Asymmetric Encryption

Table Of Contents

2
Literals

18
Regex

19
DateTime

20
Arrays

22
Enum

23
Tuples

25
GUID

26
BigInteger

28
Looping

29
Iterators

30
IEnumerable

35
Dynamic type

37
Casting

41
Interfaces

47
Methods

52
Keywords

53
Recursion

57
Inheritance

58
Generics

62
Reflection

65
LINQ Queries

66
LINQ to XML

68
XmlDocument

69
XDocument

79
Diagnostics

80
Overflow

86
Properties

89
Events

93
Structs

94
Attributes

95
Delegates

97
Networking

102
Action Filters

103
Polymorphism

104
Immutability

105
Indexer

107
Stream

108
Timers

109
Stopwatches

110
Threading

112
Async Await

114
BackgroundWorker

117
Lock Statement

118
Yield Keyword

121
Func delegates

124
ICloneable

125
IComparable

127
Using SQLite

128
Caching

129
Code Contracts

136
Pointers

144
Hash Functions

146
Cryptography

148
C# Script

149
Runtime Compile

150
Interoperability

156
Contributors