Xceed Encryption Library Documentation
Examples / Digitally singning a file (VC++ streaming example)
    Digitally singning a file (VC++ streaming example)
    VC++ Copy Code

    // This example signs data using the RSA method from a file to memory in 8K (8192 byte) chunks. 

    // This code uses the #import directive.
    // Put the following line at the beginning of your module

    #import "XCEEDCRY.DLL" no_namespace named_guids 

    CoInitialize( NULL ); 

    try
    {
      const long CHUNKSIZE = 8192;  

      IXceedSigningPtr piSign; 

      piSign.CreateInstance( CLSID_XceedSigning ); 
      piSign->License( _bstr_t( L"your license key ) ); 

      IXceedRSASigningMethodPtr piRSA; 

      piRSA.CreateInstance( CLSID_XceedRSASigningMethod );  

      // The following call to SetRandomKeyPair would, in the real world,  
      // be replaced by the assignment of a Private Key to the  
      // PrivateKey property.  

      piRSA->SetRandomKeyPair( 512, NULL, 0 ); 

      piSign->SigningMethod = IXceedSignDataPtr( piRSA );  

      BYTE* pcSignature = NULL; 
      short nSignatureSize; 
      DWORD dwBytesRead; 
      long lOffset = 0;  

      HANDLE hFile = CreateFile( "c:\\temp\\source.txt", 0, 
                                 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );  

      if( hFile == INVALID_HANDLE_VALUE ) 
        throw;  

      long lFileSize = GetFileSize( hFile, NULL ); 
      CloseHandle( hFile );  

      if( lFileSize == 0xFFFFFFFF ) 
        throw;  

      while( lOffset < lFileSize ) 
      { 
        // bEndOfData will be true if the current offset + CHUNKSIZE exceeds the end of the file. 
        piSign->ReadFile( "c:\\temp\\source.txt", lOffset, CHUNKSIZE, efpSign, 
                          (lOffset + CHUNKSIZE >= lFileSize), &dwBytesRead );  

        lOffset += dwBytesRead; 
      }  

      piRSA->GetSignature( &pcSignature, &nSignatureSize );  

      //Do something with the Signature...  

      CoTaskMemFree( pcSignature );  

      MessageBox( NULL, "File signed successfully to memory", "Signing result", MB_OK );  

      lOffset = 0; 

      while( lOffset < lFileSize ) 
      { 
        // bEndOfData will be true if the current offset + CHUNKSIZE exceeds the end of the file. 
        piSign->ReadFile( "c:\\temp\\source.txt", lOffset, CHUNKSIZE, efpVerify,
                          (lOffset + CHUNKSIZE >= lFileSize), &dwBytesRead );  

       lOffset += dwBytesRead; 
      }  

      MessageBox( NULL, "Data verified successfully", "Verifying result", MB_OK ); 
    }
    catch( const _com_error& xErr )
    {
      if( xErr.Error() == eerVerifyFailed ) 
      { 
        MessageBox( NULL, "Verification of signature failed", "Verifying result", MB_OK ); 
      } 
      else 
      { 
        char szMsg[50]; 
        wsprintf( szMsg, "Error %08x\n", xErr.Error() ); 
        MessageBox( NULL, szMsg, "Error", MB_OK ); 
      } 
    }
    catch( ... )
    {
      MessageBox( NULL, "Unknown error", "Error", MB_OK ); 

    CoUninitialize();