encryption of .mat files

85 views (last 30 days)
khaled DAWOUD
khaled DAWOUD on 17 Jul 2012
Answered: David Leffingwell on 12 Jun 2023
*hello , i have stored the data of my application in .mat file , i edit and change the values of the variables throw my application , i don't want the other user to see the name of variables or the values , any suggested function or a way to encrypt the file ??
Thanks ALL*

Accepted Answer

Jan
Jan on 18 Jul 2012
Edited: Jan on 18 Jul 2012
You can implement a string encryption method by your own. Using Java you need 3 or 4 lines of code only. Unfortuantely the export laws do not allow to publish the code on a server located in the USA, because evil countries could use it to encrypt their evil data also.
But if you ask your favourite search engine for "MATLAB AES encryption", your are still successful, I think. If you are a citizen of an evil country, please do not ask a search engine. (Any additional details of my personal opinion about the US export restrictions would be off-topic.)
Then you have the standard problem, that the encryption key must be stored reliably also. Never store the password in clear text, even not in P-coded files, otherwise the decryption gets near to trivial and a dull ROT13 method would offer the same level of security.
Another problem appears, when you need to process MAT-files: You can write the MAT file in clear text, encrypt it and ship it to the users. But then you cannot load the file directly. You have to decrypt the file at first and write it to the harddisk before load can import the data. But then you need a very sophisticated method to remove the clear-text data from the disk afterwards. I'm convinced, that you cannot reach a 95% security level by this way.
You find tools to "serialize" objects in the FEX (use this term to search there). This means, that e.g. a struct can be converted to a byte stream, which can be encrypted easily. Then the decryption can happen in the memory as well as the "deserialization". In consequence the hard disk remains clean and the clear text data can only be found using a memory dump. When the password is kept securely, this means a security level of 99% (in arbitrary units...).
  3 Comments
Matt Kindig
Matt Kindig on 18 Jul 2012
An SQL database suffers the same problem as any other password-protection-- namely, if you can crack the password, you can access the data in the database. Obviously different SQL databases have different security approaches, and I don't know enough about them to offer anything further.
If the software that you have found to do the encryption/decryption can be operated from the Windows/Linux command line, you can access it using Matlab's system() command. You should then be able to run your software from within your .m file.
Walter Roberson
Walter Roberson on 18 Jul 2012
Be cautious, though: if the software takes the encryption key on the command line, then someone monitoring the processes will be able to see the encryption key when you use system() or dos() or unix().
If you store the password in a file and redirect reading of the key from the file, then you have to ensure that the file is never readable by the user, which is tricky in MS Windows (and requires care and experience in Unix)
So you need SSL. Or need to use pipes.
Perhaps you should consider using your own binary format instead of .mat files.

Sign in to comment.

More Answers (4)

Matt Kindig
Matt Kindig on 18 Jul 2012
Edited: Matt Kindig on 18 Jul 2012
Hi Khaled,
This hasn't gotten any bites yet, so I thought I'd take a stab at it, although I am not an encryption expert by any means.
What access do you want the "other user" to have to your data? As I understand it, you basically want to encrypt your file such that someone else cannot open and read your data. I'm not sure how you'll be able to do that in a *.mat file-- if you use Matlab to save the data in MAT format, the file will be written in such a way that it can be opened with Matlab, as that is the purpose of the file format. I don't believe that Matlab has an equivalent of Excel's "Protect Workbook" or similar. You can encrypt m-files such that they can be executed without revealing the source code (using the 'pcode' function), but that is obviously something different than you want.
If you want to store your data in a format that is available for reading only for yourself (or a limited number of people), you are probably better off writing to a binary file and then using one of a number of standard encryption codes. A quick search of the File Exchange indicates a few encryption functions--Google I'm sure has countless more. How secure do you need the encryption?
Matt
  2 Comments
khaled DAWOUD
khaled DAWOUD on 18 Jul 2012
Edited: khaled DAWOUD on 18 Jul 2012
First i would like to Thank you Matt for your reply and help i really appreciated . actually this very sensitive point when it comes to protected you application data away from users . i want to explain in more details how actually matlab compiler works with .mat files
---------------------------------------------------------------
in my application i load and save the data to .mat file the values of the variables change Through running the program . The problem here is this
if i compiled my application and i added the .mat file to .ctf then i can't edit the values of variables stored in .mat file and save them to later use
if i compiled my application and put the .mat file in folder that contain .exe manually then i can edit and store new values to the variables that stored in the .mat file ( The problem here the users can see contents of the file and edit it outside the application by importing it to the matlab and save new values to the file)
so The .mat files they are not protected unless we encrypt them by ourselves.(This is waht Mathworks says )
------------------------------------------------------------------
i want to write a function that encrypt the .mat file with a password and i'll need another function to decrypt the .mat file (so when i first run the application i'll will decrepit the .mat and when i exit i'll encrypted again ?? any suggestion or ideas about how i can do that ?
Thanks ALL
Mahmoud
Mahmoud on 21 Aug 2016
Dear Khaled, Have you found any solution for your problem?

Sign in to comment.


per isakson
per isakson on 18 Jul 2012
Edited: per isakson on 18 Jul 2012
Matlab documentation:
New MAT-File Format Based on HDF5
In Version 7.3 (R2006b), you can save MAT-files in a format based on HDF5.
The v7.3 mat-file is documented and can be inspected with the hdf5-tools:
>> h5disp( 'mk.mat')
HDF5 mk.mat
Group '/'
Group '/#refs#'
Dataset 'a'
....
HDF5 is open and well supported.
In the "The Data Transfer Pipeline" of HDF5 it is possible to include your own code for encryption and decryption (and more common compression).
Thus, it should be doable - maybe.
  2 Comments
khaled DAWOUD
khaled DAWOUD on 18 Jul 2012
plz can you provide more explanation on this way ? for the source code i prefer to use matlab compiler , but i want to be sure .mat files is in safe ? Thanks
per isakson
per isakson on 18 Jul 2012
Edited: per isakson on 19 Jul 2012
I picked up the info above from reading the documentation and hdf-forum. I cannot add much. Bad news:
  • the size of the "v7.3 file" is much larger than the compressed default file
  • might work for values but maybe not for names.
If you are serious about it why not ask tech-support.

Sign in to comment.


khaled DAWOUD
khaled DAWOUD on 18 Jul 2012
another solution i found is that you can read and write your data to protected excel sheet or access database, but the problem here is that there many softwares and tricks available on the web that breaks the protection of these files .(not security enough)

David Leffingwell
David Leffingwell on 12 Jun 2023
If you are using the MATLAB Compiler, you can use the -s option to encrypt your data (e.g. MAT files) in addition to your M files. See here.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!