# 3DF: How to encrypt HSF file using

**URL:** <https://forum.techsoft3d.com/t/3df-how-to-encrypt-hsf-file-using/1358>\
**Category:** HOOPS Visualize Desktop\
**Tags:** code, sample, learn, knowledge-base, how-to\
**Created:** [February 1, 2021, 5:00am UTC](https://forum.techsoft3d.com/t/3df-how-to-encrypt-hsf-file-using/1358 "2021-02-01T05:00:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabriel.peragine](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.techsoft3d.com/gabriel.peragine/32/1535_2.png) [@gabriel.peragine](https://forum.techsoft3d.com/u/gabriel.peragine)\
**Post date:** [February 1, 2021, 5:00am UTC](https://forum.techsoft3d.com/t/3df-how-to-encrypt-hsf-file-using/1358/1 "2021-02-01T05:00:00Z")

</div>

## **Version**

3DF v.2X.XX

## **Language**

C++

## **Description:**

Below is sample code on how to encrypt your hsf file so that only your application can import hsf file.

- You can use PartViewer to test sample.

```auto
#include "HStream.h"

class encyrptHSF : public BBaseOpcodeHandler 
{
       public:
              encyrptHSF() : BBaseOpcodeHandler(0xB0) {
              };
              ~encyrptHSF() {};

              TK_Status Interpret(BStreamFileToolkit &, ID_Key, int) {
                     return TK_Normal; 
              };
              TK_Status Execute (BStreamFileToolkit &) {
                     return TK_Normal; 
              };     

              TK_Status Read (BStreamFileToolkit & tk) {
                     TK_Status status = TK_Normal;

                     switch (m_stage) {
                           case 0: {
                                  unsigned char bytes[13];

                                  if ((status = GetData (tk, bytes, 13)) != TK_Normal)
                                         return status;
                                  m_stage = -1;
                           } break;

                           default:
                                  return tk.Error();
                     }

                     return TK_Normal; 
              };
              TK_Status Write (BStreamFileToolkit & tk) {
                     TK_Status status = TK_Normal;

                     switch (m_stage) {
                           case 0: {
                                  if ((status = PutOpcode (tk)) != TK_Normal)
                                         return status;
                                  m_stage++;
                           } //nobreak;

                           case 1: {
                                  unsigned char bytes[13] = {0, ')', '(', 0x7F, 0, ';', ' ', 'H', 'S', 'F', ' ', '1', '3'};

                                  if ((status = PutData (tk, bytes, 13)) != TK_Normal)
                                         return status;
                                  m_stage = -1;
                           } break;

                           default:
                                  return tk.Error();
                     }

                     return TK_Normal; 
              };
};

static char const * filename = "D:/encyrptHSFed.hsf";

// Export
void CSolidHoopsView::OnExtraSlot1()
{
       HStreamFileToolkit tk;
       tk.SetPrewalkHandler (new encyrptHSF);

       HC_Open_Segment_By_Key (m_pHView->GetModelKey());
              HTK_Write_Stream_File (filename, &tk);
       HC_Close_Segment ();
}

// Import
void CSolidHoopsView::OnExtraSlot2()
{
       HStreamFileToolkit tk;
       tk.SetOpcodeHandler (0xB0, new encyrptHSF);

       HC_Open_Segment_By_Key (m_pHView->GetModelKey());
              HTK_Read_Stream_File (filename, &tk);
       HC_Close_Segment ();

       m_pHView->FitWorld();
       m_pHView->Update();
}

```
