-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.m
executable file
·122 lines (99 loc) · 2.84 KB
/
example.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
% Examples:
% example
%
% Takes:
% N/A
%
% Gives:
% N/A
%
% Notes:
% A few example of how to run the OpenSSL toolbox
%
% Author:
% Anthony Gabrielson
% 3/26/2010
%
% Modifications:
% 3/30/2010 Added example 1c & 1d
% Test 1a: Basic Encryption / Decryption
dataToEncrypt = uint8(1:255);
encryptedData = mexEVP_Encrypt('data',dataToEncrypt);
decrypted = mexEVP_Decrypt('data',encryptedData);
if( sum(dataToEncrypt' - decrypted) == 0 )
fprintf(1,'Test 1a Passed\n');
else
fprintf(1,'Test 1a Problem...\n');
end
clear
% Test 1b: Basic Encryption / Decryption - different key & algorithm
dataToEncrypt = uint8(1:255);
encryptedData = mexEVP_Encrypt('data',dataToEncrypt,'key','another key!','cipher','bf-cbc');
decrypted = mexEVP_Decrypt('data',encryptedData,'key','another key!','cipher','bf-cbc');
if( sum(dataToEncrypt' - decrypted) == 0 )
fprintf(1,'Test 1b Passed\n');
else
fprintf(1,'Test 1b Problem...\n');
end
clear
% Test 1c: Basic Encryption / Decryption - random key & algorithm
dataToEncrypt = uint8(1:255);
[key, iv] = mexRandom('key','iv');
encryptedData = mexEVP_Encrypt('data',dataToEncrypt,'key',key,'iv',iv,'cipher','bf-cbc');
decrypted = mexEVP_Decrypt('data',encryptedData,'key',key,'iv',iv,'cipher','bf-cbc');
if( sum(dataToEncrypt' - decrypted) == 0 )
fprintf(1,'Test 1c Passed\n');
else
fprintf(1,'Test 1c Problem...\n');
end
clear
% Test 1d: Basic Encryption / Decryption with strings
dataToEncrypt = ['0':'9','a':'z','A':'Z'];
encryptedData = mexEVP_Encrypt('data',uint8(dataToEncrypt));
decrypted = mexEVP_Decrypt('data',encryptedData);
dataDecrypted = char(decrypted');
if( strcmp(dataToEncrypt,dataDecrypted) == 1 )
fprintf(1,'Test 1d Passed\n');
else
fprintf(1,'Test 1d Problem...\n');
end
clear
%Test 2: Data Conversion
dataStart = 1:255;
toUint8 = [];
for J=1:length(dataStart)
temp = double2uint8(dataStart(J));
toUint8 = [toUint8; temp];
end
toDouble = [];
for J=1:8:length(toUint8)
temp = uint82double(toUint8(J:J+7));
toDouble = [toDouble; temp];
end
if( sum(dataStart' - toDouble) == 0 )
fprintf(1,'Test 2 Passed\n');
else
fprintf(1,'Test 2 Problem...\n');
end
clear
%Test 3: Data Conversion + Encryption
dataStart = 1:255;
toUint8 = [];
for J=1:length(dataStart)
temp = double2uint8(dataStart(J));
toUint8 = [toUint8; temp];
end
encryptedData = mexEVP_Encrypt('data',toUint8);
decrypted = mexEVP_Decrypt('data',encryptedData);
toDouble = [];
for J=1:8:length(decrypted)
temp = uint82double(decrypted(J:J+7));
toDouble = [toDouble; temp];
end
if( sum(dataStart' - toDouble) == 0 )
fprintf(1,'Test 3 Passed\n');
else
fprintf(1,'Test 3 Problem...\n');
end
clear