I have some data in CSV file, say data.csv with columns age, sex, country and status. Is it possible I could encrypt the entire CSV file some way which allows me to fetch only selected columns, say country and status, from the encrypted file and then decrypt to get back only those fields in clear text?

I am talking something similar to column-based encryption available in some databases but I cannot use any databases.

One idea I have is while encryption i encrypt each field column one by one, put a delimeter and then encrypt other field and append to the previously encrypted field but i am certain its a bad approach.

  • When you encrypt the whole file, decryption of specific fields will be problematic. It looks like for your use case need to protect parts of your file where secret info is located.
    – VovCA
    Commented Apr 4, 2018 at 18:14
  • What kind of encryption ? A hash function, what exactly ?
    – game0ver
    Commented Apr 4, 2018 at 21:10
  • Hash function is one way function, it's not encryption. Encryption would be probably something like AES 128 CBC or AES 128 CTR modes.
    – VovCA
    Commented Apr 4, 2018 at 21:32
  • Can you clarify your requirements a bit? Why not just decrypt the whole file, but only use the column you are interested in?
    – Anders
    Commented Apr 5, 2018 at 14:40
  • I am working on Intel SGX where the enclave has limited memory, I do wanted to bring the entire csv inside enclave and do the decryption as the entire csv file is too large. I am using aes gcm 256. Commented Apr 5, 2018 at 14:42

1 Answer 1


Transpose the CSV before encrypting. So each column is its own contiguous block.

So the CSV:


is stored as three blocks. The format of these only requires that you have some way of finding the beginning of each column in the file and that you have some way of delimiting values. I am not specifying an encoding. But one could use CSV encoding for the values where the spaces in the following are actually commas and the values are a single line.

a 1 4 7
b 2 5 8
c 3 6 9

Each of the lines represents a block where the part in brackets is encrypted:

a [1 4 7]
b [2 5 8]
c [3 6 9]

Then you can iterate over the rows by doing decryption steps on each column and merging their output to produce rows as the values may be of variable length the amount of decryption steps on a column depends on the length of the value in that column.

  • So you mean to say I encrypt line by line after transpose? Commented Apr 6, 2018 at 9:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .