Bug 267542. Creating salt file threw an exception. Changed mode of file creation.

This commit is contained in:
Jim Norman 2007-04-27 15:07:25 +00:00
parent bd7211dbe7
commit ec2cb08082
2 changed files with 32 additions and 29 deletions

View File

@ -291,18 +291,18 @@ namespace sscs.crypto
{ {
FileStream fsHash = null; FileStream fsHash = null;
byte[] hash = null; byte[] hash = null;
try try
{ {
SHA256 shaM = new SHA256Managed(); SHA256 shaM = new SHA256Managed();
hash = shaM.ComputeHash(baPasscode); hash = shaM.ComputeHash(baPasscode);
fsHash = new FileStream(fileName, FileMode.Create); fsHash = new FileStream(fileName, FileMode.Create);
File.SetAttributes(fileName, FileAttributes.Hidden); File.SetAttributes(fileName, FileAttributes.Hidden);
fsHash.Write(hash, 0, hash.Length); fsHash.Write(hash, 0, hash.Length);
fsHash.Flush(); fsHash.Flush();
} }
catch(Exception e) catch(Exception e)
{ {
@ -530,15 +530,17 @@ namespace sscs.crypto
try try
{ {
if(File.Exists(fileName)) if(File.Exists(fileName))
File.Delete(fileName); File.Delete(fileName);
byte[] baKey = Generate16ByteKeyFromString(passwd, fileName, false, true); byte[] baKey = Generate16ByteKeyFromString(passwd, fileName, false, true);
//Get an encryptor. //Get an encryptor.
RijndaelManaged myRijndael = new RijndaelManaged(); RijndaelManaged myRijndael = new RijndaelManaged();
ICryptoTransform encryptor; ICryptoTransform encryptor;
encryptor = myRijndael.CreateEncryptor(baKey, GenerateAndSaveIV(fileName, myRijndael)); encryptor = myRijndael.CreateEncryptor(baKey, GenerateAndSaveIV(fileName, myRijndael));
//Encrypt the data to a file //Encrypt the data to a file
fsEncrypt = new FileStream(fileName,FileMode.Create); fsEncrypt = new FileStream(fileName,FileMode.Create);
csEncrypt = new CryptoStream(fsEncrypt, encryptor, csEncrypt = new CryptoStream(fsEncrypt, encryptor,
@ -888,25 +890,22 @@ namespace sscs.crypto
private static byte[] GenerateAndSaveIV(string sFileName, RijndaelManaged theRiManaged) private static byte[] GenerateAndSaveIV(string sFileName, RijndaelManaged theRiManaged)
{ {
byte[] baIV = null; byte[] baIV = null;
if ( theRiManaged != null ) if ( theRiManaged != null )
{ {
theRiManaged.GenerateIV(); theRiManaged.GenerateIV();
baIV = theRiManaged.IV; baIV = theRiManaged.IV;
} }
else else
{ {
RandomNumberGenerator rng = RandomNumberGenerator.Create (); RandomNumberGenerator rng = RandomNumberGenerator.Create ();
baIV = new byte [16]; baIV = new byte [16];
rng.GetBytes (baIV); rng.GetBytes (baIV);
} }
try try
{ {
if (File.Exists(sFileName + ".IV"))
File.Delete(sFileName + ".IV");
// now save this // now save this
FileStream fs = new FileStream(sFileName + ".IV", FileMode.Create); FileStream fs = new FileStream(sFileName + ".IV", FileMode.OpenOrCreate);
fs.Write(baIV, 0, 16); fs.Write(baIV, 0, 16);
fs.Flush(); fs.Flush();
fs.Close(); fs.Close();

View File

@ -58,6 +58,7 @@ using System.IO;
using System.Security.Cryptography; using System.Security.Cryptography;
using sscs.lss; using sscs.lss;
using sscs.common;
namespace sscs.crypto { namespace sscs.crypto {
@ -154,15 +155,18 @@ namespace sscs.crypto {
// save salt // save salt
try try
{ {
FileStream fs = new FileStream(sFilepath + ".salt", FileMode.Create);
FileStream fs = new FileStream(sFilepath + ".salt", FileMode.OpenOrCreate);
fs.Write(randomSalt, 0, randomSalt.Length); fs.Write(randomSalt, 0, randomSalt.Length);
fs.Flush(); fs.Flush();
fs.Close(); fs.Close();
File.SetAttributes(sFilepath + ".salt", FileAttributes.Hidden); File.SetAttributes(sFilepath + ".salt", FileAttributes.Hidden);
} }
catch (Exception e) catch (Exception e)
{ {
CSSSLogger.DbgLog(e.ToString());
} }
return randomSalt; return randomSalt;
@ -181,7 +185,7 @@ namespace sscs.crypto {
} }
catch (Exception e) catch (Exception e)
{ {
CSSSLogger.DbgLog(e.ToString());
} }
return baSalt; return baSalt;
} }