C language realizes the function of irregular data encryption and decryption

  • 2020-06-15 09:54:04
  • OfStack

When important information is transmitted or stored in the network, most of the data will be encrypted to ensure the security of the data. The popular reversible encryption method on the Internet does not guarantee the security of the data, so I wrote a set of highly secure encryption and decryption methods.

Methods implementation and characteristics:

1. Use the specified single byte encryption conversion (in the form of 0-128 in ascll code table, split from 1 byte to 3 bytes, the splitting method is described below);

2. Use the random data in the array minus the specified encryption byte (for example, the current data ascll code is 121, the current data in the array is 222, the result is 222-121=101, of course, this is only a reference instance);

3. Use randomly specified array (if you need to use irregular encryption, you can use randomly specified array for encryption);

4. Specify a large data encryption format (such as using any one-time encryption ratio in 1-2048 bytes, and large amounts of data are divided into multiple encryption);

5. When encrypting data for many times, the first time the encrypted data generates the data header and the encrypted information, and then only the encrypted information is generated (the data header contains the encrypted array used (such as array 1, array 2, etc.). ,) and the length of the encrypted byte, which will be obtained according to the data header when parsed);

6. The encrypted byte is in skip order form (such as specifying the encrypted byte 1,3,5 or other byte order form, which can be achieved by simply changing some code);

Specifies that 1 byte encrypted and split into 3 bytes is denoted as:

The first byte is the random data in the random number group (for example, the array has 8 data, the first byte range 0-7 as a marker, used for decryption);
The second byte is the 1 part of the encrypted data split (less than 128 for the ascll code table);
The third byte is the adopted split format (used for decryption).

Paste the array used for encryption below:


// Any data in the array can be modified to 1-255 , and the tail data is 0 Prevents arrays from crossing boundaries 
unsigned char Data1[] ={255,210,208,179,168,199,202,189,0};
unsigned char Data2[] ={166,207,205,196,191,190,163,180,0};
unsigned char Data3[] ={155,197,186,172,228,226,219,239,0};
unsigned char Data4[] ={188,229,192,254,252,212,230,217,0};
unsigned char Data5[] ={229,206,212,224,253,211,181,207,0};

The current code USES these 5 arrays, but more randomness is needed by simply modifying the code to write more arrays, with the data in the array between 1-255(0 is not recommended, 0 is the end of the string).

Data header definition format:

[

The first 1-3 bytes of the data are "LKY", which is used to verify the encrypted data. If the data does not exist, the data will not be processed.
The data head 4-128 bytes is the calibration information segment. If the calibration is incorrect, the data will not be processed.
The 129-133 bytes of the data header are encrypted for the specified array (at present, the maximum number of 5 arrays can be specified, and 5 arrays can be used for the direct encryption processing of the specified, and more arrays can be added by modifying the code);
The data head 134-256 bytes is supplementary data;
The data header is 257-260 bytes for the specified encryption length (encryption length is 1-2048);
Data header 261-512 bytes are supplementary data;
The total length of the data header is 512 bytes.

]

h file (including encryption and decryption functions for any file format) :


#include <vector>

struct FileData
{
 CString Filename;
 LPVOID _this;
 bool bCover;
};

// Data encryption and decryption class 
class DataOperation
{
public:
 DataOperation();
 virtual ~DataOperation();
/*****************************  Encryption part  ***************************/
 // Set encrypted data (passed in 1 Said the use of unsigned char Data1[] ={255,210,208,179,168,199,202,189,193,0} Array encryption) passed in type 1-5 . 
 // If incoming data exceeds 5 Time, no longer record 
 bool SetEncryptionData(int nCount);
 // Each device 1 The data length value of the secondary encryption is 0-2048 , default use 2048
 bool SetEncryptionLen(int nLen);
 // Encrypted data function 
 // Returns the function that completes the encryption 
 // If the incoming data exceeds the set per 1 The data length for secondary encryption will take the data length set 
 // The first 1 The call is generated 512 Byte data headers with encrypted data 1 The return ( Like you need to encrypt it 3048 Bytes of data, s 1 The call function returns the data header and 2048 Encrypted data, p 2 The call returns the rest 1000 Three bytes of encrypted data )
 char *EncryptionData(char * SrcData,int & nDataLen);
 // Encrypted file 
 // parameter 1 File full path plus name 
 // parameter 2 true To overwrite file  false Add for create filename _Temp The new file 
 bool EncryptionFile(CString Filename,bool bCover);
/**********************************************/

/******************  Decryption part  *****************************/
 // Parse data header information, return each 1 The length of data passed in is required for the decryption ( Before using encrypted data 512 The byte gets the encrypted data and the length of the encrypted data for the data header information passed into the function )
 int DecryptionHead(char * SrcData);
 // Decrypt the data ( Pass in the encrypted data and the length of the data without headers )
 // Such as the decryption 3048 Byte length data, delete data 512 Bytes of data ( Data header information ) , from 513 The byte begins as the data that needs to be parsed 
 //nDataLen  The length of data that needs to be passed in each time it is returned for parsing the data header 
 char *DecryptionData(const char * SrcData,int & nDataLen);
 // Declassified documents 
 bool DecryptionFile(CString Filename,bool bCover);
/************************************************/
private:
 // Encrypt data compute function 
 char *EncryptionData_(char * SrcData,int & nDataLen,char *HeadData = 0);
 // Specifies bytes for encryption operations 
 unsigned char *Encryption_Operation(std::vector<unsigned char *> VData,unsigned char SrcData[]);
 // Gets the specified encrypted array 
 unsigned char * GetData_Operation(int nVal);
 // Gets the decryption length based on the specified encryption length 
 int GetDecryptionDataLen(int nDataLen);
 // Specifies bytes for decryption operations 
 unsigned char *Decryption_Operation(unsigned char * VData[],unsigned char SrcData[],int nVDataLen);
private:
 // Save the encrypted data that you need to use ( Use only when encrypting data, automatically obtain encrypted data according to the data header information when decrypting )
 char m_EncryptionData[6];
 // Save the decrypted data that needs to be used 
 char m_DecryptionData[6];
 //
 int m_EncryptionDataLen;
 //
 int m_DecryptionDataLen;
 //
 bool m_bValidHead;
};

Paste the.ES48en file below:


#include "stdafx.h"
#include "Operation.h"

unsigned char Data1[] ={255,210,208,179,168,199,202,189,0};
unsigned char Data2[] ={166,207,205,196,191,190,163,180,0};
unsigned char Data3[] ={155,197,186,172,228,226,219,239,0};
unsigned char Data4[] ={188,229,192,254,252,212,230,217,0};
unsigned char Data5[] ={229,206,212,224,253,211,181,207,0};
// Length of array 
int DataLen = 8;
// Access to an array 
#define GetData(nVal)\
 Data##nVal;

DataOperation::DataOperation():m_EncryptionDataLen(2048),m_bValidHead(false),m_DecryptionDataLen(0)
{
 memset(m_EncryptionData,0,sizeof(m_EncryptionData));
 memset(m_DecryptionData,0,sizeof(m_DecryptionData));
 SetEncryptionData(2);
 SetEncryptionData(1);
}

DataOperation::~DataOperation()
{

}

// If incoming data exceeds 5 Time, no longer record 
bool DataOperation::SetEncryptionData(int nCount)
{
 int nLen = strlen(m_EncryptionData);
 if (5 <= nLen)
 return false;
 m_EncryptionData[nLen] = nCount;
 return true;
}

// Each device 1 The data length value of the secondary encryption is 0-2048 , default use 2048
bool DataOperation::SetEncryptionLen(int nLen)
{
 if (0 >= nLen || 2048 < nLen)
 return false;
 m_EncryptionDataLen = nLen;
 return true;
}

// Encrypted data function 
char * DataOperation::EncryptionData(char * SrcData,int & nDataLen)
{
 if (0 == SrcData)
 return 0;
 if (!m_bValidHead)
 {
 // The data header is first generated and then merged with the data 
 char DataHead[513] = {0};
 int nInIdex = strlen("LKY");
 memcpy(DataHead,"LKY",nInIdex);
 // before 128 Bit check data header information 
 for (int i = 3;i < 128;i++)
 {
  DataHead[i] = ((DataHead[i - 1] + i)%128 + 1);
 }
 int ii = 0;
 //129-133 Encrypted data for use 
 for (int i = 128;i < 133;i++)
 {

  if (0 == m_EncryptionData[ii])
  {
  DataHead[i] = '0';
  }
  else
  {
  DataHead[i] = m_EncryptionData[ii];
  }
  ++ii;
 }
 //134-256 To fill the data 
 for (int i = 133;i < 256;i++)
 {
  DataHead[i] = ((DataHead[i - 1] + i)%128 + 1);
 }
 //257-261 Is the encryption length 
 char EncryptionDataLen[5] = {0};
 itoa(m_EncryptionDataLen,EncryptionDataLen,10);
 ii = 0;
 for (int i = 256;i < 260;i++)
 {
  if (0 == EncryptionDataLen[ii])
  {
  DataHead[i] = '99';
  }
  else
  {
  DataHead[i] = EncryptionDataLen[ii];
  }
  ++ii;
 }
 //261-512 To fill the data 
 for (int i = 260;i < 512;i++)
 {
  DataHead[i] = ((DataHead[i - 1] + i)%128 + 1);
 }
 m_bValidHead = true;
 return EncryptionData_(SrcData,nDataLen,DataHead);
 }
 return EncryptionData_(SrcData,nDataLen);
}


// Encrypt data compute function 
char *DataOperation::EncryptionData_(char * SrcData,int & nLen,char *HeadData)
{
 char pStrData[2048 + 513 + 26] = {0};
 int nIndex = 0;
 if (0 != HeadData)
 {
 memcpy(pStrData,HeadData,512);
 nIndex += 512;
 }
 int nOffset = 0,nLeft = 0;
 int nDataLen = (m_EncryptionDataLen <= nLen ? m_EncryptionDataLen : nLen);
 // Get encrypted array 
 std::vector<unsigned char *>VData;
 for(int ilen = 0;ilen < strlen(m_EncryptionData);ilen++)
 {
 int nVal = m_EncryptionData[ilen];
 unsigned char * DataArray = GetData_Operation(nVal);
 VData.push_back(DataArray);
 }
 int i = 0;
 // Start encrypting data 
 for (;i < nDataLen;(i = i * 2 + 1))
 {
 // Copy unencrypted data 
 if (0 < i - nLeft)
 {
  memcpy(pStrData + nIndex,SrcData + nLeft,i - nLeft);
  nIndex += i - nLeft;
 }

 unsigned char StrTemp[4] = {0,SrcData[i],0,0};
 Encryption_Operation(VData,StrTemp);

 if(128 <= StrTemp[1])
 {
  StrTemp[1] = StrTemp[1] - 127;
  StrTemp[2] = 127;
 }
 else if (0 == StrTemp[1])
 {
  StrTemp[1] = '0';
  StrTemp[2] = '0';
 }
 else
 {
  StrTemp[1] = 128 - StrTemp[1];
  StrTemp[2] = '1';
 }

 // Copy the converted data 
 memcpy(pStrData + nIndex,StrTemp,strlen((char *)StrTemp));
 nIndex += strlen((char *)StrTemp);
 nLeft = i + 1;
 }

 if (nLeft < nDataLen && 0 != nIndex)
 {
 memcpy(pStrData + nIndex,SrcData + nLeft,nDataLen - nLeft);
 nIndex += nDataLen - nLeft;
 }
 else if (0 == nIndex)
 {
 nLen = nDataLen;
 return pStrData;
 }
 nLen = nIndex;
 return pStrData;
}

// Specifies bytes for encryption operations 
unsigned char *DataOperation::Encryption_Operation(std::vector<unsigned char *> VData,unsigned char SrcData[])
{

 static ULONG64 StrEncryptionVal = 0;
 // The first 1 The byte position of an encrypted array is marked with two bytes 
 SrcData[0] = (StrEncryptionVal % DataLen + '0');
 for (auto it = VData.begin();it != VData.end();it++)
 {
 unsigned char * data = *it;
 SrcData[1] = (data[SrcData[0] - '0']) - SrcData[1];
 }
 ++StrEncryptionVal;
 return SrcData;
}

// Gets the specified encrypted array 
unsigned char * DataOperation::GetData_Operation(int nVal)
{
 switch (nVal)
 {
 case 1:
 {
  return GetData(1);
 }
 break;
 case 2:
 {
  return GetData(2);
 }
 break;
 case 3:
 {
  return GetData(3);
 }
 break;
 case 4:
 {
  return GetData(4);
 }
 break;
 case 5:
 {
  return GetData(5);
 }
 break;
 }
 return 0;
}

// Parse data header information, return each 1 The length of data passed in is required for the decryption ( Before using encrypted data 512 The byte gets the encrypted data and the length of the encrypted data for the data header information passed into the function )
int DataOperation::DecryptionHead(char * SrcData)
{
 if (0 == SrcData || 512 > strlen(SrcData))
 return 0;
 char pSrcData[513] = {0};
 memcpy(pSrcData,SrcData,512);
 if (pSrcData[0] != 'L' || pSrcData[1] != 'K' || pSrcData[2] != 'Y')
 return 0;
 // before 128 Bit check data header information 
 int i = 127;
 for (;i > 3;i--)
 pSrcData[i - 1] = ((pSrcData[i] + i)%128 - pSrcData[i - 1] - 1);
 if (pSrcData[i - 1] != 'Y')
 return 0;

 //129-134 Encrypted data for use 
 i = 128;
 int ii = 0;
 memset(m_DecryptionData,0,sizeof(m_DecryptionData));
 for (;i < 133;i++)
 {
 if ('0' == pSrcData[i])
 {
  continue;
 }
 else
 {
  m_DecryptionData[ii++] = pSrcData[i];
 }
 }

 //257-261 Is the encryption length 
 char EncryptionDataLen[5] = {0};
 ii = 0;
 i = 256;
 for (;i < 260;i++)
 {
 if (pSrcData[i] == '99')
 {
  continue;
 }
 else
 {
  EncryptionDataLen[ii++] = pSrcData[i];
 }
 }
 m_EncryptionDataLen = atoi(EncryptionDataLen);
 m_DecryptionDataLen = GetDecryptionDataLen(m_EncryptionDataLen);
 return m_DecryptionDataLen;
}

// Gets the decryption length based on the specified encryption length 
int DataOperation::GetDecryptionDataLen(int nDataLen)
{
 if (0 >= nDataLen)
 return 0;
 int nIndex = 0;
 for (int i = 0;i < nDataLen;(i = i * 2 + 1))
 {
 nIndex += 2;
 }
 return nDataLen + nIndex;
}

