Data Encryption - Decryption [Unity3d - C#]
- In cryptography, encryption is the process of encoding information. This process converts the original representation of the information, known as plaintext, into an alternative form known as ciphertext. Ideally, only authorized parties can decipher a ciphertext back to plaintext and access the original information.
What is
Decryption?
- The conversion of encrypted data into its original form is called Decryption. It is generally a reverse process of encryption. It decodes the encrypted information so that an authorized user can only decrypt the data because decryption requires a secret key or password.
There are
many techniques and algorithms for data Encryption and Decryption in Unity(C#).
Here Describe
4 most popular algorithms to Encrypt or Decrypt process in C#.
1) AES
2) DES
3) MD5
4) XOR
Download Unity Demo Project:
https://github.com/sudhirkotila/Data-Security-Unity-Encryption-Decryption-Demo
1) AES:
- AES
– Advanced Encryption Standard.
- This is more popular with a strong security level
encryption-decryption data algorithm.
- The Advanced Encryption Standard, also known by its
original name Rijndael is a specification for the encryption of electronic
data established by the U.S. National Institute of Standards and Technology in
2001.
- It is found
at least six times faster than triple DES.
Example:
string iv = "1234567887654321"; //set any string of 16 chars
//AES - Encription
public string AESEncryption(string inputData)
AesCryptoServiceProvider AEScryptoProvider = new AesCryptoServiceProvider();
byte[] txtByteData = ASCIIEncoding.ASCII.GetBytes(inputData);
byte[] result = trnsfrm.TransformFinalBlock(txtByteData, 0, txtByteData.Length);
//AES - Decryption
public string AESDecryption(string inputData)
AesCryptoServiceProvider AEScryptoProvider = new AesCryptoServiceProvider();
byte[] txtByteData = Convert.FromBase64String(inputData);
byte[] result = trnsfrm.TransformFinalBlock(txtByteData, 0, txtByteData.Length);
Input: This is AES Demo
Output: Pwsx645kDk6jTLZOOLjcYzRn//JwVdPu9zqH07rdUEY=
2) DES:
- DES – Data Encryption Standard
- The Data Encryption Standard (DES) is a
symmetric-key block cipher published by the National Institute of Standards and
Technology (NIST).
- DES is a symmetric-key algorithm for the encryption
of digital data. Although its short key length of 56 bits makes it too insecure
for applications, it has been highly influential in the advancement of
cryptography.
- For
more info: https://en.wikipedia.org/wiki/Data_Encryption_Standard
Example:
//DES - Encription
public string DESEncryption(string inputData)
byte[] txtByteData = ASCIIEncoding.ASCII.GetBytes(inputData);
DESCryptoServiceProvider DEScryptoProvider = new DESCryptoServiceProvider();
//Set up Stream & Write Encript data
MemoryStream mStream = new MemoryStream();
//Read Ecncrypted Data From Memory Stream
byte[] result = new byte[mStream.Length];
return Convert.ToBase64String(result);
//DES - Decryption
public string DESDecryption(string inputData)
byte[] txtByteData = Convert.FromBase64String(inputData);
DESCryptoServiceProvider DEScryptoProvider = new DESCryptoServiceProvider();
//Set up Stream & Write Encript data
MemoryStream mStream = new MemoryStream();
//Read Ecncrypted Data From Memory Stream
byte[] result = new byte[mStream.Length];
return ASCIIEncoding.ASCII.GetString(result);
Input: This is DES Demo
Output: sUCnq2SAMpO7racUGDO85ovr9i5cFI4h
3) MD5:
- The MD5 message-digest algorithm is a widely used hash
function producing a 128-bit hash value.
- Although
MD5 was initially designed to be used as a cryptographic hash function, it has
been found to suffer from extensive vulnerabilities
For more info: https://en.wikipedia.org/wiki/MD5
Example:
string hasKey = "Password@SudhirKotila12:34"; //You can use any string here as haskey
byte[] bData = UTF8Encoding.UTF8.GetBytes(inputData);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
tripalDES.Key = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hasKey));
ICryptoTransform trnsfrm = tripalDES.CreateEncryptor();
return Convert.ToBase64String(result);
//MD5 - Decryption
public string MD5Decryption(string inputData)
string hasKey = "Password@SudhirKotila12:34"; //You can use any string here as haskey
byte[] bData = Convert.FromBase64String(inputData);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
tripalDES.Key = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hasKey));
ICryptoTransform trnsfrm = tripalDES.CreateDecryptor();
return UTF8Encoding.UTF8.GetString(result);
Input: This is MD5 Demo
Output: VYnMMSoeuy3jLwl3d6otyX33NBd7NibG
4) XOR:
- XOR
Encryption is an encryption method used to encrypt data and is hard to crack by
brute-force method, i.e generating encryption keys to match with the correct
one
- This
operation is sometimes called modulus 2 addition (or subtraction) process.
- The Logic
is, a string of text can be encrypted by applying the bitwise XOR operator to
every character using a given key. To decrypt the output, merely reapplying the
XOR function with the key will remove the cipher.
For More
info: https://en.wikipedia.org/wiki/XOR_cipher
Example:
public string XOREncryptDecrypt(string inputData)
StringBuilder outSB = new StringBuilder(inputData.Length);
return outSB.ToString();
Input: This is XOR Demo
Output : ҆ҺһҡӲһҡӲҊҝҀӲҖҷҿҽ
Download Unity Demo Project: https://drive.google.com/drive/folders/1oedqWl7skJO88UeQnZVnik7_l-i6vGhn?usp=sharing
Nice. Exactly what I was needing
ReplyDeleteThanks for your feedback
DeleteThanks lot dude Now I understand how to use enyction
ReplyDeleteThank you so much for reading this article.
DeleteHello,
ReplyDeleteI use your encrypt/decrypt "MD5" in my application.
I have also inserted the address of this blog, inside the ".cs" script.
I would need your help to decrypt the "C#" MD5 in a ".php" file please.
Keep it up awesome!
This comment has been removed by the author.
ReplyDeleteMD5 is a hashing algorithm, not an encryption algorithm. Use SHA-512 or Bcrypt instead of MD5 (although bcrypt is best used for passwords).
ReplyDeleteDES is old and shouldn't be used
You're best off using AES-256 for your encryption.
*You cannot decrypt hashes.
Delete