Advertisement
Guest User

Hybrid Encrypt

a guest
Mar 27th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.49 KB | None | 0 0
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. using System.IO;
  5.  
  6. namespace EnDe
  7. {
  8.     class MainClass
  9.     {
  10.         public static void Main (string[] args)
  11.         {
  12.             string plainText = "Indonesia merdeka";
  13.             string password = "hfakdsjhfasdfjasdkfhkjadshg";
  14.  
  15.             // Enkripsi
  16.             byte[] encrypted = HybridEncrypt(plainText, password);
  17.             // Dekripsi
  18.             string decrypted = HybridDecrypt(encrypted, password);
  19.  
  20.             Console.WriteLine (decrypted);
  21.         }
  22.  
  23.         public static byte[] HybridEncrypt(string plainText, string password)
  24.         {
  25.             string desEncrypted = DESEncrypt (plainText, password);
  26.             byte[] finalEncrypted = AESEncrypt (desEncrypted, password);
  27.  
  28.             return finalEncrypted;
  29.         }
  30.  
  31.         public static string HybridDecrypt(byte[] encryptedBytes, string password)
  32.         {
  33.             string aesDecrypted = AESDecrypt (encryptedBytes, password);
  34.             string finalDecrypted = DESDecrypt (aesDecrypted, password);
  35.  
  36.             return finalDecrypted;
  37.         }
  38.  
  39.         public static string DESEncrypt(string originalString, string key)
  40.         {
  41.             byte[] bytes = CreateDESKey (key);
  42.             DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
  43.             MemoryStream memoryStream = new MemoryStream();
  44.             CryptoStream cryptoStream = new CryptoStream(memoryStream,
  45.                 cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
  46.             StreamWriter writer = new StreamWriter(cryptoStream);
  47.             writer.Write(originalString);
  48.             writer.Flush();
  49.             cryptoStream.FlushFinalBlock();
  50.             writer.Flush();
  51.             return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
  52.         }
  53.  
  54.         public static string DESDecrypt(string cryptedString, string key)
  55.         {
  56.             DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
  57.             MemoryStream memoryStream = new MemoryStream
  58.                 (Convert.FromBase64String(cryptedString));
  59.             byte[] bytes = CreateDESKey (key);
  60.             CryptoStream cryptoStream = new CryptoStream(memoryStream,
  61.                 cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
  62.             StreamReader reader = new StreamReader(cryptoStream);
  63.             return reader.ReadToEnd();
  64.         }
  65.  
  66.         private static byte[] AESEncrypt(string originalString, string password)
  67.         {
  68.             System.Text.UTF8Encoding Byte_Transform = new System.Text.UTF8Encoding();
  69.             RijndaelManaged algo = new RijndaelManaged ();
  70.  
  71.             algo.Key = CreateKey (password);
  72.             algo.IV = CreateIV (password);
  73.  
  74.             MemoryStream memoryStream = new MemoryStream ();
  75.             ICryptoTransform encryptor = algo.CreateEncryptor (algo.Key, algo.IV);
  76.             CryptoStream cryptoStream = new CryptoStream (memoryStream, encryptor, CryptoStreamMode.Write);
  77.  
  78.             byte[] PlainBytes = Byte_Transform.GetBytes(originalString);
  79.  
  80.             cryptoStream.Write (PlainBytes, 0, PlainBytes.Length);
  81.             algo.Clear ();
  82.             cryptoStream.Close ();
  83.  
  84.             return memoryStream.ToArray ();
  85.         }
  86.  
  87.         private static string AESDecrypt(byte[] encryptedBytes, string password)
  88.         {
  89.             RijndaelManaged algo = new RijndaelManaged ();
  90.             algo.Key = CreateKey(password);
  91.             algo.IV = CreateIV(password);
  92.             MemoryStream memoryStream = new MemoryStream (encryptedBytes);
  93.             ICryptoTransform decryptor = algo.CreateDecryptor (algo.Key, algo.IV);
  94.             CryptoStream cryptoStream = new CryptoStream (memoryStream, decryptor, CryptoStreamMode.Read);
  95.  
  96.             StreamReader streamReader = new StreamReader (cryptoStream);
  97.             String plainText = streamReader.ReadToEnd ();
  98.  
  99.             algo.Clear ();
  100.             memoryStream.Flush ();
  101.             memoryStream.Close ();
  102.  
  103.             return plainText;
  104.         }
  105.  
  106.         private static byte[] CreateDESKey(string strPassword)
  107.         {
  108.             char[] chrData = strPassword.ToCharArray();
  109.             int intLength = chrData.GetUpperBound(0);
  110.             byte[] bytDataToHash = new byte[intLength + 1];
  111.  
  112.             for (int i = 0; i <= chrData.GetUpperBound(0); i++)
  113.             {
  114.                 bytDataToHash[i] = Convert.ToByte(chrData[i]);
  115.             }
  116.  
  117.             System.Security.Cryptography.SHA512Managed SHA512 = new System.Security.Cryptography.SHA512Managed();
  118.             byte[] bytResult = SHA512.ComputeHash(bytDataToHash);
  119.             byte[] bytKey = new byte[8];
  120.  
  121.             for (int i = 0; i <= 7; i++)
  122.             {
  123.                 bytKey[i] = bytResult[i];
  124.             }
  125.  
  126.             return bytKey;
  127.         }
  128.  
  129.         private static byte[] CreateKey(string strPassword)
  130.         {
  131.             //Convert strPassword to an array and store in chrData.
  132.             char[] chrData = strPassword.ToCharArray();
  133.             //Use intLength to get strPassword size.
  134.             int intLength = chrData.GetUpperBound(0);
  135.             //Declare bytDataToHash and make it the same size as chrData.
  136.             byte[] bytDataToHash = new byte[intLength + 1];
  137.  
  138.             //Use For Next to convert and store chrData into bytDataToHash.
  139.             for (int i = 0; i <= chrData.GetUpperBound(0); i++)
  140.             {
  141.                 bytDataToHash[i] = Convert.ToByte(chrData[i]);
  142.             }
  143.  
  144.             //Declare what hash to use.
  145.             System.Security.Cryptography.SHA512Managed SHA512 = new System.Security.Cryptography.SHA512Managed();
  146.             //Declare bytResult, Hash bytDataToHash and store it in bytResult.
  147.             byte[] bytResult = SHA512.ComputeHash(bytDataToHash);
  148.             //Declare bytKey(31).  It will hold 256 bits.
  149.             byte[] bytKey = new byte[32];
  150.  
  151.             //Use For Next to put a specific size (256 bits) of
  152.             //bytResult into bytKey. The 0 To 31 will put the first 256 bits
  153.             //of 512 bits into bytKey.
  154.             for (int i = 0; i <= 31; i++)
  155.             {
  156.                 bytKey[i] = bytResult[i];
  157.             }
  158.  
  159.             return bytKey;
  160.             //Return the key.
  161.         }
  162.  
  163.         private static byte[] CreateIV(string strPassword)
  164.         {
  165.             //Convert strPassword to an array and store in chrData.
  166.             char[] chrData = strPassword.ToCharArray();
  167.             //Use intLength to get strPassword size.
  168.             int intLength = chrData.GetUpperBound(0);
  169.             //Declare bytDataToHash and make it the same size as chrData.
  170.             byte[] bytDataToHash = new byte[intLength + 1];
  171.  
  172.             //Use For Next to convert and store chrData into bytDataToHash.
  173.             for (int i = 0; i <= chrData.GetUpperBound(0); i++)
  174.             {
  175.                 bytDataToHash[i] = Convert.ToByte(chrData[i]);
  176.             }
  177.  
  178.             //Declare what hash to use.
  179.             System.Security.Cryptography.SHA512Managed SHA512 = new System.Security.Cryptography.SHA512Managed();
  180.             //Declare bytResult, Hash bytDataToHash and store it in bytResult.
  181.             byte[] bytResult = SHA512.ComputeHash(bytDataToHash);
  182.             //Declare bytIV(15).  It will hold 128 bits.
  183.             byte[] bytIV = new byte[16];
  184.  
  185.             //Use For Next to put a specific size (128 bits) of
  186.             //bytResult into bytIV. The 0 To 30 for bytKey used the first 256 bits.
  187.             //of the hashed password. The 32 To 47 will put the next 128 bits into bytIV.
  188.             for (int i = 32; i <= 47; i++)
  189.             {
  190.                 bytIV[i - 32] = bytResult[i];
  191.             }
  192.  
  193.             return bytIV;
  194.             //return the IV
  195.         }
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement