Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
159 lines (135 loc) · 6.42 KB
/
Copy pathProgram.cs
File metadata and controls
159 lines (135 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace BitwardenToChromePasswordConverter
{
class Program
{
static void Main(string[] args)
{
while (true)
{
try
{
Console.WriteLine(
"Rename your Bitwarden passwords .csv file to \"passwords.csv\", place it in your C: drive and press ENTER");
Console.WriteLine("Alternatively your can manually enter the path to your .csv file below");
Console.WriteLine("Example: \"C:\\Users\\user\\Desktop\\bitwarden_export.csv\"");
string filePath = Console.ReadLine();
if (filePath == "")
{
filePath = "C:\\passwords.csv";
}
string fileLocationDir = filePath.Substring(0, filePath.LastIndexOf('\\'));
string fileNameWithExtension = filePath.Split('\\').Last();
string fileName = fileNameWithExtension.Substring(0, fileNameWithExtension.LastIndexOf('.'));
string fileExtension = fileNameWithExtension.Split('.').Last();
if (fileExtension != "csv")
{
throw new Exception("ERROR: Invalid file extension. Must be .csv");
}
Console.WriteLine("Processing passwords...");
using (var reader = new StreamReader(filePath))
{
StringBuilder outputCsv = new StringBuilder();
outputCsv.AppendLine(@"name,url,username,password");
List<string?> headers = new List<string?>();
int indexOfName = 0;
int indexOfUrl = 0;
int indexOfUsername = 0;
int indexOfPassword = 0;
int lineNumber = 0;
while (!reader.EndOfStream)
{
// Read properties from line 0
string? line = reader.ReadLine();
string?[] values = line.Split(',');
if (lineNumber == 0)
{
headers = values.ToList();
indexOfName = headers.IndexOf("name");
indexOfUrl = headers.IndexOf("login_uri");
indexOfUsername = headers.IndexOf("login_username");
indexOfPassword = headers.IndexOf("login_password");
lineNumber++;
continue;
}
// Skip android logins
if (line.Contains("android://"))
{
continue;
}
if ((indexOfName + indexOfUrl + indexOfUsername + indexOfPassword) == 0)
{
throw new Exception("Invalid password file format. Please contact the developer!");
}
var name = values[indexOfName];
var uri = values[indexOfUrl];
var username = values[indexOfUsername];
var password = values[indexOfPassword];
string chromeCsvLine = $"{name},{uri},{username},{password}";
outputCsv.AppendLine(chromeCsvLine);
lineNumber++;
Console.WriteLine(name);
}
Console.WriteLine("Processing finished!");
Console.WriteLine("");
string timeStamp = DateTime.Now.ToString("yyyyMMddHHmmss");
string outputCsvString = outputCsv.ToString();
string outputFileName = $"BitwardenToChromePasswords_{timeStamp}.csv";
string outputFilePath = $"{fileLocationDir}\\{outputFileName}";
using (StreamWriter writetext = new StreamWriter(outputFilePath))
{
writetext.Write(outputCsvString);
}
Console.BackgroundColor = ConsoleColor.Green;
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("Operation successful!");
Console.ResetColor();
Console.WriteLine($"Output file: {outputFileName}");
Console.WriteLine($"Output location: {outputFilePath}");
Console.WriteLine("Press Enter to quit");
Console.ReadLine();
return;
}
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("ERROR: Cannot write output file. Please run the program as an administrator!");
Console.WriteLine("Press any key to exit!");
if (Console.ReadKey().Key != null)
{
return;
}
}
catch (Exception e)
{
if (!string.IsNullOrEmpty(e.Message))
{
Console.WriteLine(e.Message);
}
else
{
Console.WriteLine("ERROR: File not found, please enter a valid path!");
Console.WriteLine("Press any key to try again!");
}
if (Console.ReadKey().Key != null)
{
Console.Clear();
}
}
}
}
static void ThrowError(Exception e, string errorMessage)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("ERROR");
Console.WriteLine(errorMessage);
Console.ResetColor();
Console.WriteLine(e);
}
}
}