在.NET中,使用秘钥加密/混淆字节数组的简单方法是什么?

9
我正在寻找一种使用.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库中是否有我可以轻松使用的东西?

我发现最简单的方法在我的问题中。 - Blorgbeard
那么进制转换不行吗?比如转换为Base64甚至Base2?我认为这比ROT13更好! - Ali
1
@Blorgbeard:为什么不把那个作为答案,我会给你信用!那正是我一直在寻找的! - Krumelur
5个回答

8
这是我编写的加密/解密字符串的代码,加密后的字符串经过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());
    }
}

4
下面是使用.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

对称加密算法 是实现这一目的最简单的方法,你可以在.NET框架中找到这些算法。

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


1

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接