Encryption with C#: Symmetric, Asymmetric and Hash Encryption
To keep our data secure, we feel the need to encrypt our code or our data. Let's look at how they are used by working through an example of each: the algorithms used when we want to get back to the encrypted data by reversing it, and the well-known algorithms that cannot be reversed.
.Net comes with many encryption libraries ready to use. These encryption classes are located in the System.Security.Cryptography library.
First of all, it is useful to classify the encryption methods; there are currently 3 different encryption methods in the world. These are
- Hash Encryption Methods
- MD5
- SHA1
- SHA256
- SHA384
- SHA512
- Symmetric Encryption Methods
- DES
- Triple DES
- RC2
- Rijndael
- Asymmetric Encryption Methods
- DSA
- RSA
Hash Encryption Methods
It is not possible to get back to the encrypted data. Suppose you encrypted 1000 characters of data with md5, sha 1, sha256 or sha512; in that case the encrypted data you get will be a string of a standard size depending on the type of algorithm. Naturally, a question like this will come up: "if I cannot get back to the data I encrypted, why should I encrypt it". The answer to this question is very simple: in some of the applications we build, we do not need to see the unencrypted data. For example, say you have opened a forum and your users will sign up to the system to enter the forum. This is where, acting as an honest programmer, it is more correct to encrypt the password the user enters with a hash algorithm and store it in our database. That way, during login, we can encrypt the data the user enters again and compare it with the encrypted data in the database. So we do not see the user's password and thereby respect the user's private information, and we also ensure data security.
public static byte[] ByteDonustur(string deger)
{
UnicodeEncoding ByteConverter = new UnicodeEncoding();
return ByteConverter.GetBytes(deger);
}
public static byte[] Byte8(string deger)
{
char[] arrayChar = deger.ToCharArray();
byte[] arrayByte = new byte[arrayChar.Length];
for (int i = 0; i < arrayByte.Length; i++)
{
arrayByte[i] = Convert.ToByte(arrayChar[i]);
}
return arrayByte;
}
MD5 Encryption
public string MD5(string strGiris)
{
if (strGiris == "" || strGiris==null)
{
throw new ArgumentNullException("No data to encrypt");
}
else
{
MD5CryptoServiceProvider sifre = new MD5CryptoServiceProvider();
byte[] arySifre = ByteDonustur(strGiris);
byte[] aryHash = sifre.ComputeHash(arySifre);
return BitConverter.ToString(aryHash);
}
}
SHA1 Encryption
public string SHA1(string strGiris)
{
if (strGiris == "" || strGiris == null)
{
throw new ArgumentNullException("No data to encrypt.");
}else
{
SHA1CryptoServiceProvider sifre = new SHA1CryptoServiceProvider();
byte[] arySifre = ByteDonustur(strGiris);
byte[] aryHash = sifre.ComputeHash(arySifre);
return BitConverter.ToString(aryHash);
}
}
SHA256 Encryption
public string SHA256(string strGiris)
{
if (strGiris == "" || strGiris == null)
{
throw new ArgumentNullException("No Data To Encrypt");
}else
{
SHA256Managed sifre = new SHA256Managed();
byte[] arySifre = ByteDonustur(strGiris);
byte[] aryHash = sifre.ComputeHash(arySifre);
return BitConverter.ToString(aryHash);
}
}
SHA384 Encryption
public string SHA384(string strGiris)
{
if (strGiris == "" || strGiris == null)
{
throw new ArgumentNullException("No data to encrypt was found.");
}else
{
SHA384Managed sifre = new SHA384Managed();
byte[] arySifre = ByteDonustur(strGiris);
byte[] aryHash = sifre.ComputeHash(arySifre);
return BitConverter.ToString(aryHash);
}
}
SHA512 Encryption
public string SHA512(string strGiris)
{
if (strGiris == "" || strGiris == null)
{
throw new ArgumentNullException("No data to encrypt.");
}
else
{
SHA512Managed sifre = new SHA512Managed();
byte[] arySifre = ByteDonustur(strGiris);
byte[] aryHash = sifre.ComputeHash(arySifre);
return BitConverter.ToString(aryHash);
}
}
Symmetric Encryption
In symmetric encryption methods, to get to the encrypted data, the party decrypting it needs the same key as the party that encrypted the data.
public static byte[] ByteDonustur(string deger)
{
UnicodeEncoding ByteConverter = new UnicodeEncoding();
return ByteConverter.GetBytes(deger);
}
public static byte[] Byte8(string deger)
{
char[] arrayChar = deger.ToCharArray();
byte[] arrayByte = new byte[arrayChar.Length];
for (int i = 0; i < arrayByte.Length; i++)
{
arrayByte[i] = Convert.ToByte(arrayChar[i]);
}
return arrayByte;
}
DES Encryption Method with C#
public string DESSifrele(string strGiris)
{
string sonuc = "";
if (strGiris == "" || strGiris == null)
{
throw new ArgumentNullException("No data to encrypt");
}
else
{
byte[] aryKey = Byte8("12345678"); // ENTER AN 8 bit string VALUE HERE
byte[] aryIV = Byte8("12345678"); // ENTER AN 8 bit string VALUE HERE
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateEncryptor(aryKey, aryIV), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cs);
writer.Write(strGiris);
writer.Flush();
cs.FlushFinalBlock();
writer.Flush();
sonuc = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
writer.Dispose();
cs.Dispose();
ms.Dispose();
}
return sonuc;
}
public string DESCoz(string strGiris)
{
string strSonuc = "";
if (strGiris == "" || strGiris == null)
{
throw new ArgumentNullException("No data to encrypt.");
}
else
{
byte[] aryKey = Byte8("12345678");
byte[] aryIV = Byte8("12345678");
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(Convert.FromBase64String(strGiris));
CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateDecryptor(aryKey, aryIV), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cs);
strSonuc = reader.ReadToEnd();
reader.Dispose();
cs.Dispose();
ms.Dispose();
}
return strSonuc;
}
TripleDES Encryption Method with C#
The TripleDES algorithm uses a 24 bit key and an 8 bit iv value.
public string Sifrele(string strGiris)
{
string sonuc = "";
if (strGiris == "" || strGiris == null)
{
throw new ArgumentNullException("No data to encrypt.");
}
else
{
byte[] aryKey = Byte8("123456781234567812345678");
byte[] aryIV = Byte8("12345678");
TripleDESCryptoServiceProvider dec = new TripleDESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, dec.CreateEncryptor(aryKey, aryIV), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cs);
writer.Write(strGiris);
writer.Flush();
cs.FlushFinalBlock();
writer.Flush();
sonuc = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
writer.Dispose();
cs.Dispose();
ms.Dispose();
}
return sonuc;
}
public string Coz(string strGiris)
{
string strSonuc = "";
if (strGiris == "" || strGiris == null)
{
throw new ArgumentNullException("No data to encrypt.");
}
else
{
byte[] aryKey = Byte8("123456781234567812345678");
byte[] aryIV = Byte8("12345678");
TripleDESCryptoServiceProvider cryptoProvider = new TripleDESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(Convert.FromBase64String(strGiris));
CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateDecryptor(aryKey, aryIV), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cs);
strSonuc = reader.ReadToEnd();
reader.Dispose();
cs.Dispose();
ms.Dispose();
}
return strSonuc;
}
RC2 Encryption Method with C#
The RC2 algorithm uses an 8 bit key and an 8 bit iv value.
public string Sifrele(string strGiris)
{
string sonuc = "";
if (strGiris == "" || strGiris == null)
{
throw new ArgumentNullException("No data to encrypt.");
}
else
{
byte[] aryKey = Byte8("12345678");
byte[] aryIV = Byte8("12345678");
RC2CryptoServiceProvider dec = new RC2CryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, dec.CreateEncryptor(aryKey, aryIV), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cs);
writer.Write(strGiris);
writer.Flush();
cs.FlushFinalBlock();
writer.Flush();
sonuc = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
writer.Dispose();
cs.Dispose();
ms.Dispose();
}
return sonuc;
}
public string Coz(string strGiris)
{
string strSonuc = "";
if (strGiris == "" || strGiris == null)
{
throw new ArgumentNullException("No data to decrypt.");
}
else
{
byte[] aryKey = Byte8("12345678");
byte[] aryIV = Byte8("12345678");
RC2CryptoServiceProvider cp = new RC2CryptoServiceProvider();
MemoryStream ms = new MemoryStream(Convert.FromBase64String(strGiris));
CryptoStream cs = new CryptoStream(ms, cp.CreateDecryptor(aryKey, aryIV), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cs);
strSonuc = reader.ReadToEnd();
reader.Dispose();
cs.Dispose();
ms.Dispose();
}
return strSonuc;
}
Rijndael Encryption Method with C#
The Rijndael algorithm uses an 8 key and a 16 bit iv value.
public string Sifrele(string strGiris)
{
string sonuc = "";
if (strGiris == "" || strGiris == null)
{
throw new ArgumentNullException("No data to encrypt.");
}
else
{
byte[] aryKey = Byte8("12345678");
byte[] aryIV = Byte8("1234567812345678");
RijndaelManaged dec = new RijndaelManaged();
dec.Mode = CipherMode.CBC;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, dec.CreateEncryptor(aryKey, aryIV), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cs);
writer.Write(strGiris);
writer.Flush();
cs.FlushFinalBlock();
writer.Flush();
sonuc = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
writer.Dispose();
cs.Dispose();
ms.Dispose();
}
return sonuc;
}
public string Coz(string strGiris)
{
string strSonuc = "";
if (strGiris == "" || strGiris == null)
{
throw new ArgumentNullException("No data to decrypt.");
}
else
{
byte[] aryKey = Byte8("12345678");
byte[] aryIV = Byte8("1234567812345678");
RijndaelManaged cp = new RijndaelManaged();
MemoryStream ms = new MemoryStream(Convert.FromBase64String(strGiris));
CryptoStream cs = new CryptoStream(ms, cp.CreateDecryptor(aryKey, aryIV), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cs);
strSonuc = reader.ReadToEnd();
reader.Dispose();
cs.Dispose();
ms.Dispose();
}
return strSonuc;
}
Asymmetric Encryption
Asymmetric encryption methods are advanced encryption methods. In this type of algorithm, the party encrypting the data and the party that will decrypt it use different keys.
public static byte[] ByteDonustur(string deger)
{
UnicodeEncoding ByteConverter = new UnicodeEncoding();
return ByteConverter.GetBytes(deger);
}
public static byte[] Byte8(string deger)
{
char[] arrayChar = deger.ToCharArray();
byte[] arrayByte = new byte[arrayChar.Length];
for (int i = 0; i < arrayByte.Length; i++)
{
arrayByte[i] = Convert.ToByte(arrayChar[i]);
}
return arrayByte;
}
RSA Encryption Method with C#
public string Sifrele(string strGiris, out RSAParameters prm)
{
string strSonuc = "";
if (strGiris == "")
{
throw new ArgumentNullException("No data to encrypt.");
}
else
{
byte[] aryDizi = ByteDonustur(strGiris);
RSACryptoServiceProvider dec = new RSACryptoServiceProvider();
prm = dec.ExportParameters(true);
byte[] aryDonus = dec.Encrypt(aryDizi, false);
strSonuc = Convert.ToBase64String(aryDonus);
}
return strSonuc;
}
public string Coz(string strGiris, RSAParameters prm)
{
string strSonuc = "";
if (strGiris == "" || strGiris == null)
{
throw new ArgumentNullException("No record to decrypt");
}
else
{
RSACryptoServiceProvider dec = new RSACryptoServiceProvider();
byte[] aryDizi = Convert.FromBase64String(strGiris);
UnicodeEncoding UE = new UnicodeEncoding();
dec.ImportParameters(prm);
byte[] aryDonus = dec.Decrypt(aryDizi, false);
strSonuc = UE.GetString(aryDonus);
}
return strSonuc;
If you like, you can use these encryption methods to achieve stronger encryption, or you can create your own encryption algorithm , the choice is yours...
C#,Hash Şifreleme,MD5,SHA1,SHA256,SHA384,SHA512,Asimetrik Şifreleme,RSA,Simetrik Şifreleme,Kripto,Şifreleme,Algoritma,DES,RC2,Rijndael,TripleDES