Running C Code in Matlab using MEX
3 views (last 30 days)
Show older comments
#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*)"TESTKEY", 7);
Blowfish_Init (&ctx, (unsigned char*)(input.c_str()), 7);*/
//Blowfish_Encrypt(&ctx, &L, &R);
/* must be less than 56 bytes */
//char *key = "a random number string would be a better key";
/*const char* key = "TESTKEY";*/
const char* key = inputkey.c_str();
int keylen = strlen(key);
/*char const* plaintext_string = "this is our message";*/
char const* plaintext_string = input.c_str();
// explicitly cast to 'unsigned char*'
//unsigned char* in = reinterpret_cast<unsigned char*>(key);
int plaintext_len = strlen(plaintext_string);
Blowfish_Init(&ctx, (unsigned char*)key, keylen);
/* encrypt the plaintext message string */
mexPrintf("Encrypted message string is: ");
while (plaintext_len)
{
message_left = message_right = 0UL;
/* crack the message string into a 64-bit block (ok, really two 32-bit blocks); pad with zeros if necessary */
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;
}
/* encrypt and print the results */
Blowfish_Encrypt(&ctx, &message_left, &message_right);
mexPrintf("%lx%lx", message_left, message_right);
//fflush(stdout);
/* save the results for decryption below */
*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>";
//mexPrintf("Please enter a valid Decryption key:\n");
getline(cin, inputDkey);
//scanf_s("%s",inputDkey);
cout << "Your key: " << inputDkey << endl << endl;
//mexPrintf("Your key:%s",inputDkey);
const char* Dkey = inputDkey.c_str();
int Dkeylen = strlen(Dkey);
Blowfish_Init(&ctx, (unsigned char*)Dkey, keylen);
/* reverse the process */
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);
/* if plaintext message string padded, extra zeros here */
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");
}
Does anyone know how to run this code in Matlab? I have trouble getting the output in Matlab command window.
0 Comments
Answers (1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!