diff --git a/CASA/micasad/lss/CASACrypto.cs b/CASA/micasad/lss/CASACrypto.cs index a9d81877..efa243d1 100644 --- a/CASA/micasad/lss/CASACrypto.cs +++ b/CASA/micasad/lss/CASACrypto.cs @@ -291,18 +291,18 @@ namespace sscs.crypto { FileStream fsHash = null; - byte[] hash = null; + byte[] hash = null; try { - SHA256 shaM = new SHA256Managed(); - hash = shaM.ComputeHash(baPasscode); + SHA256 shaM = new SHA256Managed(); + hash = shaM.ComputeHash(baPasscode); - fsHash = new FileStream(fileName, FileMode.Create); - File.SetAttributes(fileName, FileAttributes.Hidden); + fsHash = new FileStream(fileName, FileMode.Create); + File.SetAttributes(fileName, FileAttributes.Hidden); - fsHash.Write(hash, 0, hash.Length); - fsHash.Flush(); + fsHash.Write(hash, 0, hash.Length); + fsHash.Flush(); } catch(Exception e) { @@ -530,15 +530,17 @@ namespace sscs.crypto try { if(File.Exists(fileName)) - File.Delete(fileName); + File.Delete(fileName); + byte[] baKey = Generate16ByteKeyFromString(passwd, fileName, false, true); //Get an encryptor. RijndaelManaged myRijndael = new RijndaelManaged(); - ICryptoTransform encryptor; + ICryptoTransform encryptor; + 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); csEncrypt = new CryptoStream(fsEncrypt, encryptor, @@ -888,25 +890,22 @@ namespace sscs.crypto private static byte[] GenerateAndSaveIV(string sFileName, RijndaelManaged theRiManaged) { byte[] baIV = null; - if ( theRiManaged != null ) - { - theRiManaged.GenerateIV(); - baIV = theRiManaged.IV; - } - else - { - RandomNumberGenerator rng = RandomNumberGenerator.Create (); - baIV = new byte [16]; - rng.GetBytes (baIV); - } + if ( theRiManaged != null ) + { + theRiManaged.GenerateIV(); + baIV = theRiManaged.IV; + } + else + { + RandomNumberGenerator rng = RandomNumberGenerator.Create (); + baIV = new byte [16]; + rng.GetBytes (baIV); + } try { - if (File.Exists(sFileName + ".IV")) - File.Delete(sFileName + ".IV"); - // 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.Flush(); fs.Close(); diff --git a/CASA/micasad/lss/Rfc2898DeriveBytes.cs b/CASA/micasad/lss/Rfc2898DeriveBytes.cs index f1c4f585..7e62f996 100644 --- a/CASA/micasad/lss/Rfc2898DeriveBytes.cs +++ b/CASA/micasad/lss/Rfc2898DeriveBytes.cs @@ -58,6 +58,7 @@ using System.IO; using System.Security.Cryptography; using sscs.lss; +using sscs.common; namespace sscs.crypto { @@ -154,15 +155,18 @@ namespace sscs.crypto { // save salt try { - FileStream fs = new FileStream(sFilepath + ".salt", FileMode.Create); + + FileStream fs = new FileStream(sFilepath + ".salt", FileMode.OpenOrCreate); fs.Write(randomSalt, 0, randomSalt.Length); fs.Flush(); - fs.Close(); + fs.Close(); + File.SetAttributes(sFilepath + ".salt", FileAttributes.Hidden); + } catch (Exception e) { - + CSSSLogger.DbgLog(e.ToString()); } return randomSalt; @@ -181,7 +185,7 @@ namespace sscs.crypto { } catch (Exception e) { - + CSSSLogger.DbgLog(e.ToString()); } return baSalt; }