使用C#检索访问.mdb数据库密码

时间:2017-01-03 14:21:14

标签: c# ms-access

构建一个读取访问数据库文件的c#应用程序。 每个数据库都有不同的密码。目前我正在使用Access Passview http://www.nirsoft.net/utils/accesspv.html(免费软件)来读取密码,但我希望能够自动化它,以便我可以将其分配给OLEDB连接字符串的字符串。 (执行时exe的屏幕截图)

enter image description here

exe可以从命令行运行,这是我试图在我的应用程序中实现的

    var proc = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "accesspv.exe",
            Arguments = _filePath, 
            UseShellExecute = false,
            RedirectStandardOutput = true,
            CreateNoWindow = true,     
        }
    };

    proc.Start();
    while (!proc.StandardOutput.EndOfStream)
    {
        string line = proc.StandardOutput.ReadToEnd();
        Console.WriteLine(line);
       _password2016 = line;
   }

这对我不起作用,因为访问passview exe正常运行,密码不会显示在控制台中。

我的主要问题是 1.是否可以读取密码,将其分配给我的连接字符串的变量? 2.有没有这样,accesspv.exe在后台运行,所以最终用户没有看到它?

感谢。

1 个答案:

答案 0 :(得分:2)

该工具的源代码可在网站here上找到。你可以在C#中编写相同的代码。

public class Program
{
    private static readonly byte[] XorBytes = {
        0x86, 0xFB, 0xEC, 0x37, 0x5D, 0x44, 0x9C, 0xFA, 0xC6,
        0x5E, 0x28, 0xE6, 0x13, 0xB6, 0x8A, 0x60, 0x54, 0x94
    };

    public static void Main(string[] args)
    {
        var filePath = args[0];
        var fileBytes = new byte[256];

        using (var fileReader = File.OpenRead(filePath))
        {
            fileReader.Read(fileBytes, 0, fileBytes.Length);
        }

        var passwordBytes = XorBytes
            .Select((x, i) => (byte) (fileBytes[i + 0x42] ^ x))
            .TakeWhile(x => x != 0);
        var password = Encoding.ASCII.GetString(passwordBytes.ToArray());

        Console.WriteLine($"Password is \"{password}\"");
        Console.ReadKey();
    }
}