-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
200 lines (162 loc) · 7.6 KB
/
Program.cs
File metadata and controls
200 lines (162 loc) · 7.6 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System.Security.Cryptography;
using System.IO;
using Microsoft.VisualBasic;
using System.Transactions;
using System.Text.Json;
using System.Runtime.InteropServices;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Text;
static class cSharpUtils
{
static string encryptedIdentifier ="";
static string encryptedPassword ="";
static string decrypted;
static byte[] masterKey;
static bool programTerminateFlag = false;
static string inputIdentificador;
static string inputSenha;
static List<passwordTemplate> listOfPasswords = new List<passwordTemplate>();
static string decryptedIdentifier;
static string decryptedPassword;
static void Main()
{
SQLITEAPI.Interface.DatabaseCommand("create", "passwords");
SQLITEAPI.Interface.DatabaseCommand("select", "passwords");
SQLITEAPI.Interface.TableCommand("create", "encryptedPasswords", "Id INTEGER PRIMARY KEY AUTOINCREMENT, encIdentifier TEXT NOT NULL, encPassword TEXT NOT NULL");
if (File.Exists("firstTime.txt"))
{
using (Aes myAes = Aes.Create())
{
(byte[] key, byte[] IV) = masterKeyManager.GetMasterKey();
if (key != null && IV != null)
{
myAes.Key = key;
myAes.IV = IV;
myAes.Padding = PaddingMode.PKCS7;
ICryptoTransform encrypter = myAes.CreateEncryptor(key, IV);
ICryptoTransform decrypter = myAes.CreateDecryptor(key, IV);
System.Console.WriteLine("Acesso permitido. \n");
while (!programTerminateFlag)
{
System.Console.WriteLine("Selecione uma ação: \n");
System.Console.WriteLine("0. Sair");
System.Console.WriteLine("1. Adicionar nova senha");
System.Console.WriteLine("2. Mostrar senhas");
Int32.TryParse(Console.ReadLine(), out int commandIndex);
switch (commandIndex)
{
case 0:
programTerminateFlag = true;
break;
case 1:
System.Console.WriteLine("Insira o identificador: ");
inputIdentificador = Console.ReadLine();
System.Console.WriteLine("Insira a senha: ");
inputSenha = Console.ReadLine();
passwordTemplate newPassword = new passwordTemplate(inputIdentificador, inputSenha);
WriteToDatabase(encrypter, newPassword);
break;
case 2:
System.Console.WriteLine("Descriptografando...");
DecryptAll(decrypter);
System.Console.WriteLine("Sucesso!");
break;
}
}
}
else
{
System.Console.WriteLine("Acesso negado.");
}
}
}
else
{
masterKeyManager.FirstBootUp();
}
}
static public passwordTemplate Encrypt(ICryptoTransform encrypter, passwordTemplate input)
{
try
{
byte[] encryptedIdentifierByte;
byte[] encryptedPasswordByte;
System.Console.WriteLine(input.Identificador);
System.Console.WriteLine(input.Senha);
using (MemoryStream stream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(stream, encrypter, CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(cryptoStream))
{
writer.Write(input.Identificador);
writer.Flush();
}
}
encryptedIdentifierByte = stream.ToArray();
}
using (MemoryStream stream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(stream, encrypter, CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(cryptoStream))
{
writer.Write(input.Senha);
writer.Flush();
}
}
encryptedPasswordByte = stream.ToArray();
}
encryptedIdentifier = Convert.ToBase64String(encryptedIdentifierByte);
encryptedPassword = Convert.ToBase64String(encryptedPasswordByte);
passwordTemplate encryptedPasswordTemplate = new passwordTemplate(encryptedIdentifier, encryptedPassword);
return encryptedPasswordTemplate;
}
catch(Exception e)
{
System.Console.WriteLine("Erro: " + e);
return null;
}
}
public static void WriteToDatabase (ICryptoTransform encrypter, passwordTemplate input)
{
passwordTemplate passwordInfo = Encrypt(encrypter, input);
SQLITEAPI.Interface.TableCommand("addvalue", "encryptedPasswords", "encIdentifier, encPassword", $"'{passwordInfo.Identificador}', '{passwordInfo.Senha}'");
}
static string DecryptAll(ICryptoTransform decrypter)
{
SQLITEAPI.Interface.TableCommand("create", "passwords", "Id INTEGER PRIMARY KEY AUTOINCREMENT, Identifier TEXT NOT NULL, Password TEXT NOT NULL");
for (int i = 1; i <= 4; i++)
{
SQLITEAPI.Interface.TableCommand("getvalue", "encIdentifier", "encryptedPasswords", i.ToString());
byte[] inputIdentifier = Convert.FromBase64String(SQLITEAPI.Interface.GetValueResult ?? "");
SQLITEAPI.Interface.TableCommand("getvalue", "encPassword", "encryptedPasswords", i.ToString());
byte[] inputPassword = Convert.FromBase64String(SQLITEAPI.Interface.GetValueResult ?? "");
using (MemoryStream stream = new MemoryStream(inputIdentifier))
{
using (CryptoStream cryptoStream = new CryptoStream(stream, decrypter, CryptoStreamMode.Read))
{
using (StreamReader reader = new StreamReader(cryptoStream))
{
decryptedIdentifier = reader.ReadToEnd();
}
}
}
using (MemoryStream stream = new MemoryStream(inputPassword))
{
using (CryptoStream cryptoStream = new CryptoStream(stream, decrypter, CryptoStreamMode.Read))
{
using (StreamReader reader = new StreamReader(cryptoStream))
{
decryptedPassword = reader.ReadToEnd();
}
}
}
SQLITEAPI.Interface.TableCommand("addvalue", "passwords", "Identifier, Password", $"'{decryptedIdentifier}', '{decryptedPassword}'");
}
SQLITEAPI.Interface.TableCommand("view", "passwords");
SQLITEAPI.Interface.TableCommand("destroy", "passwords");
return decrypted;
}
}