📄 csecret.cpp
字号:
memcpy(info->sessionKey , d->sessionKey , d->sessionKeyLength) ;
}
break ;
case kPGPEvent_EncryptionEvent :// = 24, /*----- Encryption Event----- Encryption data report */
//输出加密算法类型 密钥长度内容
{
PGPEventEncryptionData *d = &event->data.encryptionData;
char * cipher = cipher_algor_table(d->cipherAlgorithm);
}
break ;
case kPGPEvent_ToBeSignedEvent :// = 25, /* To-be-signed hash */
break ;
}
done :
return err ;
}
CSecret::CSecret()
{
m_pContext = kInvalidPGPContextRef;
}
CSecret::~CSecret()
{
}
bool CSecret::OnInit()
{
PGPError err = kPGPError_NoErr;
err = PGPsdkInit(kPGPFlags_ForceLocalExecution | kPGPFlags_SuppressCacheThread); //
err = PGPNewContext(kPGPsdkAPIVersion, &m_pContext);
/* make sure we have enough entropy */
err = ConsoleAcquireEntropy(m_pContext, PGPGlobalRandomPoolGetMinimumEntropy() / 8 , NULL, FALSE);
if (IsPGPError(err))
return false ;
else
return true ;
}
PGPError CSecret::ConsoleAcquireEntropy(PGPContextRef context , PGPUInt32 entropyNeeded ,PGPUInt32 * pEntropyAcquired ,PGPBoolean bOutputProgress)
{
PGPError err = kPGPError_NoErr;
PGPUInt32 entropyAcquired = 0;
time_t start = 0;
time_t current = 0;
if (entropyNeeded == 0)
return (kPGPError_NoErr);
time (&start);
while (entropyAcquired < entropyNeeded)
{
entropyAcquired = PGPContextReserveRandomBytes (context, entropyNeeded);
err = PGPGlobalRandomPoolAddSystemState ();
if (IsntPGPError (err))
goto done ;
time (¤t);
if (current > (start + 10))
break;
}
if (entropyAcquired < entropyNeeded)
return (kPGPError_OutOfEntropy);
done:
if (IsntPGPError (err))
{
if (pEntropyAcquired)
*pEntropyAcquired = entropyAcquired;
}
return (err);
}
void CSecret::Clearup()
{
if (PGPContextRefIsValid(m_pContext))
PGPFreeContext(m_pContext) ;
PGPsdkCleanup() ;
}
PGPError CSecret::importKeys (PGPContextRef context, const PGPChar8 *path, PGPInputFormat format, PGPKeyDBRef *keyDB )
{
PGPError err = kPGPError_NoErr;
PGPFileSpecRef fileSpec = kInvalidPGPFileSpecRef;
/* Create filespec to keys */
err = PGPNewFileSpecFromFullPath(context, path, &fileSpec);
if (IsPGPError(err))
{
if( PGPFileSpecRefIsValid(fileSpec))
PGPFreeFileSpec(fileSpec);
return err;
}
/* Read in the test key and get a ref to it */
err = PGPImport( context, keyDB,
PGPOInputFile( context, fileSpec ),
PGPOInputFormat( context, format),
PGPOLastOption( context ) );
if( PGPFileSpecRefIsValid(fileSpec))
PGPFreeFileSpec(fileSpec);
return err;
}
PGPError CSecret::importKeys (PGPContextRef context, const PGPChar8 *path, PGPFileSpecRef &fileSpec )
{
PGPError err = kPGPError_NoErr;
/* Create filespec to keys */
err = PGPNewFileSpecFromFullPath(context, path, &fileSpec);
return err;
}
PGPError CSecret::EncryptingFile(const CString &strUserID ,
const CString &strCer ,
const CString &strPlain ,
const CString &strScecret)
{
PGPError err = kPGPError_NoErr;
PGPKeyDBRef keyDB = kInvalidPGPKeyDBRef ;
PGPFilterRef filter = kInvalidPGPFilterRef ;
PGPUInt32 numKeys;
PGPKeySetRef userKeySet = kInvalidPGPKeySetRef ;
PGPFileSpecRef inFileRef = kInvalidPGPFileSpecRef;
PGPFileSpecRef outFileRef = kInvalidPGPFileSpecRef;
// DecodeInfo decodeInfo;
// InitDecodeInfo(&decodeInfo);
err = importKeys(m_pContext , strCer , kPGPInputFormat_PGP , &keyDB) ;
if (IsPGPError(err))
{
goto EXIT ;
}
err = PGPCountKeysInKeyDB(keyDB, &numKeys);
err = PGPNewKeyDBObjDataFilter(m_pContext ,/*kPGPUserIDProperty_EmailAddress kPGPUserIDProperty_CommonName */ kPGPUserIDProperty_Name , strUserID , strUserID.GetLength() , kPGPMatchCriterion_SubString , &filter) ;
if (IsPGPError(err))
{
goto EXIT ;
}
err = PGPFilterKeyDB(keyDB , filter , &userKeySet) ;
err = PGPCountKeys(userKeySet, &numKeys);
if (IsPGPError(err))
{
goto EXIT ;
}
PGPOptionListRef encodeOptions = kInvalidPGPOptionListRef;
PGPOptionListRef savedOptions = kInvalidPGPOptionListRef;
PGPOptionListRef moreOptions = kInvalidPGPOptionListRef;
PGPKeySetRef encryptSet = kInvalidPGPKeySetRef;
PGPBoolean hideRecipents = TRUE;
err = PGPNewOptionList(m_pContext, &encodeOptions);
/*
err = PGPAppendOptionList(encodeOptions,
PGPOCommentString ( m_pContext, "FIPS 140-2 Test message"),
PGPOLastOption( m_pContext ) );
if (IsPGPError(err))
{
goto EXIT ;
}
err = PGPBuildOptionList(m_pContext, &moreOptions,
PGPOEventHandler( m_pContext, OptestEventHandler, NULL),
PGPOForYourEyesOnly(m_pContext,TRUE),
PGPODataIsASCII ( m_pContext, FALSE),
(hideRecipents?PGPOObfuscateRecipients(m_pContext, TRUE) : PGPONullOption(m_pContext)),
PGPOOutputLineEndType ( m_pContext,kPGPLineEnd_LF),
PGPOArmorOutput(m_pContext, TRUE),
PGPOLastOption( m_pContext ) );
if (IsPGPError(err))
{
goto EXIT ;
}
err = PGPAppendOptionList( encodeOptions, moreOptions, PGPOLastOption( m_pContext ) );
if (IsPGPError(err))
{
goto EXIT ;
}
err = PGPCopyOptionList(encodeOptions, &savedOptions);
if (IsPGPError(err))
{
goto EXIT ;
}
*/
void *cBuf = NULL;
char* pBuf = NULL ;
PGPSize cBufSize;
char char10[] = "asdafdadf" ;
static const char MessageToBeSigned[] = "Our challen";
err = PGPEncode( m_pContext,
PGPOInputBuffer ( m_pContext, char10, sizeof(char10)-1 ),
//PGPOInputBuffer ( m_pContext, MessageToBeSigned, sizeof(MessageToBeSigned)-1 ),
PGPOAllocatedOutputBuffer( m_pContext, (void**)&pBuf, MAX_PGPSize, &cBufSize),
//PGPOSignWithKey ( m_pContext, signKey,
// PGPOPassphrase( m_pContext, kBobsPassPhrase),
// PGPOLastOption ( m_pContext ) ),
PGPOEncryptToKeySet(m_pContext, userKeySet),
//PGPOEncryptToKeyDBObj(m_pContext , keyDB) ,
PGPOOutputFormat(m_pContext, kPGPOutputFormat_PGP),
encodeOptions,
PGPOLastOption( m_pContext ) );
/////////////////////////////////////////
//*
err = PGPDecode(m_pContext ,
PGPOInputBuffer(m_pContext , (void*)pBuf , cBufSize) ,
//PGPOInputFile(m_pContext , inFileRef) ,
// PGPOOutputBuffer(m_pContext , pBuf , sizeof(pBuf) , &cBufSize) ,
PGPOAllocatedOutputBuffer( m_pContext, &cBuf, MAX_PGPSize, &cBufSize),
// PGPOOutputFile(m_pContext , outFileRef) ,
//PGPOEncryptToKeySet(m_pContext , userKeySet) ,
PGPOKeyDBRef(m_pContext , keyDB) ,
// PGPOOutputFormat(m_pContext, kPGPOutputFormat_PGP),
// encodeOptions,
PGPOLastOption(m_pContext)) ;
//*/
EXIT:
if( PGPFileSpecRefIsValid(inFileRef))
PGPFreeFileSpec(inFileRef);
if( PGPFileSpecRefIsValid(outFileRef))
PGPFreeFileSpec(outFileRef);
if (PGPFilterRefIsValid(filter))
PGPFreeFilter(filter) ;
if (PGPKeySetRefIsValid(userKeySet))
PGPFreeKeySet(userKeySet) ;
if (PGPKeyDBRefIsValid(keyDB))
PGPFreeKeyDB(keyDB) ;
return err ;
}
PGPError CSecret::EncryptingFile(const CString &strUserID ,
const CString &strPublic ,
const CString &strPrivate ,
const CString &strPlain ,
const CString &strScecret)
{
PGPError err = kPGPError_NoErr;
PGPKeyDBRef keyDB = kInvalidPGPKeyDBRef ;
PGPFilterRef filter = kInvalidPGPFilterRef ;
PGPKeySetRef userKeySet = kInvalidPGPKeySetRef ;
PGPFileSpecRef pubFileRef = kInvalidPGPFileSpecRef;
PGPFileSpecRef secFileRef = kInvalidPGPFileSpecRef;
PGPFileSpecRef inFileRef = kInvalidPGPFileSpecRef;
PGPFileSpecRef outFileRef = kInvalidPGPFileSpecRef;
PGPUInt32 numKeys;
err = importKeys(m_pContext , strPublic , pubFileRef) ;
if (IsPGPError(err))
{
goto EXIT ;
}
err = importKeys(m_pContext , strPrivate , secFileRef) ;
if (IsPGPError(err))
{
goto EXIT ;
}
err = PGPOpenKeyDBFile(m_pContext , kPGPOpenKeyDBFileOptions_Mutable , pubFileRef , secFileRef , &keyDB) ;
if (IsPGPError(err))
{
goto EXIT ;
}
err = PGPCountKeysInKeyDB(keyDB, &numKeys);
err = PGPNewKeyDBObjDataFilter(m_pContext , /*kPGPUserIDProperty_CommonName*/kPGPUserIDProperty_Name , strUserID , strUserID.GetLength() , kPGPMatchCriterion_SubString , &filter) ;
if (IsPGPError(err))
{
goto EXIT ;
}
err = PGPFilterKeyDB(keyDB , filter , &userKeySet) ;
err = PGPCountKeys(userKeySet, &numKeys);
if (IsPGPError(err))
{
goto EXIT ;
}
err = importKeys(m_pContext , strPlain , inFileRef) ;
if (IsPGPError(err))
{
goto EXIT ;
}
err = importKeys(m_pContext , strScecret , outFileRef) ;
if (IsPGPError(err))
{
goto EXIT ;
}
/////////////////////////////////////////
err = PGPEncode(m_pContext , PGPOEncryptToKeySet(m_pContext , userKeySet) ,
PGPOInputFile(m_pContext , inFileRef) ,
PGPOOutputFile(m_pContext , outFileRef) ,
PGPOLastOption(m_pContext)) ;
EXIT:
if( PGPFileSpecRefIsValid(pubFileRef))
PGPFreeFileSpec(pubFileRef);
if( PGPFileSpecRefIsValid(secFileRef))
PGPFreeFileSpec(secFileRef);
if( PGPFileSpecRefIsValid(inFileRef))
PGPFreeFileSpec(inFileRef);
if( PGPFileSpecRefIsValid(outFileRef))
PGPFreeFileSpec(outFileRef);
if (PGPFilterRefIsValid(filter))
PGPFreeFilter(filter) ;
if (PGPKeySetRefIsValid(userKeySet))
PGPFreeKeySet(userKeySet) ;
if (PGPKeyDBRefIsValid(keyDB))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -