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

📄 pgputil.cpp

📁 加密解密
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	//if( PGPSymmetricCipherContextRefIsValid( cipher1 ) )
	//		(void) PGPFreeSymmetricCipherContext( cipher1 );
		
	//if( PGPCFBContextRefIsValid( cfb1 ) )
	//	(void) PGPFreeCFBContext(cfb1);
		
	//if( PGPCBCContextRefIsValid( cbc1 ) )
	//	(void) PGPFreeCBCContext(cbc1);
			
	return err;

}



static PGPError pgpCipherDecrypt(PGPContextRef context , 
							 PGPCipherAlgorithm	algor , 
							 CipherMode  mode ,
							 PGPByte *key , 
							 PGPByte *plaintext , PGPSize plaintextlen  , 
							 PGPByte	*initvector )
{
	PGPSymmetricCipherContextRef cipher		= kInvalidPGPSymmetricCipherContextRef;
	//PGPSymmetricCipherContextRef cipher1	= kInvalidPGPSymmetricCipherContextRef;
	PGPCFBContextRef			cfb			= kInvalidPGPCFBContextRef;
	PGPCBCContextRef			cbc			= kInvalidPGPCBCContextRef;
	//PGPCFBContextRef			cfb1		= kInvalidPGPCFBContextRef;
	//PGPCBCContextRef			cbc1		= kInvalidPGPCBCContextRef;
	PGPError					err 		= kPGPError_NoErr;
 	PGPByte						*out		= NULL;
 	PGPSize						keySize; 
	PGPSize						blockSize;
	PGPUInt32					i;	

	out = (PGPByte *)PGPNewSecureData( PGPPeekContextMemoryMgr(context) , plaintextlen , kPGPMemoryMgrFlags_None); 
	if (out == NULL)
		goto done;

	/* create and setup the  cipher */
	err = PGPNewSymmetricCipherContext(context, algor, &cipher);
	if (IsPGPError(err))
		goto done; 	
	/*  Get information about the cipher */
	err = PGPGetSymmetricCipherSizes(cipher, &keySize, &blockSize); 	
	if (IsPGPError(err))
		goto done;

	/* create and setup the  cipher, init keys and IV */
	switch(mode)
	{
		case kCipherModeECB:
			err = PGPInitSymmetricCipher(cipher, key); 	
			if (IsPGPError(err))
				goto done; 	
///////////////////////////////////////////////////////////////////////
			for(i = 0; i < plaintextlen ; i += blockSize)
			{
				err = PGPSymmetricCipherDecrypt(cipher , plaintext + i, out + i);  
				if (IsPGPError(err))
					goto done; 	
			}
///////////////////////////////////////////////////////////////////////
			break;			
		case kCipherModeCBC:
			/* create new CBC context */
//////////////////////////////////////////////////////////////////////
			err = PGPNewCBCContext(cipher , &cbc); 
			if (IsPGPError(err))
				goto done; 	
			cipher	= kInvalidPGPSymmetricCipherContextRef;
			// init CBC with IV and Key //
			err = PGPInitCBC(cbc,  key,  initvector); 
			if (IsPGPError(err))
				goto done; 	
			for(i = 0; i < plaintextlen ; i += blockSize)
			{	
				err = PGPCBCDecrypt(cbc , plaintext + i, blockSize,  out + i); 
				if (IsPGPError(err))
					goto done; 	
			}
/////////////////////////////////////////////////////////////////////
/*
			err = PGPCopySymmetricCipherContext(cipher, &cipher1); 
			if (IsPGPError(err))
				goto done; 	
			err = PGPNewCBCContext(cipher1, &cbc); 
			if (IsPGPError(err))
				goto done; 	
			err = PGPCBCGetSymmetricCipher(cbc, &cipher1); 
			if (IsPGPError(err))
				goto done; 	
			cipher1	= kInvalidPGPSymmetricCipherContextRef;
			//init CBC with IV and Key //
			err = PGPInitCBC(cbc,  key,  initvector); 
			if (IsPGPError(err))
				goto done; 	
			//Save a copy for decryption later //
			//err = PGPCopyCBCContext(cbc, &cbc1);*/
			break;
			
		case kCipherModeCFB:
			/* create new CFB context */
//////////////////////////////////////////////////////////////////
			err = PGPNewCFBContext(cipher, 1, &cfb);
			if (IsPGPError(err))
				goto done; 		
			cipher = kInvalidPGPSymmetricCipherContextRef;
			// init CFB with IV and Key/
			err = PGPInitCFB(cfb,  key , initvector); 
			if (IsPGPError(err))
				goto done; 	
			for(i = 0; i < plaintextlen ; i += blockSize)
			{
				err =  PGPCFBDecrypt(cfb , plaintext + i, blockSize,  out + i);   
				if (IsPGPError(err))
					goto done; 	
			}
/////////////////////////////////////////////////////////////////
/*
			err = PGPCopySymmetricCipherContext(cipher, &cipher1);
			if (IsPGPError(err))
				goto done; 	
			err = PGPNewCFBContext(cipher1, 1, &cfb);
			if (IsPGPError(err))
				goto done; 	
			err = PGPCFBGetSymmetricCipher(cfb, &cipher1);
			if (IsPGPError(err))
				goto done; 	
			cipher1	= kInvalidPGPSymmetricCipherContextRef;
			// init CFB with IV and Key //
			err = PGPInitCFB(cfb,  key , initvector); 
			if (IsPGPError(err))
				goto done; 	
			// Save a copy for decryption later //
			// err = PGPCopyCFBContext(cfb, &cfb1);*/
			break;
	}

 ////////////////////////////////////////////////////////////
/*
	// Perform Decryption according to mode
	for(i = 0; i < plaintextlen ; i += blockSize)
		switch(mode)
		{
			case kCipherModeECB:
				err = PGPSymmetricCipherDecrypt(cipher , plaintext + i, out + i);  
			if (IsPGPError(err))
				goto done; 	
				break;
			
			case kCipherModeCBC:
				err = PGPCBCDecrypt(cbc , plaintext + i, blockSize,  out + i); 
			if (IsPGPError(err))
				goto done; 	
				break;
				
			case kCipherModeCFB:
				err =  PGPCFBDecrypt(cfb , plaintext + i, blockSize,  out + i);   
			if (IsPGPError(err))
				goto done; 	
				break;
		}
 */

  	memcpy(plaintext , out , plaintextlen) ;
done:	
 	if(out) 
		PGPFreeData(out);

	if( PGPSymmetricCipherContextRefIsValid( cipher ) )
			(void) PGPFreeSymmetricCipherContext( cipher );

	if( PGPCFBContextRefIsValid( cfb ) )
		(void) PGPFreeCFBContext(cfb);

	if( PGPCBCContextRefIsValid( cbc ) )
		(void) PGPFreeCBCContext(cbc);	

	//if( PGPSymmetricCipherContextRefIsValid( cipher1 ) )
	//		(void) PGPFreeSymmetricCipherContext( cipher1 );
		
	//if( PGPCFBContextRefIsValid( cfb1 ) )
	//	(void) PGPFreeCFBContext(cfb1);
		
	//if( PGPCBCContextRefIsValid( cbc1 ) )
	//	(void) PGPFreeCBCContext(cbc1);			
	return err;

}





