2

A MATLAB .mat file will be shared between 40-50 people and it will include cost numbers. This .mat is used for some elaborate calculation however the cost numbers should not be openly revealed except for a very few (1-2 people out of 40-50).

So the 1-2 people would like to keep the 'exposed' version of this .mat file

a.dim.a = 1
a.dim.b = 2
a.dim.c = 3
a.cost.x = 11
a.cost.y = 12

and then place the 'hidden' version on the shared drive for everyone else.

a.dim.a = 1
a.dim.b = 2
a.dim.c = 3
a.cost.x = ADSAUJ#$#I
a.cost.y = SDHAUWH#@$

Be mindful that m-scripts are working on this .mat file so key-pair encryption isn't right since it's not a situation where we're trying to keep third parties from snooping on our data. It's about making some peoples life a bit difficult but if they worked hard, they could expose the numbers. So I'd like to ask what in your opinion is the best way of doing this?

Sander de Jong
  • 351
  • 6
  • 18
Zaki Mohzani
  • 297
  • 4
  • 11
  • Without any context this is impossible to answer. Couldn't you just duplicate `a` and delete the sensitive parts before sending it? – David Nov 25 '15 at 02:09
  • The sensitive part shouldn't be excluded because it'll be used for some calculation on the customer end, however, we don't want to make it easy for the customer to know the value. We only want the customer to know the final result which depends on this value. – Zaki Mohzani Nov 25 '15 at 08:42

1 Answers1

2

The fact that the data is in a structure is not really relevant, the question is how to encrypt data and unfortunately MATLAB doesn't have encryption functions built-in. But fear not, as they are available in Java - which can accessed from MATLAB.

You can adapt the following to your requirement:

import javax.crypto.Cipher;
% The text to encrypt.
plaintext = 'foobar'; 

% Use RSA    
cipher = Cipher.getInstance('RSA');
% Generate a key pair
keygen = java.security.KeyPairGenerator.getInstance('RSA');
keyPair = keygen.genKeyPair();
cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPrivate());

% Convert your input to bytes
plaintextUnicodeVals = uint16(plaintext);
plaintextBytes = typecast(plaintextUnicodeVals, 'int8');

% Encrypt
ciphertext = cipher.doFinal(plaintextBytes)'  %'

% And decrypt again...
cipher.init(Cipher.DECRYPT_MODE, keyPair.getPublic());
decryptedBytes = cipher.doFinal(ciphertext);
decryptedText = char(typecast(decryptedBytes, 'uint16'))'
Community
  • 1
  • 1
gregswiss
  • 1,456
  • 9
  • 20