// Decrypt the data ( Pass in the encrypted data and the length of the data without headers )
char *DataOperation::DecryptionData(const char * SrcData,int & nDataLen)
{
 if (0 == SrcData || 0 >= nDataLen)
 return 0;

 char Data[2048 + 100] = {0};
 int nIndex = 0;
 int nOffset = 0,nLeft = 0,nCurrent = 0;
 int nLen = (m_DecryptionDataLen <= nDataLen ? m_DecryptionDataLen : nDataLen);
 // Access to decrypt 
 int nDecryptionDataLen = strlen(m_DecryptionData);
 unsigned char *VData[10] = {0};
 int nVDataLen = 0;
 for(int iLen = nDecryptionDataLen;iLen > 0;iLen--)
 {
 int nVal = m_DecryptionData[iLen - 1];
 unsigned char * DataArray = GetData_Operation(nVal);
 VData[nVDataLen++] = DataArray;
 }
 int i = 0;
 // Start encrypting data 
 for (;i < nLen;(i = i * 2 + 1))
 {
 nCurrent = i + nOffset;
 if (nCurrent >= nDataLen)
 {
  break;
 }

 // Copy unencrypted data 
 if (0 < nCurrent - nLeft)
 {
  memcpy(Data + nIndex,SrcData + nLeft,nCurrent - nLeft);
  nIndex += (nCurrent - nLeft);
 }

 unsigned char StrTemp[4] = {SrcData[nCurrent],SrcData[nCurrent + 1],SrcData[nCurrent + 2],0};

 if (127 == StrTemp[2])
 {
  StrTemp[1] = StrTemp[1] + 127;
  StrTemp[2] = 0;
 }
 else if ('1' == StrTemp[2])
 {
  StrTemp[1] = 128 - StrTemp[1];
  StrTemp[2] = 0;
 }
 else if ('0' == StrTemp[2])
 {
  StrTemp[1] = 0;
  StrTemp[2] = 0;
 }
 else
 {
  StrTemp[2] = 0;
 }

 Decryption_Operation(VData,StrTemp,nDecryptionDataLen);
 // Copy the converted data 
 memcpy(Data + nIndex,StrTemp + 1,1);
 nIndex += 1;
 nOffset += 2;
 nLeft = i + nOffset + 1;
 }
 if (nLeft < nLen && 0 != nIndex)
 {
 memcpy(Data + nIndex,SrcData + nLeft,nLen - nLeft);
 nIndex += nLen - nLeft;
 }
 else if (0 == nIndex)
 {
 return Data;
 }

 nDataLen = nIndex;
 return Data;
}

// Specifies bytes for decryption operations 
unsigned char *DataOperation::Decryption_Operation(unsigned char * VData[],unsigned char SrcData[],int nVDataLen)
{
 for (int i = 0;i < nVDataLen;i++)
 {
 unsigned char * data = VData[i];
 SrcData[1] = (data[SrcData[0] - '0']) - SrcData[1];
 }
 return SrcData;
}


// Encrypted file 
bool DataOperation::EncryptionFile(CString Filename,bool bCover)
{
 // Add random encryption   Use the default 2 . 1 An array of 
 SetEncryptionData(5);
 SetEncryptionData(4);

 if (Filename.IsEmpty())
 return false;
 CFile file1,file2;
 if (!file1.Open(Filename,CFile::modeRead))

 return false;
 ULONG64 nFileLen = file1.GetLength();
 if (0 == nFileLen)
 return false;

 CString Filename2(Filename.Mid(0,Filename.ReverseFind(_T('.'))));
 Filename2.AppendFormat(_T("_Temp%s"),Filename.Mid(Filename.ReverseFind(_T('.')),
 Filename.GetLength() - Filename.ReverseFind(_T('.'))));

 if (!file2.Open(Filename2,CFile::modeCreate | CFile::modeWrite))
 return false;
 char StrData[4096] = {0};
 int nDataLen = 0;
 while (0 < nFileLen)
 {
 if (2048 <= nFileLen)
 {
  file1.Read(StrData,2048);
  nDataLen = 2048;
  char *WriteFile = EncryptionData(StrData,nDataLen);
  file2.Write(WriteFile,nDataLen);
  nFileLen -= 2048;
 }
 else
 {
  file1.Read(StrData,nFileLen);
  nDataLen = nFileLen;
  char *WriteFile = EncryptionData(StrData,nDataLen);
  file2.Write(WriteFile,nDataLen);
  nFileLen -= nFileLen;
  break;
 }
 }
 file1.Close();
 file2.Close();
 if (bCover)
 {
 USES_CONVERSION;
 // Overwrite local files 
 int nResult = remove(T2A(Filename));
 nResult = rename(T2A(Filename2),T2A(Filename));
 }
 return true;
}

// Declassified documents 
bool DataOperation::DecryptionFile(CString Filename,bool bCover)
{
 if (Filename.IsEmpty())
 return false;
 CFile file1,file2;
 if (!file1.Open(Filename,CFile::modeRead))
 return false;
 ULONG64 nFileLen = file1.GetLength();
 if (512 >= nFileLen)
 return false;

 CString Filename2(Filename.Mid(0,Filename.ReverseFind(_T('.'))));
 Filename2.AppendFormat(_T("_Temp%s"),Filename.Mid(Filename.ReverseFind(_T('.')),
 Filename.GetLength() - Filename.ReverseFind(_T('.'))));

 if (!file2.Open(Filename2,CFile::modeCreate | CFile::modeWrite))
 return false;
 char StrData[2048 + 100] = {0};
 int nDataLen = 0;
 file1.Read(StrData,512);
 // Decryption header 
 int nDecryptionLen = DecryptionHead(StrData);
 if (0 >= nDecryptionLen)
 return false;
 nFileLen -= 512;


 while (0 < nFileLen)
 {

 if (nDecryptionLen <= nFileLen)
 {

  nDataLen = nDecryptionLen;
  file1.Read(StrData,nDataLen);
  char *WriteFile = DecryptionData(StrData,nDataLen);
  memset(StrData,0,strlen(StrData));
  memcpy(StrData,WriteFile,nDataLen);
  file2.Write(StrData,nDataLen);
  nFileLen -= nDecryptionLen;
 }
 else
 {
  nDataLen = nFileLen;
  file1.Read(StrData,nFileLen);
  char *WriteFile = DecryptionData(StrData,nDataLen);
  memset(StrData,0,strlen(StrData));
  memcpy(StrData,WriteFile,nDataLen);
  file2.Write(StrData,nDataLen);
  nFileLen -= nFileLen;
  break;
 }
 }
 file1.Close();
 file2.Close();
 if (bCover)
 {
 USES_CONVERSION;
 // Overwrite local files 
 int nResult = remove(T2A(Filename));
 nResult = rename(T2A(Filename2),T2A(Filename));
 }

 return true;
}

Here's how to use it:


// Set encrypted data (passed in 1 Said the use of unsigned char Data1[] ={255,210,208,179,168,199,202,189,193,0} Array encryption) passed in type 1-5 . 
// If incoming data exceeds 5 Time, no longer record 
// This function needs to be called before the encryption function is used, which is used by default 2 , 1 An array of encryption 
bool SetEncryptionData(int nCount);


// Set each 1 The data length value of the secondary encryption is 0-2048 , default use 2048
bool SetEncryptionLen(int nLen);


// Encrypted data function 
// Returns the function that completes the encryption 
// If the incoming data exceeds the set per 1 The data length for secondary encryption will take the data length set 
// The first 1 The call is generated 512 Byte data headers with encrypted data 1 The return ( Like you need to encrypt it 3048 Bytes of data, s 1 The call function returns the data header and 2048 Encrypted data, p 2 The call returns the rest 1000 Three bytes of encrypted data )
char *EncryptionData(char * SrcData,int & nDataLen);


// Encrypted file 
// parameter 1 File full path plus name 
// parameter 2 true To overwrite file  false Add for create filename _Temp The new file 
bool EncryptionFile(CString Filename,bool bCover);


// Parse data header information, return each 1 The length of data passed in is required for the decryption ( Before using encrypted data 512 The byte gets the encrypted data and the length of the encrypted data for the data header information passed into the function )
int DecryptionHead(char * SrcData);


// Decrypt the data ( Pass in the encrypted data and the length of the data without headers )
// Such as the decryption 3048 Byte length data, delete data 512 Bytes of data ( Data header information ) , from 513 The byte begins as the data that needs to be parsed 
//nDataLen  The length of data that needs to be passed in each time it is returned for parsing the data header 
char *DecryptionData(const char * SrcData,int & nDataLen);


// Declassified documents 
// parameter 1 File full path plus name 
// parameter 2 true To overwrite file  false Add for create filename _Temp The new file 
bool DecryptionFile(CString Filename,bool bCover);

Refer to the encrypted file and decrypted file functions, project download address


Related articles: