【问题标题】:TripleDES Encryption in Windows Store app (Windows 8.1)Windows 应用商店应用程序中的 TripleDES 加密 (Windows 8.1)
【发布时间】:2014-02-18 13:58:17
【问题描述】:

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

我想告诉你我目前在 .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 Store (Windows 8.1) 应用编写了这段代码 sn-p;

  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 分配之后)时,就会出现问题。它给了我一个值不在预期范围内的错误,并且根据MSDN forums,可能不支持块大小(我无法理解),它甚至没有那些该论坛中列出的属性(例如 SupportedKeyLengths)。

谁能帮我提供样本或指出我一直犯的错误?

【问题讨论】:

  • 你如何在寡妇应用程序中适应 paddingmode.zeros?
  • 我认为没有办法做到这一点。是吗? :)
  • 你有解决办法吗?我也遇到同样的错误

标签: c# .net windows-runtime windows-store-apps encryption-symmetric


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多