Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static class Crypt
- {
- private static IBuffer GetMD5Hash()
- {
- // Convert the message string to binary data.
- IBuffer buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary("mtzupjc", BinaryStringEncoding.Utf8);
- // Create a HashAlgorithmProvider object.
- HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
- // Hash the message.
- IBuffer buffHash = objAlgProv.HashData(buffUtf8Msg);
- // Verify that the hash length equals the length specified for the algorithm.
- if (buffHash.Length != objAlgProv.HashLength)
- {
- Debug.WriteLine("There was an error creating the hash");
- throw new Exception("There was an error creating the hash");
- }
- return buffHash;
- }
- public static string Encrypt(string toEncrypt)
- {
- try
- {
- // Get the MD5 key hash (you can as well use the binary of the key string)
- var keyHash = GetMD5Hash();
- // Create a buffer that contains the encoded message to be encrypted.
- var toDecryptBuffer = CryptographicBuffer.ConvertStringToBinary(toEncrypt, BinaryStringEncoding.Utf8);
- // Open a symmetric algorithm provider for the specified algorithm.
- var aes = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcbPkcs7);
- // Create a symmetric key.
- var symetricKey = aes.CreateSymmetricKey(keyHash);
- // The input key must be securely shared between the sender of the cryptic message
- // and the recipient. The initialization vector must also be shared but does not
- // need to be shared in a secure manner. If the sender encodes a message string
- // to a buffer, the binary encoding method must also be shared with the recipient.
- var buffEncrypted = CryptographicEngine.Encrypt(symetricKey, toDecryptBuffer, null);
- // Convert the encrypted buffer to a string (for display).
- // We are using Base64 to convert bytes to string since you might get unmatched characters
- // in the encrypted buffer that we cannot convert to string with UTF8.
- var strEncrypted = CryptographicBuffer.EncodeToBase64String(buffEncrypted);
- return strEncrypted;
- }
- catch (Exception ex)
- {
- Debug.WriteLine(ex);
- return "Encrypt Error";
- }
- }
- public static string Decrypt(string EncryptString)
- {
- try
- {
- // Get the MD5 key hash (you can as well use the binary of the key string)
- var keyHash = GetMD5Hash();
- // Create a buffer that contains the encoded message to be decrypted.
- IBuffer toDecryptBuffer = CryptographicBuffer.DecodeFromBase64String(EncryptString);
- // Open a symmetric algorithm provider for the specified algorithm.
- SymmetricKeyAlgorithmProvider aes = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcbPkcs7);
- // Create a symmetric key.
- var symetricKey = aes.CreateSymmetricKey(keyHash);
- var buffDecrypted = CryptographicEngine.Decrypt(symetricKey, toDecryptBuffer, null);
- string strDecrypted = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, buffDecrypted);
- return strDecrypted;
- }
- catch (Exception ex)
- {
- Debug.WriteLine(ex);
- return "Decrypt Error";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement