diff --git a/Encryption/Class1.cs b/Encryption/Class1.cs deleted file mode 100644 index 158f09b..0000000 --- a/Encryption/Class1.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Encryption -{ - public class Class1 - { - } -} diff --git a/Encryption/Crypto.cs b/Encryption/Crypto.cs new file mode 100644 index 0000000..085f08f --- /dev/null +++ b/Encryption/Crypto.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Security.Cryptography; +using System.Text; + +namespace Encryption +{ + /// + /// reference: https://msdn.microsoft.com/en-gb/library/system.security.cryptography.tripledescryptoserviceprovider(v=vs.110).aspx + /// + public class Crypto + { + public List GenerateKeyAndIV() + { + TripleDESCryptoServiceProvider tDES = new TripleDESCryptoServiceProvider(); + + return new List { tDES.Key, tDES.IV }; + } + + public string Encrypt(string s, byte[] key, byte[] iv) + { + try + { + MemoryStream mStream = new MemoryStream(); + CryptoStream cStream = new CryptoStream(mStream, new TripleDESCryptoServiceProvider().CreateEncryptor(key, iv), CryptoStreamMode.Write); + byte[] toEncrypt = new UTF8Encoding().GetBytes(s); + + cStream.Write(toEncrypt, 0, toEncrypt.Length); + cStream.FlushFinalBlock(); + + byte[] bytesEncrypted = mStream.ToArray(); + + cStream.Close(); + mStream.Close(); + + return Convert.ToBase64String(bytesEncrypted, 0, bytesEncrypted.Length); + } + catch (CryptographicException e) + { + Console.WriteLine("Error: {0}", e.Message); + return null; + } + } + + public string Decrypt(string s, byte[] key, byte[] iv) + { + try + { + byte[] bytesEncrypted = Convert.FromBase64String(s); + MemoryStream mStream = new MemoryStream(bytesEncrypted); + CryptoStream cStream = new CryptoStream(mStream, new TripleDESCryptoServiceProvider().CreateDecryptor(key, iv), CryptoStreamMode.Read); + byte[] fromEncrypt = new byte[s.Length]; + + cStream.Read(fromEncrypt, 0, fromEncrypt.Length); + + return new UTF8Encoding().GetString(fromEncrypt); + } + catch (CryptographicException e) + { + Console.WriteLine("Error: {0}", e.Message); + return null; + } + } + + } +} diff --git a/Encryption/Encryption.csproj b/Encryption/Encryption.csproj index 9428211..da27bbb 100644 --- a/Encryption/Encryption.csproj +++ b/Encryption/Encryption.csproj @@ -1,10 +1,10 @@ - + Debug AnyCPU - ed031a70-570f-4599-ac04-0453a997a6a4 + {ED031A70-570F-4599-AC04-0453A997A6A4} Library Properties Encryption @@ -30,24 +30,13 @@ 4 - - - - - - - - - - - - - - + + + - + - + \ No newline at end of file