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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
| #include "mex.h"
#include "stdafx.h"
#include <stdio.h>
#include "blowfish.h"
#include <iostream>
#include <sstream>
#include <cstdlib>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
//void main(void)
{
unsigned long L = 1, R = 2;
BLOWFISH_CTX ctx;
unsigned long message_left;
unsigned long message_right;
int block_len;
unsigned char ciphertext_buffer[256];
unsigned char *ciphertext_string = &ciphertext_buffer[0];
int ciphertext_len = 0;
using namespace std;
string input = "";
string inputkey = "";
string inputDkey = "";
cout << "Please enter a valid message:\n>";
getline(cin, input);
cout << "Your message: " << input << endl << endl;
cout << "Please enter a valid key:\n>";
getline(cin, inputkey);
cout << "Your key: " << inputkey << endl << endl;
Blowfish_Init (&ctx, (unsigned char*)(input.c_str()), 7);*/
const char* key = inputkey.c_str();
int keylen = strlen(key);
char const* plaintext_string = input.c_str();
int plaintext_len = strlen(plaintext_string);
Blowfish_Init(&ctx, (unsigned char*)key, keylen);
mexPrintf("Encrypted message string is: ");
while (plaintext_len)
{
message_left = message_right = 0UL;
for (block_len = 0; block_len < 4; block_len++)
{
message_left = message_left << 8;
if (plaintext_len)
{
message_left += *plaintext_string++;
plaintext_len--;
}
else message_left += 0;
}
for (block_len = 0; block_len < 4; block_len++)
{
message_right = message_right << 8;
if (plaintext_len)
{
message_right += *plaintext_string++;
plaintext_len--;
}
else message_right += 0;
}
Blowfish_Encrypt(&ctx, &message_left, &message_right);
mexPrintf("%lx%lx", message_left, message_right);
*ciphertext_string++ = (unsigned char)(message_left >> 24);
*ciphertext_string++ = (unsigned char)(message_left >> 16);
*ciphertext_string++ = (unsigned char)(message_left >> 8);
*ciphertext_string++ = (unsigned char)message_left;
*ciphertext_string++ = (unsigned char)(message_right >> 24);
*ciphertext_string++ = (unsigned char)(message_right >> 16);
*ciphertext_string++ = (unsigned char)(message_right >> 8);
*ciphertext_string++ = (unsigned char)message_right;
ciphertext_len += 8;
}
mexPrintf("\n\n");
cout << "Please enter a valid Decryption key:\n>";
getline(cin, inputDkey);
cout << "Your key: " << inputDkey << endl << endl;
const char* Dkey = inputDkey.c_str();
int Dkeylen = strlen(Dkey);
Blowfish_Init(&ctx, (unsigned char*)Dkey, keylen);
mexPrintf("Decrypted message string is: ");
ciphertext_string = &ciphertext_buffer[0];
while (ciphertext_len)
{
message_left = message_right = 0UL;
for (block_len = 0; block_len < 4; block_len++)
{
message_left = message_left << 8;
message_left += *ciphertext_string++;
if (ciphertext_len)
ciphertext_len--;
}
for (block_len = 0; block_len < 4; block_len++)
{
message_right = message_right << 8;
message_right += *ciphertext_string++;
if (ciphertext_len)
ciphertext_len--;
}
Blowfish_Decrypt(&ctx, &message_left, &message_right);
mexPrintf("%c%c%c%c%c%c%c%c",
(int)(message_left >> 24), (int)(message_left >> 16),
(int)(message_left >> 8), (int)(message_left),
(int)(message_right >> 24), (int)(message_right >> 16),
(int)(message_right >> 8), (int)(message_right));
}
mexPrintf("\n");
} |