///////////////////////////////////////////////////////////////////////////////
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
pgputil::pgputil()
{
	PGPError err = kPGPError_NoErr; 
	err = OnInit(kPGPFlags_ForceLocalExecution | kPGPFlags_SuppressCacheThread) ;
}

pgputil::pgputil(std::string pkeyfile ,std::string skeyfile,std::string strpwd)
{	
	PGPError err = kPGPError_NoErr; 
	err = OnInit(kPGPFlags_ForceLocalExecution | kPGPFlags_SuppressCacheThread) ;


}


pgputil::pgputil(std::string strkeyfile /*, std::string strEmailAddress*/ , std::string strpwd)
{	
	PGPError err = kPGPError_NoErr; 
	err = OnInit(kPGPFlags_ForceLocalExecution | kPGPFlags_SuppressCacheThread) ;
	err = ImportKeys(m_gContext , strkeyfile.c_str() , kPGPInputFormat_PGP , &m_gKeyDB) ;
    m_gPwd = strpwd ;
	InitDecodeInfo(&m_gDecodeInfo);
	err = PGPCountKeysInKeyDB(m_gKeyDB, &m_gnumKeys);

	//err = PGPNewKeyDBObjDataFilter(m_pContext , kPGPUserIDProperty_Name , 
	//strUserID , strUserID.GetLength() , kPGPMatchCriterion_SubString , &m_gFilter) ;
    //
	//err = PGPNewKeyDBObjDataFilter(m_gContext , kPGPUserIDProperty_EmailAddress , strEmailAddress.c_str() , 
	//	strEmailAddress.length() , kPGPMatchCriterion_SubString , &m_gFilter) ;


    //err = PGPFilterKeyDB(m_gKeyDB , m_gFilter , &m_guserKeySet) ;
    //	err = PGPCountKeys(m_guserKeySet, &m_gnumKeys);

	if(m_gnumKeys)
	{
		PGPKeyIterRef   iter	= kInvalidPGPKeyIterRef;
		PGPKeyDBObjRef  aKey	= kInvalidPGPKeyDBObjRef;
		
		//err = PGPNewKeyIterFromKeySet( m_guserKeySet , &iter);
		err = PGPNewKeyIterFromKeyDB(m_gKeyDB , &iter);		
		PGPKeyID  keyID;	
		int i = 0 ;
		//PGPChar8 outString[ kPGPMaxKeyIDStringSize ];
		while( IsntPGPError( PGPKeyIterNextKeyDBObj( iter, kPGPKeyDBObjType_Key, &aKey) ) )
		{
			PGPGetKeyID( aKey, &keyID );	
			//PGPGetKeyIDString( &keyID, kPGPKeyIDString_Full, outString);
			memcpy( &m_gDecodeInfo.key[i].keyID , &keyID , sizeof(PGPKeyID));
			m_gDecodeInfo.key[i].passPhrase = m_gPwd.c_str() ;
			if (i == 0)
				err = PGPGetKeyForUsage( aKey ,kPGPKeyPropertyFlags_UsageSignMessages , &m_gSignKey);
			if (m_gnumKeys == i + 1)
				err = PGPNewOneKeySet(aKey, &m_guserKeySet);  
	
			++i ;
		}			
		if( PGPKeyIterRefIsValid( iter ) ) 
			PGPFreeKeyIter( iter );
		m_gDecodeInfo.keyCount = i ;
	}
	m_gDecodeInfo.option = kDecode_NoOption ;
}

