错误在c#.net中解密路径字符串

时间:2016-12-09 08:28:43

标签: c# winforms encryption

我有一个需要在数据库中存储Windows路径的Windows应用程序。 示例:C:\Folder 1\Folder 2\ Folder 3\Folder 4\Folder 5\Folder 6\Folder 7\Folder 8\Folder 9\Folder 10\File.txt

但在保存之前,出于安全考虑,我需要先加密它。

这是我的加密方法:

 private static string Encrypt<T>(string value, string password)
            where T : SymmetricAlgorithm, new()
{

    byte[] vectorBytes = System.Text.Encoding.ASCII.GetBytes(_vector);
    byte[] saltBytes = System.Text.Encoding.ASCII.GetBytes(_salt);
    byte[] valueBytes = System.Text.UTF8Encoding.ASCII.GetBytes(value);

    byte[] encrypted;
    using (T cipher = new T())
    {
        PasswordDeriveBytes _passwordBytes =
            new PasswordDeriveBytes(password, saltBytes, _hash, _iterations);
        byte[] keyBytes = _passwordBytes.GetBytes(_keySize / 8);

        cipher.Mode = CipherMode.CBC;

        using (ICryptoTransform encryptor = cipher.CreateEncryptor(keyBytes, vectorBytes))
        {
            using (MemoryStream to = new MemoryStream())
            {
                using (CryptoStream writer = new CryptoStream(to, encryptor, CryptoStreamMode.Write))
                {
                    writer.Write(valueBytes, 0, valueBytes.Length);
                    writer.FlushFinalBlock();
                    encrypted = to.ToArray();
                }
            }
        }
        cipher.Clear();
    }
    return Convert.ToBase64String(encrypted);
}

之后,系统将其解密以进行显示。这是我的解密方法:

private static string Decrypt<T>(string value, string password) where T : SymmetricAlgorithm, new()
{
    byte[] vectorBytes = System.Text.Encoding.ASCII.GetBytes(_vector);
    byte[] saltBytes = System.Text.Encoding.ASCII.GetBytes(_salt);
    byte[] valueBytes = Convert.FromBase64String(value);

    byte[] decrypted;
    int decryptedByteCount = 0;

    using (T cipher = new T())
    {
        PasswordDeriveBytes _passwordBytes = new PasswordDeriveBytes(password, saltBytes, _hash, _iterations);
        byte[] keyBytes = _passwordBytes.GetBytes(_keySize / 8);

        cipher.Mode = CipherMode.CBC;

        try
        {
            using (ICryptoTransform decryptor = cipher.CreateDecryptor(keyBytes, vectorBytes))
            {
                using (MemoryStream from = new MemoryStream(valueBytes))
                {
                    using (CryptoStream reader = new CryptoStream(from, decryptor, CryptoStreamMode.Read))
                    {
                        decrypted = new byte[valueBytes.Length];
                        decryptedByteCount = reader.Read(decrypted, 0, decrypted.Length);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            return String.Empty;
        }

        cipher.Clear();
    }
    return Encoding.UTF8.GetString(decrypted, 0, decryptedByteCount);
}

但系统继续抛出错误:

  

“输入不是有效的Base-64字符串”。

我不知道是不是因为路径字符串太长了?还是因为空间?所以我做的是在调用加密和解密时,我正在调用HttpUtility.UrlKeyValueEncode and HttpUtility.UrlKeyValueDecode

public string Encryption(string stringToEncrypt)
{
    return HttpUtility.UrlKeyValueEncode(Encrypt(stringToEncrypt));
}


public string Decryption(string stringToDecrypt)
{
    return Decrypt(HttpUtility.UrlKeyValueDecode(stringToDecrypt));
}

这适用于我。但是,当我的路径有' - '字符时,会出现同样的问题。示例路径:C:\Folder 1 - copy\Folder 2 - copy\ Folder 3 - copy\Folder 4 - copy\Folder 5 - copy\Folder 6 - copy\Folder 7 - copy\Folder 8 - copy\Folder 9 - copy\Folder 10 - copy\File.txt

出了什么问题?

0 个答案:

没有答案