【问题标题】:Encrypted string changes on different application domain不同应用程序域上的加密字符串更改
【发布时间】:2012-05-13 22:24:37
【问题描述】:

以下加密和解密算法通过 powershell 和共享点应用程序页面调用:

    public static string Encrypt(string dataToEncrypt, string password, string salt)
    {
        AesManaged aes = null;
        MemoryStream memoryStream = null;
        CryptoStream cryptoStream = null;

        try
        {
            Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), 10000);

            aes = new AesManaged();
            aes.Key = rfc2898.GetBytes(32);
            aes.IV = rfc2898.GetBytes(16);

            memoryStream = new MemoryStream();
            cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write);

            byte[] data = Encoding.UTF8.GetBytes(dataToEncrypt);
            cryptoStream.Write(data, 0, data.Length);
            cryptoStream.FlushFinalBlock();

            return Convert.ToBase64String(memoryStream.ToArray());
        }
        finally
        {
            if (cryptoStream != null)
                cryptoStream.Close();

            if (memoryStream != null)
                memoryStream.Close();

            if (aes != null)
                aes.Clear();
        }
    }

为什么加密的字符串会改变?是关于应用领域的吗?

【问题讨论】:

  • 可能是因为添加了随机填充以避免 MITM 攻击。
  • 我试过你的代码,不管应用程序域如何,它似乎都会生成相同的结果。您确定没有其他转换吗?
  • @Oded:当我将填充定义为无时,问题仍然存在。
  • @w0lf:我确信没有任何其他转换。

标签: .net sharepoint encryption powershell aes


【解决方案1】:

当我使用相同的数据、密码和盐运行问题代码时,每次都会产生相同的结果。你应该确保 dataToEncrypt 和 Salt 每次都是相同的,即使一个字节改变了,其余的也改变了。

但是,为了语义安全,这不是您想要的。您想要一个随机盐以使暴力破解密码变得更加困难,并且需要一个随机的非秘密 IV 集,以便两个相同的明文没有相同的密文。

这里是encrypting and decrypting string 的最佳做法示例,使用了设计的加密算法安全功能。 SimpleEncryptWithPassword 与您正在执行的操作类似,尽管在示例中派生密钥的迭代是可变的,并且出于性能原因您可能希望对其进行硬编码。

【讨论】:

    【解决方案2】:

    加密字符串因$ 字符而不同。 $ 应该在通过 powershell 调用函数时转义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-19
      相关资源
      最近更新 更多