pgputil::~pgputil() 
{
	Clearup();
}

bool pgputil::pk_enc(const std::vector<BYTE>& in,std::vector<BYTE>& out)
{
	PGPError err = kPGPError_NoErr; 
	PGPOptionListRef		encodeOptions	= kInvalidPGPOptionListRef;
	err = PGPNewOptionList(m_gContext, &encodeOptions);
    BYTE *pBuf = NULL ;
	PGPSize nBufSize = 0 ;
	err =  PGPEncode( m_gContext, 
			PGPOInputBuffer ( m_gContext, &in[0] , in.size() ),
			PGPOAllocatedOutputBuffer( m_gContext, (void**)&pBuf, MAX_PGPSize, &nBufSize),
		    //PGPOSignWithKey (	m_gContext	, m_gSignKey,
						//		PGPOPassphrase( m_gContext, m_gPwd.c_str()) , 
 					//			PGPOLastOption ( m_gContext ) ) ,
 			PGPOEncryptToKeySet(m_gContext, m_guserKeySet) ,
  			PGPOOutputFormat(m_gContext, kPGPOutputFormat_PGP),
			encodeOptions,	
			PGPOLastOption( m_gContext ) );
	if(PGPOptionListRefIsValid(encodeOptions))
		PGPFreeOptionList(encodeOptions);
	if (nBufSize < 1)
		return false ;
	out.insert(out.begin() , pBuf , pBuf + nBufSize) ;
	if( pBuf != NULL)
		PGPFreeData(pBuf);
	return true ;
}

bool pgputil::pk_dec(const std::vector<BYTE>& in,std::vector<BYTE>& out)
{
	PGPError err = kPGPError_NoErr; 
    BYTE *pBuf = NULL ;
	PGPSize nBufSize = 0 ;
	err = PGPDecode( m_gContext, 
			PGPOInputBuffer ( m_gContext, &in[0] , in.size() ),
			PGPOAllocatedOutputBuffer( m_gContext, (void**)&pBuf, MAX_PGPSize, &nBufSize),
			PGPOEventHandler( m_gContext, OptestEventHandler, &m_gDecodeInfo),			
  			PGPOKeyDBRef(m_gContext, m_gKeyDB),			
 			PGPOLastOption( m_gContext ) );
	if (nBufSize < 1)
		return false ;
	out.insert(out.begin() , pBuf , pBuf + nBufSize) ;
	if( pBuf != NULL)
		PGPFreeData(pBuf);
	return true ;
}

