Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Security.Cryptography;
- using System.Text;
- using System.IO;
- namespace EnDe
- {
- class MainClass
- {
- public static void Main (string[] args)
- {
- string plainText = "Indonesia merdeka";
- string password = "hfakdsjhfasdfjasdkfhkjadshg";
- // Enkripsi
- byte[] encrypted = HybridEncrypt(plainText, password);
- // Dekripsi
- string decrypted = HybridDecrypt(encrypted, password);
- Console.WriteLine (decrypted);
- }
- public static byte[] HybridEncrypt(string plainText, string password)
- {
- string desEncrypted = DESEncrypt (plainText, password);
- byte[] finalEncrypted = AESEncrypt (desEncrypted, password);
- return finalEncrypted;
- }
- public static string HybridDecrypt(byte[] encryptedBytes, string password)
- {
- string aesDecrypted = AESDecrypt (encryptedBytes, password);
- string finalDecrypted = DESDecrypt (aesDecrypted, password);
- return finalDecrypted;
- }
- public static string DESEncrypt(string originalString, string key)
- {
- byte[] bytes = CreateDESKey (key);
- DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
- MemoryStream memoryStream = new MemoryStream();
- CryptoStream cryptoStream = new CryptoStream(memoryStream,
- cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
- StreamWriter writer = new StreamWriter(cryptoStream);
- writer.Write(originalString);
- writer.Flush();
- cryptoStream.FlushFinalBlock();
- writer.Flush();
- return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
- }
- public static string DESDecrypt(string cryptedString, string key)
- {
- DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
- MemoryStream memoryStream = new MemoryStream
- (Convert.FromBase64String(cryptedString));
- byte[] bytes = CreateDESKey (key);
- CryptoStream cryptoStream = new CryptoStream(memoryStream,
- cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
- StreamReader reader = new StreamReader(cryptoStream);
- return reader.ReadToEnd();
- }
- private static byte[] AESEncrypt(string originalString, string password)
- {
- System.Text.UTF8Encoding Byte_Transform = new System.Text.UTF8Encoding();
- RijndaelManaged algo = new RijndaelManaged ();
- algo.Key = CreateKey (password);
- algo.IV = CreateIV (password);
- MemoryStream memoryStream = new MemoryStream ();
- ICryptoTransform encryptor = algo.CreateEncryptor (algo.Key, algo.IV);
- CryptoStream cryptoStream = new CryptoStream (memoryStream, encryptor, CryptoStreamMode.Write);
- byte[] PlainBytes = Byte_Transform.GetBytes(originalString);
- cryptoStream.Write (PlainBytes, 0, PlainBytes.Length);
- algo.Clear ();
- cryptoStream.Close ();
- return memoryStream.ToArray ();
- }
- private static string AESDecrypt(byte[] encryptedBytes, string password)
- {
- RijndaelManaged algo = new RijndaelManaged ();
- algo.Key = CreateKey(password);
- algo.IV = CreateIV(password);
- MemoryStream memoryStream = new MemoryStream (encryptedBytes);
- ICryptoTransform decryptor = algo.CreateDecryptor (algo.Key, algo.IV);
- CryptoStream cryptoStream = new CryptoStream (memoryStream, decryptor, CryptoStreamMode.Read);
- StreamReader streamReader = new StreamReader (cryptoStream);
- String plainText = streamReader.ReadToEnd ();
- algo.Clear ();
- memoryStream.Flush ();
- memoryStream.Close ();
- return plainText;
- }
- private static byte[] CreateDESKey(string strPassword)
- {
- char[] chrData = strPassword.ToCharArray();
- int intLength = chrData.GetUpperBound(0);
- byte[] bytDataToHash = new byte[intLength + 1];
- for (int i = 0; i <= chrData.GetUpperBound(0); i++)
- {
- bytDataToHash[i] = Convert.ToByte(chrData[i]);
- }
- System.Security.Cryptography.SHA512Managed SHA512 = new System.Security.Cryptography.SHA512Managed();
- byte[] bytResult = SHA512.ComputeHash(bytDataToHash);
- byte[] bytKey = new byte[8];
- for (int i = 0; i <= 7; i++)
- {
- bytKey[i] = bytResult[i];
- }
- return bytKey;
- }
- private static byte[] CreateKey(string strPassword)
- {
- //Convert strPassword to an array and store in chrData.
- char[] chrData = strPassword.ToCharArray();
- //Use intLength to get strPassword size.
- int intLength = chrData.GetUpperBound(0);
- //Declare bytDataToHash and make it the same size as chrData.
- byte[] bytDataToHash = new byte[intLength + 1];
- //Use For Next to convert and store chrData into bytDataToHash.
- for (int i = 0; i <= chrData.GetUpperBound(0); i++)
- {
- bytDataToHash[i] = Convert.ToByte(chrData[i]);
- }
- //Declare what hash to use.
- System.Security.Cryptography.SHA512Managed SHA512 = new System.Security.Cryptography.SHA512Managed();
- //Declare bytResult, Hash bytDataToHash and store it in bytResult.
- byte[] bytResult = SHA512.ComputeHash(bytDataToHash);
- //Declare bytKey(31). It will hold 256 bits.
- byte[] bytKey = new byte[32];
- //Use For Next to put a specific size (256 bits) of
- //bytResult into bytKey. The 0 To 31 will put the first 256 bits
- //of 512 bits into bytKey.
- for (int i = 0; i <= 31; i++)
- {
- bytKey[i] = bytResult[i];
- }
- return bytKey;
- //Return the key.
- }
- private static byte[] CreateIV(string strPassword)
- {
- //Convert strPassword to an array and store in chrData.
- char[] chrData = strPassword.ToCharArray();
- //Use intLength to get strPassword size.
- int intLength = chrData.GetUpperBound(0);
- //Declare bytDataToHash and make it the same size as chrData.
- byte[] bytDataToHash = new byte[intLength + 1];
- //Use For Next to convert and store chrData into bytDataToHash.
- for (int i = 0; i <= chrData.GetUpperBound(0); i++)
- {
- bytDataToHash[i] = Convert.ToByte(chrData[i]);
- }
- //Declare what hash to use.
- System.Security.Cryptography.SHA512Managed SHA512 = new System.Security.Cryptography.SHA512Managed();
- //Declare bytResult, Hash bytDataToHash and store it in bytResult.
- byte[] bytResult = SHA512.ComputeHash(bytDataToHash);
- //Declare bytIV(15). It will hold 128 bits.
- byte[] bytIV = new byte[16];
- //Use For Next to put a specific size (128 bits) of
- //bytResult into bytIV. The 0 To 30 for bytKey used the first 256 bits.
- //of the hashed password. The 32 To 47 will put the next 128 bits into bytIV.
- for (int i = 32; i <= 47; i++)
- {
- bytIV[i - 32] = bytResult[i];
- }
- return bytIV;
- //return the IV
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement