Encrypting parts of a matlab struct [closed] - matlab

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
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?

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'))'

Related

Dont understand the function of cmd_data(ii) = cell2mat(textscan(char(data{i}(ALL_STRT(ii):(ALL_STRT(ii)+4))),'%f')); at all [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
IND_STRT = 0;
ALL_STRT = IND_STRT:12:510;
cmd_data = zeros(length(ALL_STRT),1); %example: x=zeros(1,21) gives you a 1 by 21 matrix
for ii = 1:length(ALL_STRT) %declare variable ii to run from the row of length
if ~isempty(data{i})
cmd_data(ii) = cell2mat(textscan(char(data{i}(ALL_STRT(ii):(ALL_STRT(ii)+4))),'%f'));
end
end
I need to read the EPS from EnduroSat, however i have difficulty understanding the line cmd_data(ii) = cell2mat(textscan(char(data{i}(ALL_STRT(ii):(ALL_STRT(ii)+4))),'%f'));
Im required to utilised MatLab to code and this particular line have an error and i don't understand why.
Whenever you see a complicated line like this in MATLAB, try to break it up.
% find some indices. These values have been selected by the programmer/data, can't say why.
a=ALL_STRT(ii):(ALL_STRT(ii)+4)
% obtain that articular section of the data
b=data{i}(a)
% convert it to a char data type (characters)
c=char(b)
% scan text, and treat them as float
d=textscan(c,'%f')
% the output is a cell array, we want a matrix instead. Make the cell array into a matrix.
cmd_data(ii) = cell2mat(d)
You can read particularly what each of these do better in their documentation pages, and you can see it work if you put a break-point in the code, and see what each of this parts output when you call it. Learn how to debug, is a very very powerful tool

Crypt a message with matlab [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have one question.
I didn't work with MatLab before.
how I read a message from a file. text? and how I crypt and decrypt it?
I need this work for my study project.
To read text:
fid=fopen(filename,'r');
text=fread(fid,'*char')';
fclose(fid);
To encrypt you can use whatever encrpytion suite you want. look here for aes implementation.
Very simple enc/dec algorithem is xoring the text with some key:
filename=('my_cypher.m');
key='Justin Bieber'; %some very secret key
fprintf('\n------------Text-------------\n');
fid=fopen(filename,'r');
text=fread(fid,'*char')';
fclose(fid);
text(text==13)=[]; %remove windows CR for readablity
disp(text);
key=uint8(key);
text=uint8(text);
lenkey=length(key);
text(end+1:end+lenkey-mod(length(text),lenkey))=32; %add extra spaces for reshape.
fprintf('\n------------Cipher-------------\n');
cipher = reshape(bitxor(reshape(text,[],lenkey),key),1,[]);
disp(char(cipher));
fprintf('\n------------Decrpyt-------------\n');
decrpyt = reshape(bitxor(reshape(cipher,[],lenkey),key),1,[]);
disp(char(decrpyt));

merge two values into one variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Suppose I have two values like 100 and 80 now I wish to store these 2 values in the memory but with in one variable without creating a array or file handling and the thing is retrieving the same values afterwards at another place
It could be a strange approach, but this allows having a single varaible actually holding two varaibles.
You can create a complex varaible in which the real part is the first varaible and the imaginary part is the second variable.
a=100;
b=80
c=complex(a,b)
You can retrieve the original values using the real and imag functions
a=real(c)
b=imag(c)
Hope this helps.
Qapla'
a=80;
b=100;
c = [a,b]; % array (row)
c = [a;b]; % array (column)
c.a=a;c.b=b; % struct
c = {a,b}; % cell
Several options available.

Matlab Classification load dataset [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to load and use a dataset in order to run some algorithms(Neural Networks) in Matlab. I've downloaded a dataset from the internet which has instances and attributes.
I've saved that dataset as a plain text file, and also with the extension .data or .mat. But I am not able to import and use it in Matlab.
How should I do? I also have to define a training and a test set after.
Thank you in advance.
I have to mention I am new to Matlab and trying to study it as a hobby.
You can just load the data by:
data = load('wine.data');
Then, you can split the data to training and testing very easily.
Here, I put 70% data for training and 30% for testing, but you could choose other fraction. 60-40 or 80-20
data = data(randperm(end), :);
traindata = data(1:floor(0.7*size(data, 1)), :);
testdata = data(floor(0.7*size(data, 1))+1:end, :);
In the end, when you want to run the classifier, remember that in this dataset, the first column is the label and the rest are features.

Differentiate b/w fscanf and load function in matlab [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In my matlab program i am reading the data file using fscanf and writing the the code code to read all the values.That makes me write several steps.
How to use Load() function to overcome this ad make it simple.
So the way load works is to load variables from MATLAB binary/ascii files. In order to create said files you'll have to use the save function e.g.
octave:3> T = "Hello"
T = Hello
octave:4> save "-binary" "testfile" T
octave:5> clear
octave:6> T
error: 'T' undefined near line 1 column 1
octave:6> load "-binary" "testfile" T
octave:7> T
T = Hello
octave:8>
Sorry I used octave for the example but it's the same code either way. So if you know your going to be using the same data just save it in a MATLAB's binary format. It should save your self the time of having to use fscanf on it next time your playing around with the data.