0

I am using CryptoApi to encrypt a file (asymmetric encryption). Everywthing is ok but when the file is large, it can not encrypt it. I searched and found that I must encrypt block by block. Except for the last block the Final flag in CryptEncrypt function must be false.

I know all of above conception but I dont know how I can to implement them. I mean I dont know how read, encrypt and write block by block. Can you give me a real code example.

Update:

I used the code of this website: http://blogs.msdn.com/b/alejacma/archive/2008/01/28/how-to-generate-key-pairs-encrypt-and-decrypt-data-with-cryptoapi.aspx

user3864147
  • 285
  • 1
  • 3
  • 9

2 Answers2

1

I am writing this solution for programmers who will have this problem in the future:

In this link has been shown how to encrypt large file (block by block):

https://msdn.microsoft.com/en-us/library/windows/desktop/aa382358%28v=vs.85%29.aspx

Note: Somethings must be change when you want to use the above code

1) In encryption, block size must be set to 128 - 11 ( DWORD dwBlockLen = 128 - 11 )

2) In decryption, block size must be set to 128 ( DWORD dwBlockLen = 128 )

Both tested in win 7.

user3864147
  • 285
  • 1
  • 3
  • 9
0

Try something like:

final_flag <- false
repeat
  this_block <- read_next_block(file)
  if (is_EoF(file)) {final_flag <- true }
  encrypt(this_block, final_flag)
until (final_flag == true)

I don't know enough about the C++ file system handling to write a working check for the end of a file, but there should be one in there somethere.

rossum
  • 15,344
  • 1
  • 24
  • 38