Security question
-
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?
-
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?
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 Mprivate 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
-
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 Mprivate 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
-
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.
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...
-
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...
-
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?