EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Callbackdisk st file encryption.

Also by EldoS: CallbackRegistry
A component to monitor and control Windows registry access and create virtual registry keys.
Posted: 08/22/2014 05:10:01
by manish mahor (Basic support level)
Joined: 02/26/2014
Posts: 33


I have compile your sample code for callback disk. it is working fine.

Now i want to encrypt the pointer buffer

void CallbackWrite(CBDisk *Sender, LongLongWord Offset, Pointer Buffer, LongWord Count)

so that after working our .st file remain encrypted.
i have check your all sample code of SecureBlackbox, but i am getting which code sample is suitable for that and how to apply that code only for encryption of pointer buffer / .st file.
Posted: 08/22/2014 05:39:33
by Volodymyr Zinin (EldoS Corp.)

Here it is:

You can use any symmetric algorithm like AES. CallbackDisk always calls the OnRead and OnWrite callbacks with buffer which size is multiple to the sector size (by default it's 512) and offset is always multiple to the sector size. So the encryption block size can be equal to this value.
Posted: 08/22/2014 06:01:23
by manish mahor (Basic support level)
Joined: 02/26/2014
Posts: 33

Thanks for reply

but i am working on only c++.
I am not find any proper solution for that.

So suggest me for c++ if there is any sample code then please provide the information about that and how to use it.
Posted: 08/22/2014 06:52:26
by Vsevolod Ievgiienko (EldoS Corp.)

Hello. Here is a sample code of AES-256 encryption using SecureBlackbox C++ edition:

vector<uint8_t> iv; // initialization vector should be put here
   vector<uint8_t> key; // encryption key should be put here

   TElSymmetricKeyMaterial keyMaterial(NULL);

   TElSymmetricCryptoFactory factory;
   TElSymmetricCryptoHandle hCrypto = factory.CreateInstance(SB_ALGORITHM_CNT_AES256, TSBSymmetricCryptoMode::_cmDefault);

   TElSymmetricCrypto crypto(hCrypto, true);

   char *inBuf = "input text"; // data to encrypt
   int32_t size = 0;

   crypto.Encrypt(inBuf, strlen(inBuf), NULL, size);
   char *outBuf = new char[size]; // output encrypted data
   crypto.Encrypt(inBuf, strlen(inBuf), outBuf, size);

   // work with outBuf encrypted buffer

   delete[] outBuf;
Posted: 08/23/2014 01:32:04
by manish mahor (Basic support level)
Joined: 02/26/2014
Posts: 33

i have just shifted from c++ to dot net and i have made changes in code

Sub CallbackRead(ByVal Sender As CallbackDisk.CBDisk, ByVal Offset As Long, ByRef Buffer As Byte(), ByVal BytesToRead As Integer, ByRef BytesRead As Integer)

        Dim fs As FileStream = CType(Sender.UserData, FileStream)
        fs.Seek(Offset, SeekOrigin.Begin)
        BytesRead = fs.Read(Buffer, 0, BytesToRead)


    End Sub 'CallbackRead

    Sub CallbackWrite(ByVal Sender As CallbackDisk.CBDisk, ByVal Offset As Long, ByVal Buffer As Byte(), ByVal BytesToWrite As Integer)

        Dim a(BytesToWrite) As Byte

        a = Encrypt(Buffer)

        Dim fs As FileStream = CType(Sender.UserData, FileStream)
        fs.Seek(Offset, SeekOrigin.Begin)
        fs.Write(a, 0, BytesToWrite)

    End Sub 'CallbackWrite

and apply simple encryption for testing.

but after successfully mounting i am getting windows error "the device attached to system is not functioning"

Posted: 08/23/2014 01:37:33
by manish mahor (Basic support level)
Joined: 02/26/2014
Posts: 33

This is my code of encryption.
Private Function Encrypt(ByVal clearBytes As Byte()) As Byte()

        Dim b(clearBytes.Length) As Byte

        For lng = 0 To clearBytes.Length - 1
            b(lng) = clearBytes(lng) ^ 100

        Return b

    End Function

    Private Function Decrypt(ByVal cipherBytes As Byte()) As Byte()

        For lng = 0 To cipherBytes.Length - 1
            cipherBytes(0) ^= 100

        Return cipherBytes

    End Function
Posted: 08/23/2014 02:21:18
by Volodymyr Zinin (EldoS Corp.)

It seems something is wrong with encryption/decription. First of all ensure that a block of data passed for the encryption/decription has always the same size and this size is multiple to the sector size. By default the sector size is 512 byte, so the block size can be for example 64, 128, 256, 512. So when to the OnRead and OnWrite callbacks comes a data buffer which is 512 or 1024 or greater size (but it's always to be multiple to the sector size) then you use a cycle where encrypt/decrypt data sequentially with defined by you block size.
Create a test code and try there to encrypt and then decrypt some predefined block of data (for example let it be all zeros). If after the decryption you obtain the same data as before encryption then the algorithm works.
Posted: 08/23/2014 02:27:47
by Eugene Mayevski (EldoS Corp.)

1) The file to which you store the data must be opened in non-buffered mode and writing there is possible only in 512-byte chunks. Are you doing this?

manish2590 wrote:
cipherBytes(0) ^= 100

You have a typo here -- instead of changing cipherBytes(lng) you change cipherBytes(0)

Sincerely yours
Eugene Mayevski
Posted: 08/23/2014 02:56:17
by manish mahor (Basic support level)
Joined: 02/26/2014
Posts: 33


Decrypt code TYPO error was corrected (it was correct in our code, though)

In Insert buttyom following change was done... buffer = 512

mCBDisk.UserData = New FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, 512, FileOptions.None)

I tried with all options of FileOptions like encrypted, SequentialScan etc... but the problem is same.

You can download FileDisc sample after correction here from our site:
Posted: 08/23/2014 03:39:46
by Eugene Mayevski (EldoS Corp.)

We have FileDisk sample in VB.NET. Does it work for you as is ? IF it does, try adding encryption to it.

Sincerely yours
Eugene Mayevski
Also by EldoS: SecureBlackbox
200+ components and classes for digital security, signing, encryption and secure networking.



Topic viewed 10820 times

Number of guests: 1, registered members: 0, in total hidden: 0


Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!