⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eccformmemory.cpp

📁 vc++实现的ecc加密算法
💻 CPP
字号:
/* ==========================================================================

	Ecc - Erik's Code Collection
	Copyright (C) 2003 - Erik Dienske

	This file is part of Ecc.

	Ecc is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	Ecc is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Ecc; if not, write to the Free Software Foundation, Inc.,
	59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
	
===========================================================================*/

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "EccFormMemory.h"
#include "EccPropertyHandler.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------

//===========================================================================
namespace ecc {
//===========================================================================

//---------------------------------------------------------------------------

TFormMemory::TFormMemory(const String ini_fpath, const bool protect)
{
	FProtected	= protect;
	FFilePath 	= ini_fpath;

	if (FProtected)
		ProtectFile(false);  // Unscramble

	if(! DirectoryExists(ExtractFilePath(ini_fpath)) )
		ForceDirectories(ExtractFilePath(ini_fpath));
	FIniFile = new TIniFile(ini_fpath);
}
//---------------------------------------------------------------------------

TFormMemory::~TFormMemory(void)
{
	FIniFile->UpdateFile(); // Flush file to disk.
	delete FIniFile;

	if (FProtected)
		ProtectFile(true);   // Scramble
}
//---------------------------------------------------------------------------

TIniFile* TFormMemory::GetIniFile()
{
	return (TIniFile*) FIniFile;
}
//---------------------------------------------------------------------------

void TFormMemory::SetComponent(TComponent* sect_comp)
{
	FSectionComponent = sect_comp;
}
//---------------------------------------------------------------------------

void TFormMemory::WriteProps(String props)
{
	TPropertyHandler ph(FSectionComponent);
	TStringList* prop_list = new TStringList();

	try
	{
		prop_list->Text = ph.GetNamesValues(props);
		for (int i = 0; i < prop_list->Count; i++)
		{
			FIniFile->WriteString(
				ph.GetName(),
				prop_list->Names[i],
				prop_list->Values[prop_list->Names[i]]);
		}
	}
	__finally
	{
		delete prop_list;
	}
}
//---------------------------------------------------------------------------

void TFormMemory::WriteProps(TComponent* comp, String props)
{
	TPropertyHandler ph(FSectionComponent);
	TStringList* prop_list = new TStringList();

	try
	{
		prop_list->Text = ph.GetNamesValues(comp, props);
		for (int i = 0; i < prop_list->Count; i++)
		{
			FIniFile->WriteString(
				ph.GetName(FSectionComponent),
				ph.GetName(comp) + "." + prop_list->Names[i],
				prop_list->Values[prop_list->Names[i]]);
		}
	}
	__finally
	{
		delete prop_list;
	}

}
//---------------------------------------------------------------------------

void TFormMemory::ReadProps(TComponent* sect_comp)
{
	TPropertyHandler ph(sect_comp);
	TStringList* prop_list = new TStringList();

	try
	{
		FIniFile->ReadSectionValues(ph.GetName(), prop_list);
		ph.SetNamesValues(prop_list->Text);
	}
	__finally
	{
		delete prop_list;
	}
}
//---------------------------------------------------------------------------

void TFormMemory::ProtectFile(bool protect)
//	(Un)Scrambles the ini file.
//	Note that this is a lousy way to scramble the file, it is NOT encrypted.
//	The file becomes just a bit unreadable...
{
	if (!FileExists(FFilePath)) return;

	FileSetAttr(FFilePath, 0);

	try
	{
		// Read file:
		int fh = FileOpen(FFilePath, fmOpenRead);
		int flen = FileSeek(fh,0,2);
		FileSeek(fh,0,0);
		char* buf = new char[flen+1];
		flen = FileRead(fh, buf, flen);
		FileClose(fh);

		// FileWrite() inserts...does not overwrite! So empty file by deleting it:
		DeleteFile(FFilePath);

		if (protect) Scramble(buf, flen);
		else Unscramble(buf, flen);

		// Create file:
		fh = FileCreate(FFilePath);
		FileClose(fh);

		// Write file:
		fh = FileOpen(FFilePath, fmOpenWrite);
		FileSeek(fh,0,0);
		FileWrite(fh, buf, flen);
		FileClose(fh);

		delete[] buf;
	}
	catch(...)
	{
		//"FILE ERROR! "__FUNC__;
	}

	if (protect) FileSetAttr(FFilePath, faHidden | faReadOnly);
}
//---------------------------------------------------------------------------

void TFormMemory::Scramble(char* buf, int flen)
{
	for (int i = 0; i < flen; i++)
	{
		buf[i] = buf[i] ^ 0xff;
		buf[i] = buf[i] ^ i;
	}
}
//---------------------------------------------------------------------------

void TFormMemory::Unscramble(char* buf, int flen)
{
	for (int i = 0; i < flen; i++)
	{
		buf[i] = buf[i] ^ i;
		buf[i] = buf[i] ^ 0xff;
	}
}
//---------------------------------------------------------------------------

//===========================================================================
} // namespace ecc;
//===========================================================================

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -