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

📄 dib.cpp

📁 VC实现的图像加密算法
💻 CPP
字号:
// DIB.cpp


#include "stdafx.h"
#include "DIB.h"
#include <math.h>
CDib::CDib()
{

	m_pDib = NULL;
	//m_pWordData = NULL;
	//m_pFile = NULL;
}

CDib::~CDib()
{


	if( m_pDib != NULL )
		delete [] m_pDib;
	//if( m_pWordData != NULL )
		//delete [] m_pWordData;
	//if (m_pFile != NULL)
		//delete [] m_pFile;

}

int CDib::sdd()
{
	int nWidth;
	nWidth= m_pBIH->biWidth;
	int nHeight; 
    nHeight= m_pBIH->biHeight;
	if(nWidth!=nHeight)
	{
       return 0;
	}
}
void CDib::KeyProcess(long Key)
{  
  int Length=0;
  long pa;
 for(int i=0;i<=9;i++)
   {
	   if(Key/pow(10,i)>=1)
	       Length++;
       else
		   break;
   }
   double x;
   x=Key/pow(10,Length);
   int n=0;
   while(n<8)
   {
     double u=3.8;
     x=u*x*(1-x);
     n++;
   }
  pa=x*pow(10,6);
  KandN = pa/100; 
}

int CDib::GetPeriod()
{
    int Period;
    int nWidth;
	int k=KandN%100;
	nWidth= m_pBIH->biWidth;
    if(nWidth<2)
	{
		Period=0;
		return Period;
	}
    //  else
	//{
	int n=1;
	int x=1;
	int y=1;
	int xn,yn;
	while(n!=0)
	{
		 xn=x+y;
		 yn=k*x+k*y+y;
		if((xn%nWidth)==1&&(yn%nWidth)==1)
		{        	
			Period=n;
		 return Period;
		}
		else
		{
		   x=xn%nWidth;
		   y=yn%nWidth;
	       n=n+1;
		}

//}
}
}

void CDib::jiami()
{
   	int n=0;
    int nWidth;
	nWidth= m_pBIH->biWidth;
	int nHeight; 
    nHeight= m_pBIH->biHeight;
	//Msize=nWidth*nHeight;
	unsigned char *M1;
    M1=new unsigned char[nWidth*nHeight];
    unsigned char *M2;
    M2=new unsigned char[nWidth*nHeight];
	for(int i=0;i<nWidth*nHeight;i++)
	{
		M1[i]=p[i];
	    M2[i]=p[i];
	}
	for(int Frequency=1;Frequency<=28;Frequency++)
	{
		n=n+1;
		if(n%2==0)
		{
			for(int i=1;i<=nWidth;i++)
			 for(int j=1;j<=nHeight;j++)
			{
			     int k=KandN%100;
			     int I=(i+j-2)%nWidth;
			     int J=(k*i+k*j+j-2*k-1)%nHeight;
                 unsigned char c;
                 c=M2[(i-1)*nWidth+(j-1)];
			     M1[I*nWidth+J]=c;
			}
        }

        else
		{
            for(int i=1;i<=nWidth;i++)
			 for(int j=1;j<=nHeight;j++)
			{
			     int k=KandN%100;
			     int I=(i+j-2)%nWidth;
			     int J=(k*i+k*j+j-2*k-1)%nHeight;
                 unsigned char c;
                 c=M2[(i-1)*nWidth+(j-1)];
			     M1[I*nWidth+J]=c;
			}
		}
	}
	if((Frequency%2)==0)
      for(int i=0;i<nWidth*nHeight;i++)	
		p[i]=M1[i];	
	else
      for(int i=0;i<nWidth*nHeight;i++)
	   p[i]=M2[i];
	delete[]M1;
	delete[]M2;
}