bool pgputil::sym_enc(const std::vector<BYTE>& in,const std::vector<BYTE>& key,std::vector<BYTE>& out,algos lg )
{
	PGPError			err		= kPGPError_NoErr;
	PGPFlags			flags;	
	PGPBoolean			fipsMode = FALSE;
	PGPCipherAlgorithm	algor ;
/*
	kPGPCipherAlgorithm_None		= 0,
	kPGPCipherAlgorithm_IDEA		= 1,
	kPGPCipherAlgorithm_3DES		= 2,
	kPGPCipherAlgorithm_CAST5		= 3,
	kPGPCipherAlgorithm_Blowfish	= 4,
	kPGPCipherAlgorithm_AES128		= 7,
	kPGPCipherAlgorithm_AES192		= 8,
	kPGPCipherAlgorithm_AES256		= 9,
	kPGPCipherAlgorithm_Twofish256	= 10,
*/
	err = PGPGetFeatureFlags(kPGPFeatures_GeneralSelector, &flags); 
	if (IsPGPError(err))
		return false ; 	
	switch(lg)
	{
		case algo_aes :
			algor = kPGPCipherAlgorithm_AES128	;//	= 7,
//			algor = kPGPCipherAlgorithm_AES192	;//	= 8,
//			algor = kPGPCipherAlgorithm_AES256	;//	= 9,
			break ;
		case algo_des :
			algor = kPGPCipherAlgorithm_3DES	;//	= 2,
			break ;
	}

	PGPSize nSize = key.size() ;
	BYTE *pKey = new BYTE[nSize] ;
	memcpy(pKey , &key[0] , nSize) ;
	nSize = in.size() ;
	BYTE *pBuf = new BYTE[nSize] ;
	memcpy(pBuf , &in[0] , nSize) ;

	PGPByte initVect8[] = {0x01, 0x06, 0x0B, 0x10, 0x15, 0x1A, 0x1F, 0x24};
	PGPByte initVect16[] = {0x0A, 0x0B, 0x0C, 0x0D, 0x0F, 0x10, 0x11, 0x12, 0x14, 0x15, 0x16, 0x17, 0x19, 0x1A, 0x1B, 0x1C};

//kCipherModeECB, kCipherModeCBC, kCipherModeCFB
	fipsMode =  PGPFeatureExists(flags, kPGPFeatureMask_FIPSmode);
	err = pgpCipherEncrypt(m_gContext , algor , kCipherModeECB , pKey , pBuf , nSize , initVect16); 
    if (pKey != NULL)
		delete []pKey ;
	pKey = NULL ;
	if (IsPGPError(err))
	{	
		if( pBuf != NULL)
			delete []pBuf ;
		pBuf = NULL ;
		return false ; 	
	}
	out.insert(out.begin() , pBuf , pBuf + nSize) ;
	if( pBuf != NULL)
		delete []pBuf ;
	pBuf = NULL ;
	return true ;
}

bool pgputil::sym_dec(const std::vector<BYTE>& in,const std::vector<BYTE>& key,std::vector<BYTE>& out,algos lg)
{
	PGPError			err		= kPGPError_NoErr;
	PGPFlags			flags;	
	PGPBoolean			fipsMode = FALSE;
	PGPCipherAlgorithm	algor ;
/*
	kPGPCipherAlgorithm_None		= 0,
	kPGPCipherAlgorithm_IDEA		= 1,
	kPGPCipherAlgorithm_3DES		= 2,
	kPGPCipherAlgorithm_CAST5		= 3,
	kPGPCipherAlgorithm_Blowfish	= 4,
	kPGPCipherAlgorithm_AES128		= 7,
	kPGPCipherAlgorithm_AES192		= 8,
	kPGPCipherAlgorithm_AES256		= 9,
	kPGPCipherAlgorithm_Twofish256	= 10,
*/
	err = PGPGetFeatureFlags(kPGPFeatures_GeneralSelector, &flags); 
	if (IsPGPError(err))
		return false ; 	
	switch(lg)
	{
		case algo_aes :
			algor = kPGPCipherAlgorithm_AES128	;//	= 7,
//			algor = kPGPCipherAlgorithm_AES192	;//	= 8,
//			algor = kPGPCipherAlgorithm_AES256	;//	= 9,
			break ;
		case algo_des :
			algor = kPGPCipherAlgorithm_3DES	;//	= 2,
			break ;
	}

	PGPSize nSize = key.size() ;
	BYTE *pKey = new BYTE[nSize] ;
	memcpy(pKey , &key[0] , nSize) ;
	nSize = in.size() ;
	BYTE *pBuf = new BYTE[nSize] ;
	memcpy(pBuf , &in[0] , nSize) ;

	PGPByte initVect8[] = {0x01, 0x06, 0x0B, 0x10, 0x15, 0x1A, 0x1F, 0x24};
	PGPByte initVect16[] = {0x0A, 0x0B, 0x0C, 0x0D, 0x0F, 0x10, 0x11, 0x12, 0x14, 0x15, 0x16, 0x17, 0x19, 0x1A, 0x1B, 0x1C};

//kCipherModeECB, kCipherModeCBC, kCipherModeCFB
	fipsMode =  PGPFeatureExists(flags, kPGPFeatureMask_FIPSmode);
	err = pgpCipherDecrypt(m_gContext , algor , kCipherModeECB , pKey , pBuf , nSize , initVect16); 
    if (pKey != NULL)
		delete []pKey ;
	pKey = NULL ;
	if (IsPGPError(err))
	{	
		if( pBuf != NULL)
			delete []pBuf ;
		pBuf = NULL ;
		return false ; 	
	}
	out.insert(out.begin() , pBuf , pBuf + nSize) ;
	if( pBuf != NULL)
		delete []pBuf ;
	pBuf = NULL ;
	return true ;
}

⌨️ 快捷键说明

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