C# Windows应用商店应用程序(Windows 8.1)中的三重加密

C# Windows应用商店应用程序(Windows 8.1)中的三重加密,c#,.net,windows-runtime,windows-store-apps,encryption-symmetric,C#,.net,Windows Runtime,Windows Store Apps,Encryption Symmetric,我想在Windows应用商店应用程序(Windows 8.1)中使用带ECB密码模式的TripleDES Encryption对一些文本进行加密,但在为对称算法创建密钥时遇到问题 我想告诉你我目前在.NET 4.5中做什么 public static string EncryptData(string Message, string passphrase) { byte[] tpinBytes = System.Text.Encoding.ASCII.Ge

我想在Windows应用商店应用程序(Windows 8.1)中使用带ECB密码模式的TripleDES Encryption对一些文本进行加密,但在为对称算法创建密钥时遇到问题

我想告诉你我目前在.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);
        }
现在,我已经为我的Windows应用商店(Windows 8.1)应用程序编写了此代码段

  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);
        }
当我匹配TDESKey和EncryptData的值时,它们是相同的。但是,当我尝试创建对称密钥(在TDESKey分配之后)时会出现问题。它给我一个错误值不在预期范围内,根据,块大小可能不受支持(我无法理解),甚至没有该论坛中列出的属性(例如SupportedKeyLength)


有人能帮我拿样品或指出我犯的错误吗

WinRT不支持16字节键。尝试一个24字节的键

你如何适应widows应用程序中的paddingmode.zero?我认为没有办法做到这一点。是吗?:)你有解决办法吗?我也得到了同样的错误