void CDib::jiemi()
{
   	int n=0;
    int nWidth;
	nWidth= m_pBIH->biWidth;
	int nHeight; 
    nHeight= m_pBIH->biHeight;
	//Msize=nWidth*nHeight;
	unsigned char *M1;
    M1=new unsigned char[nWidth*nHeight];
    unsigned char *M2;
    M2=new unsigned char[nWidth*nHeight];
	for(int i=0;i<nWidth*nHeight;i++)
	{
		M1[i]=p[i];
	    M2[i]=p[i];
	}
	for(int Frequency=1;Frequency<=GetPeriod()-26;Frequency++)
	{
		n=n+1;
		if(n%2==0)
		{
			for(int i=1;i<=nWidth;i++)
			 for(int j=1;j<=nHeight;j++)
			{
			     int k=KandN%100;
			     int I=(i+j-2)%nWidth;
			     int J=(k*i+k*j+j-2*k-1)%nHeight;
                 unsigned char c;
                 c=M2[(i-1)*nWidth+(j-1)];
			     M1[I*nWidth+J]=c;
			}
        }

        else
		{
            for(int i=1;i<=nWidth;i++)
			 for(int j=1;j<=nHeight;j++)
			{
			     int k=KandN%100;
			     int I=(i+j-2)%nWidth;
			     int J=(k*i+k*j+j-2*k-1)%nHeight;
                 unsigned char c;
                 c=M2[(i-1)*nWidth+(j-1)];
			     M1[I*nWidth+J]=c;
			}
		}
	}
	if((Frequency%2)==0)
      for(int i=0;i<nWidth*nHeight;i++)	
		p[i]=M1[i];	
	else
      for(int i=0;i<nWidth*nHeight;i++)
	   p[i]=M2[i];
	delete[]M1;
	delete[]M2;
    
}
BOOL CDib::Load(const char *pszFilename)
{

	CFile cf;

	if( !cf.Open(pszFilename, CFile::modeRead ) )
		return( FALSE );

	
	DWORD dwDibSize;
	dwDibSize =
		cf.GetLength() - sizeof( BITMAPFILEHEADER );

	unsigned char *pDib;
	pDib = new unsigned char [dwDibSize];
	if( pDib == NULL )
		return( FALSE );

	BITMAPFILEHEADER BFH;

	try{

		if( cf.Read( &BFH, sizeof( BITMAPFILEHEADER ) )
			!= sizeof( BITMAPFILEHEADER ) ||

			BFH.bfType != 'MB' ||

			cf.Read( pDib, dwDibSize ) != dwDibSize ){

			delete [] pDib;
			return( FALSE );
			}
		}

	
	catch( CFileException *e ){
		e->Delete();
		delete [] pDib;
		return( FALSE );
		}
	
	cf.Close();

	if( m_pDib != NULL )
		delete m_pDib;

	m_pDib = pDib;
	m_dwDibSize = dwDibSize;


	m_pBIH = (BITMAPINFOHEADER *) m_pDib;
	m_pPalette =
		(RGBQUAD *) &m_pDib[sizeof(BITMAPINFOHEADER)];


	m_nPaletteEntries = 1 << m_pBIH->biBitCount;
	if( m_pBIH->biBitCount > 8 )         
		m_nPaletteEntries = 0;
	else if( m_pBIH->biClrUsed != 0 )
		m_nPaletteEntries = m_pBIH->biClrUsed;

	
	m_pDibBits =                     
		&m_pDib[sizeof(BITMAPINFOHEADER)+
			m_nPaletteEntries*sizeof(RGBQUAD)];

	if( m_Palette.GetSafeHandle() != NULL )
		m_Palette.DeleteObject();

	if( m_nPaletteEntries != 0 ){      

		LOGPALETTE *pLogPal = (LOGPALETTE *) new char
				[sizeof(LOGPALETTE)+
				m_nPaletteEntries*sizeof(PALETTEENTRY)];

		if( pLogPal != NULL ){

			pLogPal->palVersion = 0x300;
			pLogPal->palNumEntries = m_nPaletteEntries;

			for( int i=0; i<m_nPaletteEntries; i++ ){
				pLogPal->palPalEntry[i].peRed =
					m_pPalette[i].rgbRed;
				pLogPal->palPalEntry[i].peGreen =
					m_pPalette[i].rgbGreen;
				pLogPal->palPalEntry[i].peBlue =
					m_pPalette[i].rgbBlue;
				}

			m_Palette.CreatePalette( pLogPal );
			delete [] pLogPal;
			}
		}

	p = m_pDibBits;              //指向位图数据的指针,用来执行处理操作用
	bitmap_size = m_dwDibSize - (m_pDibBits - m_pDib);//真正的位图数据的大小(即除头结构外)
	
	return( TRUE );

}

BOOL CDib::Write( const char *pszFilename ) //将加密信息写入文件。
{

	
	if( m_pDib == NULL )
		return( FALSE );

	CFile cf;
    if( !cf.Open( pszFilename,
		CFile::modeCreate | CFile::modeWrite ) )
		return( FALSE );
	try{
		BITMAPFILEHEADER BFH;
		memset( &BFH, 0, sizeof( BITMAPFILEHEADER ) );
		BFH.bfType = 'MB';
		BFH.bfSize = sizeof( BITMAPFILEHEADER ) + m_dwDibSize;
		BFH.bfOffBits = sizeof( BITMAPFILEHEADER ) +
			sizeof( BITMAPINFOHEADER ) +
			m_nPaletteEntries * sizeof( RGBQUAD );
		cf.Write( &BFH, sizeof( BITMAPFILEHEADER ) );
		cf.Write( m_pDib, m_dwDibSize );
		}


	catch( CFileException *e ){
		e->Delete();
		return( FALSE );
		}
	return( TRUE );

}


⌨️ 快捷键说明

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