-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakeCipherImage.m
62 lines (42 loc) · 1.61 KB
/
MakeCipherImage.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function [squareImage] = MakeCipherImage(text,key)
% This function takes text and converts it into a square black and white image
% using a encryption key
% Input: Text, encryption key
% Output: encrypted black and white square image
% Author: Albert Sun UPI: asun555
TwoByNmatrix = [];
cellArray = cell(1,length(text));
for i = 1:length(text)
% get text ascii values
ASCIIvalues{i} = GetHexASCII(text(i));
% split ascii value in two
ASCIIvalues{i} = regexp(ASCIIvalues{i},'\w','match');
% convert ascii values to 2 decimal values
value1 = hex2dec(ASCIIvalues{i}(1,1));
value2 = hex2dec(ASCIIvalues{i}(1,2));
% put values into key and bring out corresponding graphs
graph1 = key{value1+1};
graph2 = key{value2+1};
% put graphs into cell array
cellArray{i} = [graph1 graph2];
% bring out binary values from cell array and turn into 2x(4*n) matrix
uint8matrix(1:2,1:4) = cellArray{1,i}(1:2,1:4);
TwoByNmatrix = [TwoByNmatrix uint8matrix];
% convert 1s to 255s
TwoByNmatrix(TwoByNmatrix==1)=255;
end
% Cutting up matrix and arranging it into a square
squareImage = [];
totelElements = numel(TwoByNmatrix);
amountRowsAndColumns = sqrt(totelElements);
for i = 1:amountRowsAndColumns:(totelElements/2)
% cut out values from TwoByNmatrix
cutOut = TwoByNmatrix(1:2,i:i+(amountRowsAndColumns-1));
% Lay out values vertically into a square
squareImage = [squareImage;cutOut];
end
% if not a square then display error message
if size(squareImage,1) ~= size(squareImage,2)
squareImage = 'ERROR';
end
end