private string HeaderToString(string password, string mode, string keySize, List <string> selectedUsers, string IV) { string header = ""; header += "EncryptedFileHeader|||Algorithm|||AES|||KeySize|||" + keySize.ToString() + "|||BlockSize|||128|||CipherMode|||" + mode.ToString() + "|||IV|||"; header += IV + "|||ApprovedUsers|||"; foreach (var user in selectedUsers) { string dirpath = @"..\..\UsersFiles\"; dirpath += user + @"\PUGB\PUGB.txt"; //read public key from file var publicKeyString = File.ReadAllText(dirpath); //encrypt sesion key and return string string passwordEncrypted = RSAHandle.EncryptMessage(publicKeyString, password); header += "User|||" + user + "|||SessionKey|||"; //add encrypted session key to Header header += passwordEncrypted + "|||"; //////////////KURWA zakodowany Teks może mieć ||| i zjebac dekodowanie pliku } header += "Done"; return(header); }
private void CreateUser(string login, string password) ///////Creates a user by adding his name to userlist.txt and creating his user folder with hashed pass and salt { //////////also create RSA keys string path = @"..\..\UsersFiles\UserList.txt"; string dirpath = @"..\..\UsersFiles\"; dirpath += login; try { using (StreamWriter sw = File.AppendText(path)) ///////adding to userlist { sw.WriteLine(login); } byte[] salt1 = new byte[8]; using (RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider()) { // Fill the array with a random value. rngCsp.GetBytes(salt1); } System.IO.Directory.CreateDirectory(dirpath); string passpath = dirpath + "\\\\paswd.txt"; using (StreamWriter sw = File.CreateText(passpath)) { //create the file for password sw.WriteLine(SHA2salted.GenerateSHA512String(password, salt1)); } string saltpath = dirpath + "\\\\salt.txt"; using (StreamWriter sw = File.CreateText(saltpath)) //create the file for salt { sw.WriteLine(SHA2salted.GetStringFromHash(salt1)); } string who = "Hi " + login; MessageBox.Show("User Created", who, MessageBoxButton.OK, MessageBoxImage.None); //lets take a new CSP with a new 2048 bit rsa key pair var csp = new RSACryptoServiceProvider(2048); //how to get the private key var privKey = csp.ExportParameters(true); string privKeyString; //we need some buffer var sw1 = new System.IO.StringWriter(); //we need a serializer var xs1 = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters)); //serialize the key into the stream xs1.Serialize(sw1, privKey); //get the string from the stream privKeyString = sw1.ToString(); var sw2 = new System.IO.StringWriter(); //we need a serializer var xs2 = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters)); //and the public key ... var pubKey = csp.ExportParameters(false); string pubKeyString; //serialize the key into the stream xs2.Serialize(sw2, pubKey); //get the string from the stream pubKeyString = sw2.ToString(); string dirpathPub = dirpath + @"\PUGB"; System.IO.Directory.CreateDirectory(dirpathPub); dirpathPub += @"\PUGB.txt"; File.WriteAllText(dirpathPub, pubKeyString); //using (StreamWriter sw = File.CreateText(dirpathPub)) //{ //create the file for publicKey // sw.WriteLine(pubKeyString); //} string dirpathPriv = dirpath + @"\PRIV"; System.IO.Directory.CreateDirectory(dirpathPriv); dirpathPriv += @"\PRIV.txt"; RSAHandle.EncryptPrivate(privKeyString, SHA2salted.GenerateSHA512String(password, salt1), dirpathPriv); string resultPrivRSA = RSAHandle.DecryptPrivate(SHA2salted.GenerateSHA512String(password, salt1), dirpathPriv); var key = RSAHandle.StringToKey(resultPrivRSA); //we want to decrypt, therefore we need a csp and load our private key var csp2 = new RSACryptoServiceProvider(); csp2.ImportParameters(key); csp.PersistKeyInCsp = false; this.Close(); } catch (Exception) { throw; } }
public void AES_Decrypt(string originFile, string whereToSave, string logedUser, string aPanDoKogo) { FileStream fsCrypt = new FileStream(originFile, FileMode.Open); //odczytaj rozmiar naglowka byte[] stringLenghtAsBytes = new byte[4]; fsCrypt.Read(stringLenghtAsBytes, 0, stringLenghtAsBytes.Length); string result = System.Text.Encoding.ASCII.GetString(stringLenghtAsBytes); int paresdResult = Int32.Parse(result); //odczytaj naglowek byte[] stringAsBytes = new byte[paresdResult]; fsCrypt.Read(stringAsBytes, 0, stringAsBytes.Length); string header = System.Text.Encoding.ASCII.GetString(stringAsBytes); string[] headerArray; //headerArray = header.Split('|'); headerArray = header.Split(new string[] { "|||" }, StringSplitOptions.None); //na podstawie pozycji w nagłówku uzupełnij pola sprawdz HeaderToString() żeby wiedzieć co gdzie leży string keySize = headerArray[4]; string mode = headerArray[8]; string IVString = headerArray[10]; byte[] IV = System.Text.Encoding.ASCII.GetBytes(IVString); //11 ApprovedUsers 12User 13username 14SessionKey 15paswd 16 User2 17 username2 18Sessionkey string password = ""; int i = 13; //sprawdz czy znajduje się na liście odbiorców while (!headerArray[i].Equals("Done")) { if (headerArray[i].Equals(logedUser)) { string pathToPass = @"..\..\UsersFiles\" + logedUser + @"\paswd.txt"; string userpass = ""; using (StreamReader sr = File.OpenText(pathToPass)) { userpass = sr.ReadLine(); } string dirpathPriv = @"..\..\UsersFiles\" + logedUser + @"\PRIV\PRIV.txt"; string resultPrivRSA = RSAHandle.DecryptPrivate(userpass, dirpathPriv); password = RSAHandle.DecryptMessage(resultPrivRSA, headerArray[i + 2]); break; } i += 4; } // sprawdź czy wybraliśmy siebie jako odbiorcę if (!logedUser.Equals(aPanDoKogo)) { password = "******"; } //password = UTF8toASCII(password); //odczytaj sol byte[] salt = new byte[32]; fsCrypt.Read(salt, 0, salt.Length); // Set your salt here, change it to meet your flavor: // The salt bytes must be at least 8 bytes. //password bytes form string password byte[] passwordBytes = System.Text.Encoding.ASCII.GetBytes(password); using (RijndaelManaged AES = new RijndaelManaged()) { if (keySize.Contains("128")) { AES.KeySize = 128; } if (keySize.Contains("192")) { AES.KeySize = 192; } if (keySize.Contains("256")) { AES.KeySize = 256; } AES.BlockSize = 128; AES.Padding = PaddingMode.PKCS7; var key = new Rfc2898DeriveBytes(passwordBytes, salt, 10000); AES.Key = key.GetBytes(AES.KeySize / 8); AES.IV = IV; if (mode.Contains("CBC")) { AES.Mode = CipherMode.CBC; } if (mode.Contains("ECB")) { AES.Mode = CipherMode.ECB; } if (mode.Contains("CFB")) { AES.Mode = CipherMode.CFB; } if (mode.Contains("OFB")) { AES.Mode = CipherMode.OFB; } CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateDecryptor(), CryptoStreamMode.Read); FileStream fsOut = new FileStream(whereToSave, FileMode.Create); int read; byte[] buffer = new byte[1048576]; try { var mainWin = Application.Current.Windows.Cast <Window>().FirstOrDefault(window => window is MainWindow) as MainWindow; mainWin.ProBar.Minimum = 0; mainWin.ProBar.Maximum = new System.IO.FileInfo(originFile).Length; mainWin.ProBar.Dispatcher.Invoke(() => mainWin.ProBar.Value = 0, DispatcherPriority.Background); double proggres = 0; while ((read = cs.Read(buffer, 0, buffer.Length)) > 0) { fsOut.Write(buffer, 0, read); proggres += read; mainWin.ProBar.Dispatcher.Invoke(() => mainWin.ProBar.Value = proggres, DispatcherPriority.Background); } mainWin.ProBar.Dispatcher.Invoke(() => mainWin.ProBar.Value = mainWin.ProBar.Maximum, DispatcherPriority.Background); } catch (Exception ex) { MessageBox.Show("Szyfracja nie wyszła " + ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Warning); } finally { cs.Close(); fsOut.Close(); fsCrypt.Close(); } } }