简体   繁体   中英

TripleDES Encryption in Windows Store app (Windows 8.1)

I want to encrypt some text using TripleDES Encryption with ECB cipher mode in Windows Store app (Windows 8.1) but I am having issues in creating a key for symmetric algorithm.

I would like to tell you what I am currently doing in .NET 4.5

public static string EncryptData(string Message, string passphrase)
        {
            byte[] tpinBytes = System.Text.Encoding.ASCII.GetBytes(Message);
            string tpinHex = ByteArrayHelper.ByteArrayToHexString(tpinBytes);

            byte[] Results;

            byte[] TDESKey = ByteArrayHelper.HexStringToByteArray(passphrase);

            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.Zeros;

            byte[] DataToEncrypt = ByteArrayHelper.HexStringToByteArray(tpinHex);
            try
            {
                ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
                Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
            }
            finally
            {
                TDESAlgorithm.Clear();
            }
            return ByteArrayHelper.ByteArrayToHexString(Results);
        }

Now, I have written this code snippet for my Windows Store (Windows 8.1) app;

  private static string TripleDESEncryption(string strMsg, string passphrase)
        {
            String strAlgName = SymmetricAlgorithmNames.TripleDesEcb;
            var bytes = System.Text.Encoding.UTF8.GetBytes(strMsg);
            string hex = BitConverter.ToString(bytes).Replace("-", "");

            // Initialize the initialization vector
            IBuffer iv = null;

            // Create a buffer that contains the encoded message to be encrypted. 
            IBuffer DataToEncrypt = CryptographicBuffer.DecodeFromHexString(hex);

            // Open a symmetric algorithm provider for the specified algorithm. 
            SymmetricKeyAlgorithmProvider objAlg = SymmetricKeyAlgorithmProvider.OpenAlgorithm(strAlgName);

            // Create a symmetric key.
            IBuffer TDESKey = CryptographicBuffer.DecodeFromHexString(passphrase);
            CryptographicKey key = objAlg.CreateSymmetricKey(TDESKey); // Line of problem.

            // Encrypt the data and return.
            IBuffer buffEncrypt = CryptographicEngine.Encrypt(key, DataToEncrypt, iv);

            return CryptographicBuffer.EncodeToHexString(buffEncrypt);
        }

When I match the values of TDESKey and EncryptData, they are identical. However, the issue occurs when I try to create symmetric key (after TDESKey assignment). It gives me an error of Value does not fall within the expected range and according to MSDN forums , the block size may not be supported (which I am unable to understand) and it does not even have those properties which are listed in that forum (for eg SupportedKeyLengths).

Can anyone help me out with the sample or point out the mistake I have been making?

WinRT does not support 16-byte keys. Try a 24-byte key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM