Modifications of Poorna to fix SetMasterPassword() defect.
This commit is contained in:
parent
dc2d35ba49
commit
a9e5a67876
@ -34,6 +34,8 @@ namespace sscs.crypto
|
||||
byte[] IV, byte[] baMasterPasscode, string fileName)
|
||||
{
|
||||
bool bRet = false;
|
||||
FileStream fsEncrypt = null;
|
||||
CryptoStream csEncrypt = null;
|
||||
try
|
||||
{
|
||||
|
||||
@ -43,7 +45,7 @@ namespace sscs.crypto
|
||||
encryptor = myRijndael.CreateEncryptor(baMasterPasscode, baMasterPasscode);
|
||||
|
||||
//Encrypt the data to a file
|
||||
FileStream fsEncrypt = new FileStream(fileName, FileMode.Create);
|
||||
fsEncrypt = new FileStream(fileName, FileMode.Create);
|
||||
#if LINUX
|
||||
Mono.Unix.Syscall.chmod(fileName,Mono.Unix.FilePermissions.S_IRUSR | Mono.Unix.FilePermissions.S_IWUSR);
|
||||
#endif
|
||||
@ -54,12 +56,11 @@ namespace sscs.crypto
|
||||
fsEncrypt.Write(hash,0,hash.Length);
|
||||
fsEncrypt.Flush();
|
||||
|
||||
CryptoStream csEncrypt = new CryptoStream(fsEncrypt, encryptor, CryptoStreamMode.Write);
|
||||
csEncrypt = new CryptoStream(fsEncrypt, encryptor, CryptoStreamMode.Write);
|
||||
|
||||
//Write all data to the crypto stream and flush it.
|
||||
csEncrypt.Write(key, 0, key.Length);
|
||||
csEncrypt.FlushFinalBlock();
|
||||
fsEncrypt.Close();
|
||||
bRet = true;
|
||||
}
|
||||
catch(Exception e)
|
||||
@ -68,6 +69,10 @@ namespace sscs.crypto
|
||||
CSSSLogger.DbgLog("Unable to store the generated key");
|
||||
bRet = false;
|
||||
}
|
||||
if( fsEncrypt != null )
|
||||
fsEncrypt.Close();
|
||||
if( csEncrypt != null )
|
||||
csEncrypt.Close();
|
||||
return bRet;
|
||||
}
|
||||
|
||||
@ -75,6 +80,8 @@ namespace sscs.crypto
|
||||
string fileName )
|
||||
{
|
||||
byte[] baSavedKey = null;
|
||||
FileStream fsDecrypt = null;
|
||||
CryptoStream csDecrypt = null;
|
||||
try
|
||||
{
|
||||
if(!File.Exists(fileName))
|
||||
@ -89,17 +96,16 @@ namespace sscs.crypto
|
||||
RijndaelManaged myRijndael = new RijndaelManaged();
|
||||
ICryptoTransform decryptor = myRijndael.CreateDecryptor(baMasterPasscode, baMasterPasscode);
|
||||
//Now decrypt
|
||||
FileStream fsDecrypt = new FileStream(fileName, FileMode.Open);
|
||||
fsDecrypt = new FileStream(fileName, FileMode.Open);
|
||||
|
||||
byte[] storedHash = new byte[32];
|
||||
fsDecrypt.Read(storedHash,0,storedHash.Length);
|
||||
|
||||
CryptoStream csDecrypt = new CryptoStream(fsDecrypt, decryptor, CryptoStreamMode.Read);
|
||||
csDecrypt = new CryptoStream(fsDecrypt, decryptor, CryptoStreamMode.Read);
|
||||
baSavedKey = new byte[32];
|
||||
|
||||
//Read the data out of the crypto stream.
|
||||
csDecrypt.Read(baSavedKey, 0, baSavedKey.Length);
|
||||
fsDecrypt.Close();
|
||||
|
||||
SHA256 sha = new SHA256Managed();
|
||||
byte[] newHash = sha.ComputeHash(baSavedKey);
|
||||
@ -108,11 +114,11 @@ namespace sscs.crypto
|
||||
if(storedHash[i] != newHash[i])
|
||||
{
|
||||
CSSSLogger.DbgLog("Hash doesnot match");
|
||||
fsDecrypt.Close();
|
||||
csDecrypt.Close();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return baSavedKey;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
@ -120,12 +126,19 @@ namespace sscs.crypto
|
||||
CSSSLogger.DbgLog("Unable to get the stored key");
|
||||
baSavedKey = null;
|
||||
}
|
||||
if ( fsDecrypt != null )
|
||||
fsDecrypt.Close();
|
||||
|
||||
if( csDecrypt != null )
|
||||
csDecrypt.Close();
|
||||
return baSavedKey;
|
||||
}
|
||||
|
||||
internal static void EncryptDataAndWriteToFile(byte[] xmlData,
|
||||
byte[] key, string fileName)
|
||||
{
|
||||
FileStream fsEncrypt = null;
|
||||
CryptoStream csEncrypt = null;
|
||||
try
|
||||
{
|
||||
byte[] IV = new byte[16];
|
||||
@ -137,7 +150,7 @@ namespace sscs.crypto
|
||||
ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, IV);
|
||||
|
||||
//Encrypt the data to a file
|
||||
FileStream fsEncrypt = new FileStream(fileName, FileMode.Create);
|
||||
fsEncrypt = new FileStream(fileName, FileMode.Create);
|
||||
#if LINUX
|
||||
Mono.Unix.Syscall.chmod(fileName,Mono.Unix.FilePermissions.S_IRUSR | Mono.Unix.FilePermissions.S_IWUSR);
|
||||
#endif
|
||||
@ -148,24 +161,28 @@ namespace sscs.crypto
|
||||
fsEncrypt.Write(hash,0,hash.Length);
|
||||
fsEncrypt.Flush();
|
||||
|
||||
CryptoStream csEncrypt = new CryptoStream(fsEncrypt, encryptor, CryptoStreamMode.Write);
|
||||
csEncrypt = new CryptoStream(fsEncrypt, encryptor, CryptoStreamMode.Write);
|
||||
|
||||
//Write all data to the crypto stream and flush it.
|
||||
csEncrypt.Write(xmlData, 0, xmlData.Length);
|
||||
csEncrypt.FlushFinalBlock();
|
||||
fsEncrypt.Close();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
CSSSLogger.ExpLog(e.ToString());
|
||||
CSSSLogger.DbgLog("Encrypting and storing to file failed.");
|
||||
}
|
||||
if( fsEncrypt != null )
|
||||
fsEncrypt.Close();
|
||||
if( csEncrypt != null )
|
||||
csEncrypt.Close();
|
||||
}
|
||||
|
||||
internal static byte[] ReadFileAndDecryptData(byte[] key,
|
||||
string fileName)
|
||||
{
|
||||
FileStream fsDecrypt = null;
|
||||
CryptoStream csDecrypt = null;
|
||||
try
|
||||
{
|
||||
byte[] IV = new byte[16];
|
||||
@ -175,7 +192,6 @@ namespace sscs.crypto
|
||||
//Get a decryptor that uses the same key and IV as the encryptor.
|
||||
RijndaelManaged myRijndael = new RijndaelManaged();
|
||||
ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, IV);
|
||||
|
||||
if(!File.Exists(fileName))
|
||||
{
|
||||
return null;
|
||||
@ -186,7 +202,7 @@ namespace sscs.crypto
|
||||
byte[] storedHash = new byte[32];
|
||||
fsDecrypt.Read(storedHash,0,storedHash.Length);
|
||||
|
||||
CryptoStream csDecrypt = new CryptoStream(fsDecrypt, decryptor, CryptoStreamMode.Read);
|
||||
csDecrypt = new CryptoStream(fsDecrypt, decryptor, CryptoStreamMode.Read);
|
||||
long fileLen = fsDecrypt.Length - 32;
|
||||
byte[] fromEncrypt = new byte[fileLen];
|
||||
|
||||
@ -204,11 +220,14 @@ namespace sscs.crypto
|
||||
if(storedHash[i] != newHash[i])
|
||||
{
|
||||
CSSSLogger.DbgLog("Hash doesnot match");
|
||||
fsDecrypt.Close();
|
||||
csDecrypt.Close();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
fsDecrypt.Close();
|
||||
csDecrypt.Close();
|
||||
return tmpEncrypt;
|
||||
}
|
||||
catch(Exception e)
|
||||
@ -219,8 +238,11 @@ namespace sscs.crypto
|
||||
{
|
||||
fsDecrypt.Close();
|
||||
}
|
||||
if( csDecrypt != null )
|
||||
{
|
||||
csDecrypt.Close();
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
/* The methods EncryptData() and DecryptData() would be
|
||||
@ -317,6 +339,8 @@ namespace sscs.crypto
|
||||
string passwd,
|
||||
string fileName)
|
||||
{
|
||||
FileStream fsEncrypt = null;
|
||||
CryptoStream csEncrypt = null;
|
||||
try
|
||||
{
|
||||
if(File.Exists(fileName))
|
||||
@ -330,11 +354,11 @@ namespace sscs.crypto
|
||||
encryptor = myRijndael.CreateEncryptor(baKey, baKey);
|
||||
|
||||
//Encrypt the data to a file
|
||||
FileStream fsEncrypt = new FileStream(fileName,FileMode.Create);
|
||||
fsEncrypt = new FileStream(fileName,FileMode.Create);
|
||||
#if LINUX
|
||||
Mono.Unix.Syscall.chmod(fileName,Mono.Unix.FilePermissions.S_IRUSR | Mono.Unix.FilePermissions.S_IWUSR);
|
||||
#endif
|
||||
CryptoStream csEncrypt = new CryptoStream(fsEncrypt, encryptor,
|
||||
csEncrypt = new CryptoStream(fsEncrypt, encryptor,
|
||||
CryptoStreamMode.Write);
|
||||
|
||||
//Write all data to the crypto stream and flush it.
|
||||
@ -342,15 +366,28 @@ namespace sscs.crypto
|
||||
csEncrypt.Write(baMasterPasscode, 0, baMasterPasscode.Length);
|
||||
csEncrypt.FlushFinalBlock();
|
||||
fsEncrypt.Close();
|
||||
csEncrypt.Close();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
CSSSLogger.ExpLog(e.ToString());
|
||||
}
|
||||
if( fsEncrypt != null )
|
||||
{
|
||||
fsEncrypt.Close();
|
||||
}
|
||||
if( csEncrypt != null )
|
||||
{
|
||||
csEncrypt.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] DecryptMasterPasscodeUsingString(string passwd,
|
||||
string fileName)
|
||||
{
|
||||
FileStream fsDecrypt = null;
|
||||
CryptoStream csDecrypt = null;
|
||||
byte[] baSavedMasterPasscode = null;
|
||||
try
|
||||
{
|
||||
byte[] baKey = Generate16ByteKeyFromString(passwd);
|
||||
@ -362,23 +399,25 @@ namespace sscs.crypto
|
||||
ICryptoTransform decryptor = myRijndael.CreateDecryptor(baKey,
|
||||
baKey);
|
||||
//Now decrypt
|
||||
FileStream fsDecrypt = new FileStream(fileName, FileMode.Open);
|
||||
CryptoStream csDecrypt = new CryptoStream(fsDecrypt, decryptor,
|
||||
fsDecrypt = new FileStream(fileName, FileMode.Open);
|
||||
csDecrypt = new CryptoStream(fsDecrypt, decryptor,
|
||||
CryptoStreamMode.Read);
|
||||
byte[] baSavedMasterPasscode = new byte[16];
|
||||
baSavedMasterPasscode = new byte[16];
|
||||
|
||||
//Read the data out of the crypto stream.
|
||||
csDecrypt.Read(baSavedMasterPasscode, 0, 16);
|
||||
fsDecrypt.Close();
|
||||
|
||||
return baSavedMasterPasscode;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
CSSSLogger.ExpLog(e.ToString());
|
||||
CSSSLogger.DbgLog("Unable to decrypt master passode");
|
||||
baSavedMasterPasscode = null;
|
||||
}
|
||||
return null;
|
||||
if( fsDecrypt != null )
|
||||
fsDecrypt.Close();
|
||||
if( csDecrypt != null )
|
||||
csDecrypt.Close();
|
||||
return baSavedMasterPasscode;
|
||||
}
|
||||
|
||||
internal static byte[] GetMasterPasscodeUsingMasterPasswd(
|
||||
|
Loading…
Reference in New Issue
Block a user