Advertisement
Guest User

Untitled

a guest
Mar 5th, 2017
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.85 KB | None | 0 0
  1.  static class Crypt
  2.     {
  3.         private static IBuffer GetMD5Hash()
  4.         {
  5.             // Convert the message string to binary data.
  6.             IBuffer buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary("mtzupjc", BinaryStringEncoding.Utf8);
  7.  
  8.             // Create a HashAlgorithmProvider object.
  9.             HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
  10.  
  11.             // Hash the message.
  12.             IBuffer buffHash = objAlgProv.HashData(buffUtf8Msg);
  13.  
  14.             // Verify that the hash length equals the length specified for the algorithm.
  15.             if (buffHash.Length != objAlgProv.HashLength)
  16.             {
  17.                 Debug.WriteLine("There was an error creating the hash");
  18.                 throw new Exception("There was an error creating the hash");
  19.             }
  20.             return buffHash;
  21.         }
  22.  
  23.         public static string Encrypt(string toEncrypt)
  24.         {
  25.             try
  26.             {
  27.                 // Get the MD5 key hash (you can as well use the binary of the key string)
  28.                 var keyHash = GetMD5Hash();
  29.  
  30.                 // Create a buffer that contains the encoded message to be encrypted.
  31.                 var toDecryptBuffer = CryptographicBuffer.ConvertStringToBinary(toEncrypt, BinaryStringEncoding.Utf8);
  32.  
  33.                 // Open a symmetric algorithm provider for the specified algorithm.
  34.                 var aes = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcbPkcs7);
  35.  
  36.                 // Create a symmetric key.
  37.                 var symetricKey = aes.CreateSymmetricKey(keyHash);
  38.  
  39.                 // The input key must be securely shared between the sender of the cryptic message
  40.                 // and the recipient. The initialization vector must also be shared but does not
  41.                 // need to be shared in a secure manner. If the sender encodes a message string
  42.                 // to a buffer, the binary encoding method must also be shared with the recipient.
  43.  
  44.                 var buffEncrypted = CryptographicEngine.Encrypt(symetricKey, toDecryptBuffer, null);
  45.  
  46.                 // Convert the encrypted buffer to a string (for display).
  47.                 // We are using Base64 to convert bytes to string since you might get unmatched characters
  48.                 // in the encrypted buffer that we cannot convert to string with UTF8.
  49.                 var strEncrypted = CryptographicBuffer.EncodeToBase64String(buffEncrypted);
  50.  
  51.                 return strEncrypted;
  52.             }
  53.             catch (Exception ex)
  54.             {
  55.                 Debug.WriteLine(ex);
  56.                 return "Encrypt Error";
  57.             }
  58.         }
  59.  
  60.         public static string Decrypt(string EncryptString)
  61.         {
  62.             try
  63.             {
  64.                 // Get the MD5 key hash (you can as well use the binary of the key string)
  65.                 var keyHash = GetMD5Hash();
  66.  
  67.                 // Create a buffer that contains the encoded message to be decrypted.
  68.                 IBuffer toDecryptBuffer = CryptographicBuffer.DecodeFromBase64String(EncryptString);
  69.  
  70.                 // Open a symmetric algorithm provider for the specified algorithm.
  71.                 SymmetricKeyAlgorithmProvider aes = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcbPkcs7);
  72.  
  73.                 // Create a symmetric key.
  74.                 var symetricKey = aes.CreateSymmetricKey(keyHash);
  75.  
  76.                 var buffDecrypted = CryptographicEngine.Decrypt(symetricKey, toDecryptBuffer, null);
  77.  
  78.                 string strDecrypted = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, buffDecrypted);
  79.  
  80.                 return strDecrypted;
  81.             }
  82.             catch (Exception ex)
  83.             {
  84.                 Debug.WriteLine(ex);
  85.                 return "Decrypt Error";
  86.             }
  87.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement