Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在.NET中使用密码加密/混淆字节数组的简单方法?_C#_.net - Fatal编程技术网

C# 在.NET中使用密码加密/混淆字节数组的简单方法?

C# 在.NET中使用密码加密/混淆字节数组的简单方法?,c#,.net,C#,.net,我正在寻找一种使用.NET3.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"); 它不必是防弹的。这个想法只是为了防止用户直接看到字节中的内容,

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

中有各种有趣的东西。

这是最简单的方法,您可以在.NET framework中找到它们


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

以下是使用.NET framework中的Rijndael类加密和解密字节数组的代码示例;显然,这门课可以取代最适合你的课

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

私有静态字节[]加密字节(IEnumerable字节)
{
//由于MSDN文档表明公共方法可能不是线程安全的,因此我们无法保存对实例的静态引用,因此为每个对此方法的调用创建了ICryptoTransform
使用(var r=Rijndael.Create())
{
使用(var encryptor=r.CreateEncryptor(KEY,IV))
{
返回转换(字节、加密器);
}
}
}
专用静态字节[]解密字节(IEnumerable字节)
{
//由于MSDN文档表明公共方法可能不是线程安全的,因此我们无法保存对实例的静态引用,因此为每个对此方法的调用创建了ICryptoTransform
使用(var r=Rijndael.Create())
{
使用(var decryptor=r.CreateDecryptor(KEY,IV))
{
返回转换(字节、解密器);
}
}
}
专用静态字节[]转换(IEnumerable字节,ICryptoTransform转换)
{
使用(var stream=new MemoryStream())
{
使用(var cryptoStream=newcryptostream(stream、transform、CryptoStreamMode.Write))
{
foreach(变量b,字节)
cryptoStream.WriteByte(b);
}
返回流ToArray();
}
}

如果你不需要加密,你可以把所有东西都转换成十六进制或Base 64或类似的格式,除非有人真的很专注,否则实际上这会让你无法阅读

下面是我为加密/解密字符串而编写的一些代码。加密字符串是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());
    }
}
//
///创建并初始化加密算法。
/// 
///密码。
私有静态对称算法(字符串密码)
{
var algorithm=Rijndael.Create();
var rdb=新的Rfc2898DeriveBytes(密码,新字节[]){
0x53,0x6f,0x64,0x69,0x75,0x6d,0x20,//咸味佳肴
0x43,0x68,0x6c,0x6f,0x72,0x69,0x64,0x65
});
算法.Padding=PaddingMode.ISO10126;
算法.Key=rdb.GetBytes(32);
算法IV=rdb.GetBytes(16);
返回算法;
}
/// 
///使用给定密码加密字符串。
/// 
///清晰的文本。
///密码。
公共静态字符串加密字符串(字符串明文、字符串密码)
{
var算法=GetAlgorithm(密码);
var encryptor=algorithm.CreateEncryptor();
var clearBytes=Encoding.Unicode.GetBytes(明文);
使用(var ms=new MemoryStream())
使用(var cs=新加密流(ms、encryptor、CryptoStreamMode.Write))
{
cs.Write(clearBytes,0,clearBytes.Length);
cs.Close();
返回Convert.tobase64字符串(ms.ToArray());
}
}
/// 
///使用给定密码解密字符串。
/// 
///密码文本。
///密码。
公共静态字符串解密字符串(字符串密文、字符串密码)
{
var算法=GetAlgorithm(密码);
var decryptor=algorithm.CreateDecryptor();
var cipherBytes=Convert.FromBase64String(密文);
使用(var ms=new MemoryStream())
使用(var cs=newcryptostream(ms、解密器、CryptoStreamMode.Write))
{
cs.Write(cipherBytes,0,cipherBytes.Length);
cs.Close();
返回Encoding.Unicode.GetString(ms.ToArray());
}
}

我发现的最简单的方法是,这样基本转换就不可能了?比如说转换到Base64甚至Base2?我觉得这比ROT13好@布罗格比尔德:为什么不回答这个问题呢,我相信你!这正是我一直在寻找的!
/// <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());
    }
}