如何在C#中创建受密码保护的文件

时间:2013-10-12 01:27:07

标签: c# .net .net-4.5

我知道之前已经问过这个问题,但答案根本不是我需要的。我需要创建一个受密码保护的文本文件。我不是要加密,而只是创建一个带有简单文本密码的文件。了解如何在C#中打开此文件也很好。

创建纯文本密码保护文件,稍后再打开该文件。全部在C#。

2 个答案:

答案 0 :(得分:1)

密码保护未加密的文本文件实际上是不可能的。但是,您可以验证您的文本文件是否已被修改,以便您知道它已被某人更改。

这个概念很简单。使用密码加密或混淆数据的散列(文本文件)。您可以稍后根据当前数据检查此哈希,并确定它是否匹配。您需要将此签名(加密哈希)存储在某个名为(textfile.sig)的文件中。

您可以使用SHA1Managed .NET类创建散列,使用TripleDESCryptoServiceProvider类来加密生成的散列。

类似......

public string GetSignature(string text, string password)
{
    byte[] key;
    byte[] key2;
    GetKeys(password, out key, out key2);

    string sig = encryptstring(GetSHA1(text), key, key2);

}    

public void GetKeys(string password, out byte[] key, out byte[] key2)
{
    byte[] data = StringToByte(password);
    SHA1 sha = new SHA1CryptoServiceProvider();
    MD5 md5 = new MD5CryptoServiceProvider();

    byte[] hash1 = sha.ComputeHash(data);
    byte[] hash2 = md5.ComputeHash(data);

    // Generate some key data based on the supplied password;
    byte[] key = new byte[24];
    for (int i = 0; i < 20; i++)
    {
        key[i] = hash1[i];
    }
    for (int i = 0; i < 4; i++)
    {
        key[i + 20] = hash2[i];
    }
    byte[] key2 = new byte[8];

    for (int i = 0; i < 8; i++)
    {
        key2[i] = hash2[i+4];
    }
}

public string GetSHA1(string text)
{
    UnicodeEncoding UE = new UnicodeEncoding();
    byte[] hashValue;
    byte[] message = UE.GetBytes(text);

    SHA1Managed hashString = new SHA1Managed();
    string hex = "";

    hashValue = hashString.ComputeHash(message);
    foreach (byte x in hashValue)
    {
        hex += String.Format("{0:x2}", x);
    }
    return hex;
}

public string ByteToString(byte[] buff)
{
    string sbinary = "";
    for (int i = 0; i < buff.Length; i++)
    {
        sbinary += buff[i].ToString("X2"); // hex format
    }
    return (sbinary);
}

public string encryptstring(string instr, byte[] key, byte[] key2)
{
    TripleDES threedes = new TripleDESCryptoServiceProvider();
    threedes.Key = key;
    threedes.IV = key2;

    ICryptoTransform encryptor = threedes.CreateEncryptor(key, key2);
    MemoryStream msEncrypt = new MemoryStream();
    CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

    // Write all data to the crypto stream and flush it.
    csEncrypt.Write(StringToByte(instr), 0, StringToByte(instr).Length);
    csEncrypt.FlushFinalBlock();

    return ByteToString(msEncrypt.ToArray());
}

public string decryptstring(string instr, byte[] key, byte[] key2)
{
    if (string.IsNullOrEmpty(instr)) return "";

    TripleDES threedes = new TripleDESCryptoServiceProvider();
    threedes.Key = key;
    threedes.IV = key2;

    ICryptoTransform decryptor = threedes.CreateDecryptor(key, key2);

    // Now decrypt the previously encrypted message using the decryptor
    MemoryStream msDecrypt = new MemoryStream(HexStringToByte(instr));
    CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);

    try
    {
        return ByteToString(csDecrypt);
    }
    catch (CryptographicException)
    {
        return "";
    }
}

否则,如果要隐藏人员数据,则需要加密文本文件中的数据。您可以通过使用全文数据而不是数据的哈希调用encryptstring()来完成此操作。

答案 1 :(得分:1)

问题的关键在于不必手动加密数据,但让Windows处理它,所以我决定采用Hans Passant的建议,只需使用密码保护的zip文件,里面包含所需的信息(txt文件) 。这可以通过DotNetZip轻松完成。

相关问题