【问题标题】:Easy way to encrypt/obfuscate a byte array using a secret in .NET?使用.NET中的秘密加密/混淆字节数组的简单方法?
【发布时间】:2011-07-12 05:26:19
【问题描述】:

我正在寻找一种使用 .NET 3.5 加密/混淆(当然还有解密/去混淆)字节数组的方法。

基本上:

byte[] aMixedUp = Encrypt(aMyByteData, "THIS IS THE SECRET KEY USED TO ENCRYPT");

另一方面:

byte[] aDecrypted = Decrypt(aMixedUp, "THIS IS THE SECRET KEY USED TO ENCRYPT");

它不一定是防弹的。这个想法只是为了防止用户直接看到字节中的内容,以防它们映射到 ASCII,但它应该比 ROT13 更好。

.NET 库中有什么我可以轻松使用的东西吗?

【问题讨论】:

  • 我发现最简单的方法是在my question here
  • 所以基础转换是不可能的?可以说转换为 Base64 甚至是 Base2?我认为这比 ROT13 更好!
  • @Blorgbeard:为什么不回答这个问题,我会给你功劳!这正是我一直在寻找的!

标签: c# .net


【解决方案1】:

这是我为加密/解密字符串而编写的一些代码。加密字符串采用 Base64 编码,便于序列化为 XML 等。您可以轻松将此代码转换为直接使用字节数组而不是字符串。

/// <summary>
/// Create and initialize a crypto algorithm.
/// </summary>
/// <param name="password">The password.</param>
private static SymmetricAlgorithm GetAlgorithm(string password)
{
    var algorithm = Rijndael.Create();
    var rdb = new Rfc2898DeriveBytes(password, new byte[] {
        0x53,0x6f,0x64,0x69,0x75,0x6d,0x20,             // salty goodness
        0x43,0x68,0x6c,0x6f,0x72,0x69,0x64,0x65
    });
    algorithm.Padding = PaddingMode.ISO10126;
    algorithm.Key = rdb.GetBytes(32);
    algorithm.IV = rdb.GetBytes(16);
    return algorithm;
}


/// <summary>
/// Encrypts a string with a given password.
/// </summary>
/// <param name="clearText">The clear text.</param>
/// <param name="password">The password.</param>
public static string EncryptString(string clearText, string password)
{
    var algorithm = GetAlgorithm(password);
    var encryptor = algorithm.CreateEncryptor();
    var clearBytes = Encoding.Unicode.GetBytes(clearText);
    using (var ms = new MemoryStream())
    using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
    {
        cs.Write(clearBytes, 0, clearBytes.Length);
        cs.Close();
        return Convert.ToBase64String(ms.ToArray());
    }
}

/// <summary>
/// Decrypts a string using a given password.
/// </summary>
/// <param name="cipherText">The cipher text.</param>
/// <param name="password">The password.</param>
public static string DecryptString(string cipherText, string password)
{
    var algorithm = GetAlgorithm(password);
    var decryptor = algorithm.CreateDecryptor();
    var cipherBytes = Convert.FromBase64String(cipherText);
    using (var ms = new MemoryStream())
    using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
    {
        cs.Write(cipherBytes, 0, cipherBytes.Length);
        cs.Close();
        return Encoding.Unicode.GetString(ms.ToArray());
    }
}

【讨论】:

    【解决方案2】:

    以下是使用 .NET 框架中的 Rijndael 类对字节数组进行加密和解密的代码示例;显然这个类可以替换成最适合你的。

    您只需要定义 KEY 和 IV 属性并从某处获取它们(例如应用程序配置文件的加密部分)。

        private static byte[] EncryptBytes(IEnumerable<byte> bytes)
        {
            //The ICryptoTransform is created for each call to this method as the MSDN documentation indicates that the public methods may not be thread-safe and so we cannot hold a static reference to an instance
            using (var r = Rijndael.Create())
            {
                using (var encryptor = r.CreateEncryptor(KEY, IV))
                {
                    return Transform(bytes, encryptor);
                }
            }
        }
    
        private static byte[] DecryptBytes(IEnumerable<byte> bytes)
        {
            //The ICryptoTransform is created for each call to this method as the MSDN documentation indicates that the public methods may not be thread-safe and so we cannot hold a static reference to an instance
            using (var r = Rijndael.Create())
            {
                using (var decryptor = r.CreateDecryptor(KEY, IV))
                {
                    return Transform(bytes, decryptor);
                }
            }
        }
    
        private static byte[] Transform(IEnumerable<byte> bytes, ICryptoTransform transform)
        {
            using (var stream = new MemoryStream())
            {
                using (var cryptoStream = new CryptoStream(stream, transform, CryptoStreamMode.Write))
                {
                    foreach (var b in bytes)
                        cryptoStream.WriteByte(b);
                }
    
                return stream.ToArray();
            }
        }
    

    【讨论】:

      【解决方案3】:

      Symmetric-key algorithm 是最简单的方法,您可以在 .NET 框架中找到它们。

      但请注意,黑客可以“轻松”反编译您的应用并找到加密密钥。根据您的情况,您可以使用public/private 密钥系统。你至少可以控制谁可以加密字节数组。

      【讨论】:

        【解决方案4】:

        cryptography namespace里有各种好玩的东西。

        【讨论】:

          【解决方案5】:

          如果您不需要加密,则可以将所有内容转换为 HEX 或 Base 64 或类似的东西,除非有人真的很敬业,否则它实际上会让人无法阅读。 Here is a link that shows how in .NET

          【讨论】:

            猜你喜欢
            • 2015-08-11
            • 2021-05-25
            • 2012-10-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-02-28
            • 1970-01-01
            相关资源
            最近更新 更多