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

📄 scx3_main_unit.cpp

📁 This file contains a C++Builder 4 project called SimplyChaos-X ver 3.1 (SCX31). SCX31 is an encry
💻 CPP
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "SCX3_main_unit.h"
#include "TChaosFunctionUnit.h"
#include "TKeyMatrice.h"
#include "PasphraseAnalyserUnit.h"
#include "GetResultPath_Form_Unit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CGAUGES"
#pragma resource "*.dfm"
TMainForm *MainForm;

TChaosFunction CF1, CF2, CF3, KV;
TKM KM1;
TPassphrase Passphrase;

//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ButtonClick(TObject *Sender)
{
  switch(dynamic_cast<TSpeedButton*>(Sender)->Tag)
  {
    case 1: //btnOpening
      if(odOpening->Execute())
        edSourcePath->Text = odOpening->FileName;
      else
        edSourcePath->Text = "";
      break;
    case 2: //btnSaving
      if(GetResultPathForm->ShowModal() == mrOk)
      {
        edResultPath->Text =
          GetResultPathForm->DirectoryList->Directory;

        if(edResultPath->Text[edResultPath->Text.Length()] != '\\')
          edResultPath->Text = edResultPath->Text + AnsiString("\\\0");
      }
      else
        edResultPath->Text = "";
      break;
    case 3: //btnEngage
      InitiateKeyMatrice();
      DestroyPassphrase();
      if(rbEncrypt->Checked)
        Encryption();
      if(rbDecrypt->Checked)
        Decryption();
      break;
    case 4: //btnExit
      Close();
      break;
  };
}
//----------------------------------------------------------------------
void __fastcall TMainForm::InitiateKeyMatrice(void)
{
  int i, j, k;
  unsigned char NewKey;

  Passphrase.SetPassphrase(edPassphrase->Text.c_str());
  Passphrase.Analyse();
  for(i = 0; i < 50; i++)
    for(j = 0; j < 50; j++)
      for(k = 0; k < 50; k++)
      {
        NewKey = char(int(Passphrase.ExtractKey() * 256));
        KM1.SetKey(i, j, k, char(NewKey));
      }

  CF1.StartCycle(Passphrase.ExtractKey());
  CF2.StartCycle(Passphrase.ExtractKey());
  CF3.StartCycle(Passphrase.ExtractKey());
}
//----------------------------------------------------------------------
void __fastcall TMainForm::DestroyPassphrase(void)
{
  AnsiString DestroyString;

  DestroyString = "                              ";

  edPassphrase->Text = DestroyString;
  edPassphrase->Text = "";
}
//----------------------------------------------------------------------
void __fastcall TMainForm::Encryption(void)
{
  int i, j, a, b, c, Cipher, Plain;
  double FileLength, ActualRead, ByteCount;
  unsigned char NewKey, TheKey;
  char Buffer1 [10240], Buffer2 [10240]; //each buffer 10k in length
  AnsiString FileName;

  Gauge->Progress = 0;
  ByteCount = 0;

  //Edit result file name and path
  j = i = edSourcePath->Text.Length();
  while(edSourcePath->Text[i] != '\\')
    i--;
  i++;
  FileName = "";
  FileName = edSourcePath->Text.SubString(i, j - i + 1);
  FileName += ".enc\0";

  //Open plain file
  Plain = FileOpen(edSourcePath->Text, fmOpenRead);
  FileLength = FileSeek(Plain, 0, 2);
  FileSeek(Plain, 0, 0);
  //Create cipher file
  Cipher = FileCreate(edResultPath->Text + FileName);
  SignFile(Cipher);
  Gauge->Progress = 0;

  do
  {
    //Read plain char
    ActualRead = FileRead(Plain, &Buffer1, 10240);

    //Encrypt the plain char
    for(i = 0; i < ActualRead; i++)
    {
      a = int(CF1.StepIt() * 50);
      b = int(CF2.StepIt() * 50);
      c = int(CF3.StepIt() * 50);
      TheKey = KM1.GetKey(a, b, c);

      Buffer2[i] = Buffer1[i] - TheKey;
      Gauge->Progress = (++ByteCount / FileLength * 100) + 1;

      if(KM1.ReplaceKey(a, b, c) == 3)
      {
        for(j = 1; j <= 15; j++)
          NewKey = char(int(Passphrase.ExtractKey() * 256));
        KM1.SetKey(a, b, c, NewKey);
      }
    }
    FileWrite(Cipher, &Buffer2, ActualRead);
  } while(ActualRead == 10240);
  //Do encryption while there are still more byte to be encrypted

  FileClose(Plain);
  FileClose(Cipher);

  if(cbDeletion->Checked)
    DeleteFile(edSourcePath->Text);
}
//----------------------------------------------------------------------
void __fastcall TMainForm::Decryption(void)
{
  int i, j, a, b, c, Cipher, Plain, SignatureLength;
  double FileLength, ActualRead, ByteCount;
  unsigned char NewKey, TheKey;
  char Buffer1 [10240], Buffer2 [10240]; //each buffer 10k in length
  AnsiString FileName;

  Gauge->Progress = 0;
  ByteCount = 0;

  //Edit result file name and path
  j = i = edSourcePath->Text.Length();
  while(edSourcePath->Text[i] != '\\')
    i--;
  i++;
  FileName = "";
  FileName = edSourcePath->Text.SubString(i, j - i - 3);

  //Open cipher file
  Cipher = FileOpen(edSourcePath->Text, fmOpenRead);
  FileLength = FileSeek(Cipher, 0, 2);
  FileSeek(Cipher, 0, 0);
  if(VerifySignature(Cipher, SignatureLength))
  {
    ByteCount += SignatureLength;

    //Create plain file
    Plain = FileCreate(edResultPath->Text + FileName);
    Gauge->Progress = 0;

    do
    {
      //Read cipher char
      ActualRead = FileRead(Cipher, &Buffer1, 10240);

      //Decrypt
      for(i = 0; i < ActualRead; i++)
      {
        a = int(CF1.StepIt() * 50);
        b = int(CF2.StepIt() * 50);
        c = int(CF3.StepIt() * 50);
        TheKey = KM1.GetKey(a, b, c);

        Buffer2[i] = Buffer1[i] + TheKey;
        Gauge->Progress = (++ByteCount / FileLength * 100) + 1;

        if(KM1.ReplaceKey(a, b, c) == 3)
        {
          for(j = 1; j <= 15; j++)
            NewKey = char(int(Passphrase.ExtractKey() * 256));
          KM1.SetKey(a, b, c, NewKey);
        }
      }
      FileWrite(Plain, &Buffer2, ActualRead);
    } while(ActualRead == 10240);
    //Do encryption while there are still more byte to be encrypted

    FileClose(Plain);
    FileClose(Cipher);

    if(cbDeletion->Checked)
      DeleteFile(edSourcePath->Text);
  }
  else
  {
    FileClose(Cipher);

    MessageBox(
      Handle,
      "Passphrase is not match",
      "Error",
      MB_OK | MB_ICONWARNING | MB_APPLMODAL
    );
  }
}
//----------------------------------------------------------------------
void __fastcall TMainForm::EditBoxChange(TObject *Sender)
{
  if(edSourcePath->Text.Length() == 0 || edResultPath->Text.Length() == 0 ||
     edPassphrase->Text.Length() == 0)
    btnEngage->Enabled = false;
  else
    btnEngage->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::rbEncryptClick(TObject *Sender)
{
  edSourcePath->Enabled = true;
  btnGetSourcePath->Enabled = true;
  edResultPath->Enabled = true;
  btnSetResultPath->Enabled = true;
  edPassphrase->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::SignFile(int &Cipher)
{
  /*
    The signature is encrypted string 'SC-X3.1' by user's password
  */
  int a, b, c, SignatureLength, i;
  unsigned char TheKey;
  char Signature[20];

  strcpy(Signature, "SC-X3.1\0");
  SignatureLength = strlen(Signature);

  for(i = 0; i < SignatureLength; i++)
  {
    a = int(CF1.StepIt() * 50);
    b = int(CF2.StepIt() * 50);
    c = int(CF3.StepIt() * 50);
    TheKey = KM1.GetKey(a, b, c);

    Signature[i] = Signature[i] - TheKey;
  }

  FileWrite(Cipher, &Signature, SignatureLength);
}
//---------------------------------------------------------------------------
bool __fastcall TMainForm::VerifySignature(int &Cipher, int &SigLength)
{
  /*
    The signature is encrypted string 'SC-X3.1' by user's password
  */
  int a, b, c, SignatureLength, i;
  unsigned char TheKey;
  double ActualRead;
  char Signature[20];

  strcpy(Signature, "SC-X3.1\0");
  SignatureLength = strlen(Signature);
  SigLength = SignatureLength;

  ActualRead = FileRead(Cipher, &Signature, SignatureLength);

  if(SignatureLength == ActualRead)
  {
    for(i = 0; i < SignatureLength; i++)
    {
      a = int(CF1.StepIt() * 50);
      b = int(CF2.StepIt() * 50);
      c = int(CF3.StepIt() * 50);
      TheKey = KM1.GetKey(a, b, c);

      Signature[i] = Signature[i] + TheKey;
    }

    if(strcmp("SC-X3.1", Signature) == 0)
      return true;
    else
      return false;
  }
  else
    return false;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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