Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Security question

Security question

Scheduled Pinned Locked Moved C#
questioncsharpsecurityxml
6 Posts 3 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Omega501
    wrote on last edited by
    #1

    I'm looking at writing an app for storing passwords for the different mailing lists i'm on, with the data encrypted and stored in an xml file. I was wondering if there is a way (in c#) to get a password from a user, and use that as the key to decrypt the data. I've looked (briefly) through the System.Security.Cryptography namespace but it seems to mostly deal with keys (seperate files), but I would much prefer to input a password and use that as the decryption key - is this possible?

    J F 2 Replies Last reply
    0
    • O Omega501

      I'm looking at writing an app for storing passwords for the different mailing lists i'm on, with the data encrypted and stored in an xml file. I was wondering if there is a way (in c#) to get a password from a user, and use that as the key to decrypt the data. I've looked (briefly) through the System.Security.Cryptography namespace but it seems to mostly deal with keys (seperate files), but I would much prefer to input a password and use that as the decryption key - is this possible?

      J Offline
      J Offline
      jtmtv18
      wrote on last edited by
      #2

      this is what i use in my program to encrypt hole files. you can possibly use it for yours as well; inName = full path of the file to be encrypted(this file is only opened and not deleted...you will have to rewrite that part) outName : output...encrypted file KeySecretString (the password to encrypt with) to decrypt the file use the same code but replace : CryptoStream encStream = new CryptoStream(fout, tdes.CreateEncryptor(tdes.Key, tdes.IV), CryptoStreamMode.Write); with : CryptoStream encStream = new CryptoStream(fout, tdes.CreateDecryptor(tdes.Key, tdes.IV), CryptoStreamMode.Write); further more where you see the code DESCryptoServiceProvider des = new DESCryptoServiceProvider(); you can replace the DES part with RC2 to get RC2 encryption / decryption instead. Happy Coding... Jesse M private void DecryptData(String inName, String outName, string KeySecretString) { //Create the file streams to handle the input and output files. FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read); FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write); fout.SetLength(0); string FileN = Path.GetFileName(outName); InformationFile.Text = FileN; InformationFile.Update(); //Create variables to help with read and write. byte[] bin = new byte[buffersize]; //This is intermediate storage for the encryption. long rdlen = 0; //This is the total number of bytes written. long totlen = fin.Length; //This is the total length of the input file. int len; //This is the number of bytes to be written at a time. DESCryptoServiceProvider tdes = new DESCryptoServiceProvider(); tdes.IV = ASCIIEncoding.ASCII.GetBytes(KeySecretString); tdes.Key =ASCIIEncoding.ASCII.GetBytes(KeySecretString); CryptoStream encStream = new CryptoStream(fout, tdes.CreateDecryptor(tdes.Key, tdes.IV), CryptoStreamMode.Write); Console.WriteLine("Encrypting..."); //Read from the input file, then encrypt and write to the output file. while(rdlen < totlen) { len = fin.Read(bin, 0, buffersize); encStream.Write(bin, 0, len); rdlen = rdlen + len; Console.WriteLine("{0} bytes processed", rdlen); } fout.Flush(); fin.Flush(); encStream.Flush(); encStream.Close(); fout.Close(); fi

      O 1 Reply Last reply
      0
      • J jtmtv18

        this is what i use in my program to encrypt hole files. you can possibly use it for yours as well; inName = full path of the file to be encrypted(this file is only opened and not deleted...you will have to rewrite that part) outName : output...encrypted file KeySecretString (the password to encrypt with) to decrypt the file use the same code but replace : CryptoStream encStream = new CryptoStream(fout, tdes.CreateEncryptor(tdes.Key, tdes.IV), CryptoStreamMode.Write); with : CryptoStream encStream = new CryptoStream(fout, tdes.CreateDecryptor(tdes.Key, tdes.IV), CryptoStreamMode.Write); further more where you see the code DESCryptoServiceProvider des = new DESCryptoServiceProvider(); you can replace the DES part with RC2 to get RC2 encryption / decryption instead. Happy Coding... Jesse M private void DecryptData(String inName, String outName, string KeySecretString) { //Create the file streams to handle the input and output files. FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read); FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write); fout.SetLength(0); string FileN = Path.GetFileName(outName); InformationFile.Text = FileN; InformationFile.Update(); //Create variables to help with read and write. byte[] bin = new byte[buffersize]; //This is intermediate storage for the encryption. long rdlen = 0; //This is the total number of bytes written. long totlen = fin.Length; //This is the total length of the input file. int len; //This is the number of bytes to be written at a time. DESCryptoServiceProvider tdes = new DESCryptoServiceProvider(); tdes.IV = ASCIIEncoding.ASCII.GetBytes(KeySecretString); tdes.Key =ASCIIEncoding.ASCII.GetBytes(KeySecretString); CryptoStream encStream = new CryptoStream(fout, tdes.CreateDecryptor(tdes.Key, tdes.IV), CryptoStreamMode.Write); Console.WriteLine("Encrypting..."); //Read from the input file, then encrypt and write to the output file. while(rdlen < totlen) { len = fin.Read(bin, 0, buffersize); encStream.Write(bin, 0, len); rdlen = rdlen + len; Console.WriteLine("{0} bytes processed", rdlen); } fout.Flush(); fin.Flush(); encStream.Flush(); encStream.Close(); fout.Close(); fi

        O Offline
        O Offline
        Omega501
        wrote on last edited by
        #3

        The code looks interesting, I might be able to modify it to what I want. Instead of encrypting the whole file though, all I want to do is encrypt the data in the xml file.

        J 1 Reply Last reply
        0
        • O Omega501

          The code looks interesting, I might be able to modify it to what I want. Instead of encrypting the whole file though, all I want to do is encrypt the data in the xml file.

          J Offline
          J Offline
          jtmtv18
          wrote on last edited by
          #4

          im not gonna post the code agian...but the code utilized a file stream(inName)....you can save the xml file then encrypt it..but the problem is..for the millisecond that the file is saved on the hd and encryption hasnt got to it it is open. but you might beable to swap the FileStream with the XMLWriter (cant remeber the exsact name for it) try it..and the encryption stream might work fine with a xml stream ..... happy coding. Jesse M The Code Project Is Your Friend...

          O 1 Reply Last reply
          0
          • J jtmtv18

            im not gonna post the code agian...but the code utilized a file stream(inName)....you can save the xml file then encrypt it..but the problem is..for the millisecond that the file is saved on the hd and encryption hasnt got to it it is open. but you might beable to swap the FileStream with the XMLWriter (cant remeber the exsact name for it) try it..and the encryption stream might work fine with a xml stream ..... happy coding. Jesse M The Code Project Is Your Friend...

            O Offline
            O Offline
            Omega501
            wrote on last edited by
            #5

            Thanks fror your help - in this instance security isn't that high a priority - I'm only writing this for myself as an experiment

            1 Reply Last reply
            0
            • O Omega501

              I'm looking at writing an app for storing passwords for the different mailing lists i'm on, with the data encrypted and stored in an xml file. I was wondering if there is a way (in c#) to get a password from a user, and use that as the key to decrypt the data. I've looked (briefly) through the System.Security.Cryptography namespace but it seems to mostly deal with keys (seperate files), but I would much prefer to input a password and use that as the decryption key - is this possible?

              F Offline
              F Offline
              Furty
              wrote on last edited by
              #6

              This is something I see implemented incorrectly time and time again. The *correct* way to derive a key from a string password is using the System.Security.Cryptography.PasswordDeriveBytes method - it's usage is very straight forward.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups