File Based User and password system

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thomashurlburt
    New Member
    • Feb 2013
    • 6

    File Based User and password system

    i can't seem to get my program to save the Set.savv file and encrypt it and decrypt it when user logs back on to there virtual file on a CD and writing it to the disk its self and closing the session
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    We would need to see the code you're using.

    Comment

    • thomashurlburt
      New Member
      • Feb 2013
      • 6

      #3
      okay let me copy it down for you

      Comment

      • thomashurlburt
        New Member
        • Feb 2013
        • 6

        #4
        Code:
          private void EncryptFile(string inputFile, string outputFile)
                {
        
                    try
                    {
                        string password = "H#GH!QK&"; // Your Key Here
                        UnicodeEncoding UE = new UnicodeEncoding();
                        byte[] key = UE.GetBytes(password);
        
                        string cryptFile = outputFile;
                        FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
        
                        RijndaelManaged RMCrypto = new RijndaelManaged();
        
                        CryptoStream cs = new CryptoStream(fsCrypt,
                            RMCrypto.CreateEncryptor(key, key),
                            CryptoStreamMode.Write);
        
                        FileStream fsIn = new FileStream(inputFile, FileMode.Open);
                        int data;
                        while ((data = fsIn.ReadByte()) != -1)
                            cs.WriteByte((byte)data);
        
        
                        fsIn.Close();
                        cs.Close();
                        fsCrypt.Close();
                    }
                    catch
                    {
        
                    }
                }
                ///<summary>
                /// Steve Lydford - 12/05/2008.
                ///
                /// Decrypts a file using Rijndael algorithm.
                ///</summary>
                ///<param name="inputFile"></param>
                ///<param name="outputFile"></param>
                private void DecryptFile(string inputFile, string outputFile)
                {
        
                    {
                        string password = "H#GH!QK&"; // Your Key Here
        
                        UnicodeEncoding UE = new UnicodeEncoding();
                        byte[] key = UE.GetBytes(password);
        
                        FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
        
                        RijndaelManaged RMCrypto = new RijndaelManaged();
        
                        CryptoStream cs = new CryptoStream(fsCrypt,
                            RMCrypto.CreateDecryptor(key, key),
                            CryptoStreamMode.Read);
        
                        FileStream fsOut = new FileStream(outputFile, FileMode.Create);
        
                        int data;
                        while ((data = cs.ReadByte()) != -1)
                            fsOut.WriteByte((byte)data);
        
                        fsOut.Close();
                        cs.Close();
                        fsCrypt.Close();
        
                    }
                }
            }
        }

        Comment

        • thomashurlburt
          New Member
          • Feb 2013
          • 6

          #5
          Code:
          private void Login(object sender, EventArgs e)
                  {
                      string location = "Tri//Accounts//" + textBox1.Text + "//" + "Set.sav";
                      string pingGPS="Tri//Accounts//"+textBox1.Text+"//"+"Set.savv";
                      if (Directory.Exists("Tri"))
                      {
                          if (File.Exists("Tri//Accounts//" + textBox1.Text+"//"+pingGPS)&File.Exists(location))
                          {
                              DecryptFile(pingGPS, location);
                             
                              StreamReader read = new StreamReader(pingGPS);
                              var reads = read.ReadLine();
                              if (reads == textBox2.Text)
                              {
                              }
                              else
                              {
                                  MessageBox.Show("wrong password please try again");
                              }
          
                          }
                          else
                          {
                              MessageBox.Show(pingGPS+"........."+location+"....."+"Does not exist");
                          }
          
                      }
                      else
                      {
                          MessageBox.Show("Can't seem to find[" + textBox1.Text + "]");
          
                      }
          
          
                  
                     
                  }
          
          
                 
                  private void timer1_Tick(object sender, EventArgs e)
                  {
                      
                      timer1.Interval = 100;
                      string TriAccount = "Tri//Accounts//" + textBox1.Text;
          
                      string all = TriAccount + "//" + "Set.sav";
                      string all2 = TriAccount + "//" + "Set.savv";
                      //creating the account and saving it and looking for existing 
          
          
                      toolStripProgressBar1.Increment(+1);
                      if (toolStripProgressBar1.Value == 1)
                      {
                          label1.Text = "Generating Key";
                          //sSecretKey = safe.GennerateKey();
                      }
                      if (toolStripProgressBar1.Value == 5)
                      {
                          label1.Text = "Creating account";
                          Properties.Settings.Default.Folder = all;
                          Properties.Settings.Default.Username = all2;
                          Properties.Settings.Default.Save();
                          Directory.CreateDirectory(TriAccount);
                          
                      }
                      if (toolStripProgressBar1.Value == 10)
                      {
                          Main();
                          label1.Text = "Writing personal use data";
                          StreamWriter write = new StreamWriter(all);
                          write.WriteLine(textBox2.Text);
                          write.Flush();
                          write.Close();
                     
                      }
                      if (toolStripProgressBar1.Value == 15)
                      {
          
                          
                         /* FileInfo fileinfo = new FileInfo(TriAccount);
                          FileSecurity accesscontrol = fileinfo.GetAccessControl();
                          accesscontrol.AddAccessRule(new FileSystemAccessRule(TriAccount, FileSystemRights.FullControl, AccessControlType.Allow));
                          fileinfo.SetAccessControl(accesscontrol); */
                      }
          
                      if (toolStripProgressBar1.Value == 20)
                      {
                          Main();
                          label1.Text = "Applying key";
                          GCHandle gch = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned);
                          EncryptFile(TriAccount, all);
                          //safe.EncrytFile(TriAccount, all, sSecretKey);
                         
                      }
                      stat.Text = toolStripProgressBar1.ToString();
                      if (toolStripProgressBar1.Value == 100)
                      {
                          timer1.Stop();
                          MessageBox.Show("Account Created,TriBot needs to Restart");
                          Application.Restart();
                      }
                  }
          
          
                  
                 
                  private void button3_Click(object sender, EventArgs e)
                  {
                      Main();
                      string TriAccount = "Tri//Accounts//" + textBox1.Text;
                      
                      if (Directory.Exists(TriAccount))
                      {
                          MessageBox.Show("account exists please provide the password"); 
                      }
                      else
                      {
                          MessageBox.Show("This may take up to a few mintues", "Creating Account");
                          timer1.Start();
                      }
                  }
                  public void Main()
                  {
                      string TriAccount = "Tri//Accounts//" + textBox1.Text;
                      string triaccountkey = TriAccount + "//" + "Set.savv";
                      string keydone = TriAccount + "//" + "Set.sav";
          
                      try
                      {
                          string fileName = keydone ;
          
                          Console.WriteLine("Adding access control entry for "
                              + fileName);
          
                          // Add the access control entry to the file.
                          AddFileSecurity(fileName, TriAccount,
                              FileSystemRights.FullControl, AccessControlType.Allow);
          
                          Console.WriteLine("Removing access control entry from "
                              + fileName);
          
                          // Remove the access control entry from the file.
                          RemoveFileSecurity(fileName, TriAccount,
                              FileSystemRights.ReadData, AccessControlType.Allow);
          
                          Console.WriteLine("Done.");
                      }
                      catch (Exception e)
                      {
                          Console.WriteLine(e);
                      }
                  }
          
                  // Adds an ACL entry on the specified file for the specified account. 
                  public static void AddFileSecurity(string fileName, string account,
                      FileSystemRights rights, AccessControlType controlType)
                  {
          
          
                      // Get a FileSecurity object that represents the 
                      // current security settings.
                      FileSecurity fSecurity = File.GetAccessControl(fileName);
          
                      // Add the FileSystemAccessRule to the security settings.
                      fSecurity.AddAccessRule(new FileSystemAccessRule(account,
                          rights, controlType));
          
                      // Set the new access settings.
                      File.SetAccessControl(fileName, fSecurity);
          
          }
          
                  // Removes an ACL entry on the specified file for the specified account. 
                  public static void RemoveFileSecurity(string fileName, string account,
                      FileSystemRights rights, AccessControlType controlType)
                  {
          
                      // Get a FileSecurity object that represents the 
                      // current security settings.
                      FileSecurity fSecurity = File.GetAccessControl(fileName);
          
                      // Remove the FileSystemAccessRule from the security settings.
                      fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
                          rights, controlType));
          
                      // Set the new access settings.
                      File.SetAccessControl(fileName, fSecurity);
          
                  }
          
              }
          }

          Comment

          • thomashurlburt
            New Member
            • Feb 2013
            • 6

            #6
            Code:
            ;
            using System.Security;
            using System.Security.Cryptography;
            using System.Runtime.InteropServices;
            using System.Security.AccessControl;
            
            namespace TriLockerBot
            {
            
            
                public partial class Form1 : Form
                {
            
                    Safe safe = new Safe();
                    public Form1()
                    {
                        InitializeComponent();
                    }
                    string sSecretKey;
            
                    private void EncryptFile(string inputFile, string outputFile)
                    {
            
                        try
                        {
                            string password = "H@JPK09"; // Your Key Here
                            UnicodeEncoding UE = new UnicodeEncoding();
                            byte[] key = UE.GetBytes(password);
            
                            string cryptFile = outputFile;
                            FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
            
                            RijndaelManaged RMCrypto = new RijndaelManaged();
            
                            CryptoStream cs = new CryptoStream(fsCrypt,
                                RMCrypto.CreateEncryptor(key, key),
                                CryptoStreamMode.Write);
            
                            FileStream fsIn = new FileStream(inputFile, FileMode.Open);
                            int data;
                            while ((data = fsIn.ReadByte()) != -1)
                                cs.WriteByte((byte)data);
            
            
                            fsIn.Close();
                            cs.Close();
                            fsCrypt.Close();
                        }
                        catch
                        {
            
                        }
                    }
                    ///<summary>
                    /// Steve Lydford - 12/05/2008.
                    ///
                    /// Decrypts a file using Rijndael algorithm.
                    ///</summary>
                    ///<param name="inputFile"></param>
                    ///<param name="outputFile"></param>
                    private void DecryptFile(string inputFile, string outputFile)
                    {
            
                        {
                            string password = "H@JPK09"; // Your Key Here
            
                            UnicodeEncoding UE = new UnicodeEncoding();
                            byte[] key = UE.GetBytes(password);
                            if (File.Exists("Tri//Account//" + textBox1.Text + "//" + "Set.savv"))
                            {
                                FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
            
                                RijndaelManaged RMCrypto = new RijndaelManaged();
            
                                CryptoStream cs = new CryptoStream(fsCrypt,
                                    RMCrypto.CreateDecryptor(key, key),
                                    CryptoStreamMode.Read);
            
                                FileStream fsOut = new FileStream(outputFile, FileMode.Create);
            
                                int data;
                                while ((data = cs.ReadByte()) != -1)
                                    fsOut.WriteByte((byte)data);
            
                                fsOut.Close();
                                cs.Close();
                                fsCrypt.Close();
                            }
                            else
                            {
                                MessageBox.Show("Error");
                            }
            
                        }
                    }
            
            
            
            
                    private void Form1_Load(object sender, EventArgs e)
                    {
            
                        vision.Text = "Ver:" + Application.ProductVersion;
                    }
            
                    private void Login(object sender, EventArgs e)
                    {
                        string location = "Tri//Accounts//" + textBox1.Text + "//" + "Set.sav";
                        string pingGPS="Tri//Accounts//"+textBox1.Text+"//"+"Set.savv";
                        if (Directory.Exists("Tri"))
                        {
                            if (File.Exists("Tri//Accounts//" + textBox1.Text+"//"+pingGPS)&File.Exists(location))
                            {
                                DecryptFile(pingGPS, location);
                               
                                StreamReader read = new StreamReader(pingGPS);
                                var reads = read.ReadLine();
                                if (reads == textBox2.Text)
                                {
                                }
                                else
                                {
                                    MessageBox.Show("wrong password please try again");
                                }
            
                            }
                            else
                            {
                                MessageBox.Show(pingGPS+"........."+location+"....."+"Does not exist");
                            }
            
                        }
                        else
                        {
                            MessageBox.Show("Can't seem to find[" + textBox1.Text + "]");
            
                        }
            
            
                    
                       
                    }
            
            
                   
                    private void timer1_Tick(object sender, EventArgs e)
                    {
                        
                        timer1.Interval = 100;
                        string TriAccount = "Tri//Accounts//" + textBox1.Text;
            
                        string all = TriAccount + "//" + "Set.sav";
                        string all2 = TriAccount + "//" + "Set.savv";
                        //creating the account and saving it and looking for existing 
            
            
                        toolStripProgressBar1.Increment(+1);
                        if (toolStripProgressBar1.Value == 1)
                        {
                            label1.Text = "Generating Key";
                            //sSecretKey = safe.GennerateKey();
                        }
                        if (toolStripProgressBar1.Value == 5)
                        {
                            label1.Text = "Creating account";
                            Properties.Settings.Default.Folder = all;
                            Properties.Settings.Default.Username = all2;
                            Properties.Settings.Default.Save();
                            Directory.CreateDirectory(TriAccount);
                            
                        }
                        if (toolStripProgressBar1.Value == 10)
                        {
                            Main();
                            label1.Text = "Writing personal use data";
                            StreamWriter write = new StreamWriter(all);
                            write.WriteLine(textBox2.Text);
                            write.Flush();
                            write.Close();
                       
                        }
                        if (toolStripProgressBar1.Value == 15)
                        {
            
                            
                           /* FileInfo fileinfo = new FileInfo(TriAccount);
                            FileSecurity accesscontrol = fileinfo.GetAccessControl();
                            accesscontrol.AddAccessRule(new FileSystemAccessRule(TriAccount, FileSystemRights.FullControl, AccessControlType.Allow));
                            fileinfo.SetAccessControl(accesscontrol); */
                        }
            
                        if (toolStripProgressBar1.Value == 20)
                        {
                            Main();
                            label1.Text = "Applying key";
                            GCHandle gch = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned);
                            EncryptFile(TriAccount, all);
                            //safe.EncrytFile(TriAccount, all, sSecretKey);
                           
                        }
                        stat.Text = toolStripProgressBar1.ToString();
                        if (toolStripProgressBar1.Value == 100)
                        {
                            timer1.Stop();
                            MessageBox.Show("Account Created,TriBot needs to Restart");
                            Application.Restart();
                        }
                    }
            
            
                    
                   
                    private void Register(object sender, EventArgs e)
                    {
                        Main();
                        string TriAccount = "Tri//Accounts//" + textBox1.Text;
                        
                        if (Directory.Exists(TriAccount))
                        {
                            MessageBox.Show("account exists please provide the password"); 
                        }
                        else
                        {
                            MessageBox.Show("This may take up to a few mintues", "Creating Account");
                            timer1.Start();
                        }
                    }
                    public void Main()
                    {
                        string TriAccount = "Tri//Accounts//" + textBox1.Text;
                        string triaccountkey = TriAccount + "//" + "Set.savv";
                        string keydone = TriAccount + "//" + "Set.sav";
            
                        try
                        {
                            string fileName = keydone ;
            
                            Console.WriteLine("Adding access control entry for "
                                + fileName);
            
                            // Add the access control entry to the file.
                            AddFileSecurity(fileName, TriAccount,
                                FileSystemRights.FullControl, AccessControlType.Allow);
            
                            Console.WriteLine("Removing access control entry from "
                                + fileName);
            
                            // Remove the access control entry from the file.
                            RemoveFileSecurity(fileName, TriAccount,
                                FileSystemRights.ReadData, AccessControlType.Allow);
            
                            Console.WriteLine("Done.");
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }
                    }
            
                    // Adds an ACL entry on the specified file for the specified account. 
                    public static void AddFileSecurity(string fileName, string account,
                        FileSystemRights rights, AccessControlType controlType)
                    {
            
            
                        // Get a FileSecurity object that represents the 
                        // current security settings.
                        FileSecurity fSecurity = File.GetAccessControl(fileName);
            
                        // Add the FileSystemAccessRule to the security settings.
                        fSecurity.AddAccessRule(new FileSystemAccessRule(account,
                            rights, controlType));
            
                        // Set the new access settings.
                        File.SetAccessControl(fileName, fSecurity);
            
            }
            
                    // Removes an ACL entry on the specified file for the specified account. 
                    public static void RemoveFileSecurity(string fileName, string account,
                        FileSystemRights rights, AccessControlType controlType)
                    {
            
                        // Get a FileSecurity object that represents the 
                        // current security settings.
                        FileSecurity fSecurity = File.GetAccessControl(fileName);
            
                        // Remove the FileSystemAccessRule from the security settings.
                        fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
                            rights, controlType));
            
                        // Set the new access settings.
                        File.SetAccessControl(fileName, fSecurity);
            
                    }
            
                }
            }

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              Yeah... I'm not about to read 500 lines of code.

              Are you getting errors? What are the errors? Which lines cause the errors?

              Are you not getting errors but it's not behaving in the way you expect? Which lines of code is executing incorrectly? What behavior is resulting? What is supposed to happen instead?

              Comment

              • thomashurlburt
                New Member
                • Feb 2013
                • 6

                #8
                no im not getting errors. it runs fine but it wont create the encryption file and save the password from the textBox2.text and encrypt the password and then when the user types the same password in again it doesn't recognize it and cant decrypt. and I created a fail catch point to recreate the whole thing but it doesn't want to work. and I've tried everything

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  Like I said earlier, I'm not trying to read 500 lines of code to find the one you're talking about. Please tell me which block of code it is and which line number you're referring to.

                  Comment

                  Working...