Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

blowfish_test.mexw64 : fatal error LNK1120: 3 unresolved externals

Status
Not open for further replies.

klun

Junior Member level 2
Joined
May 1, 2015
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
386

Code C - [expand]
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");
}


Can anyone help me to look on this C code? I have included the mex.h in my code which I want this code to run in Matlab using MEX file. However I have not getting any output and keep getting the error shown below.

Error : blowfish_test.obj : error LNK2019: unresolved external symbol "void __cdecl Blowfish_Decrypt(struct BLOWFISH_CTX
*,unsigned long *,unsigned long *)" (?Blowfish_Decrypt@@YAXPEAUBLOWFISH_CTX@@PEAK1@Z) referenced in function mexFunction
 
Last edited by a moderator:

you are making a call
Code:
Blowfish_Decrypt(&ctx, &message_left, &message_right);
but the linker cannot find a version Blowfish_Decrypt() with the paramters pointer to a BLOWFISH_CTX structure and two pointers to unsigned long
Code:
void __cdecl Blowfish_Decrypt(struct BLOWFISH_CTX*,unsigned long *,unsigned long *)

1. check you are calling it with the correct paramters
2. are you linking in the blowfish libraries?
the message : fatal error LNK1120: 3 unresolved externals indicates you have two other unresolved externals - are they Blowfish_Init() and Blowfish_Encrypt()?
 

Yes sir you are absolutely right. Hence in this case, do I need to alter the pointer so that it can be called in Matlab? And by using MEX function, I thought it supposed to called C code in Matlab directly without need to change anything? Am I right?
 

it looks like you are not linking the blowfish library correctly
is it a Matlab, C or C++ library?
if C++ are you linking blowfish.cpp at compile time, e.g.
Code:
mex blowfish_test.cpp blowfish.cpp
can you post blowfish.h?
 
Last edited:

Blowfish.h

typedef struct {
unsigned long P[16 + 2];
unsigned long S[4][256];
} BLOWFISH_CTX;

void Blowfish_Init(BLOWFISH_CTX *ctx, unsigned char *key, int keyLen);
void Blowfish_Encrypt(BLOWFISH_CTX *ctx, unsigned long *xl, unsigned long *xr);
void Blowfish_Decrypt(BLOWFISH_CTX *ctx, unsigned long *xl, unsigned long *xr);



Yea. I did called my C code from Matlab using mex -v blowfish_test.cpp blowfish.cpp. In this instant, I need to call blowfish.h too? It will show an error saying .h is an unrecognised file sir.
 

check that the function names and parameter number and types are the same in blowfish.cpp and blowfish.h
does blowfish.cpp include blowfish.h?
you don't need to include blowfish.h in the mex statement
 


Code C - [expand]
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
#include "stdafx.h"
#include "blowfish.h"
 
#define N               16/* Exchange Xl and Xr */
        temp = Xl;
        Xl = Xr;
        Xr = temp;
    }
 
    /* Exchange Xl and Xr */
    temp = Xl;
    Xl = Xr;
    Xr = temp;
 
    Xr = Xr ^ ctx->P[1];
    Xl = Xl ^ ctx->P[0];
 
    *xl = Xl;
    *xr = Xr;
}
 
 
void Blowfish_Init(BLOWFISH_CTX *ctx, unsigned char *key, int keyLen) {
    int i, j, k;
    unsigned long data, datal, datar;
 
    for (i = 0; i < 4; i++) {
        for (j = 0; j < 256; j++)
            ctx->S[i][j] = ORIG_S[i][j];
    }
 
    j = 0;
    for (i = 0; i < N + 2; ++i) {
        data = 0x00000000;
        for (k = 0; k < 4; ++k) {
            data = (data << 8) | key[j];
            j = j + 1;
            if (j >= keyLen)
                j = 0;
        }
        ctx->P[i] = ORIG_P[i] ^ data;
    }
 
    datal = 0x00000000;
    datar = 0x00000000;
 
    for (i = 0; i < N + 2; i += 2) {
        Blowfish_Encrypt(ctx, &datal, &datar);
        ctx->P[i] = datal;
        ctx->P[i + 1] = datar;
    }
 
    for (i = 0; i < 4; ++i) {
        for (j = 0; j < 256; j += 2) {
            Blowfish_Encrypt(ctx, &datal, &datar);
            ctx->S[i][j] = datal;
            ctx->S[i][j + 1] = datar;
        }
    }
}
 
 
 
int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}


This is my blowfish_koc.cpp file sir. I dont see any difference in the function names and types. And yea, I did included blowfish.h in blowfish_koc.cpp
 
Last edited by a moderator:

C code to Matlab code (new to Matlab)


Code C - [expand]
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
138
#include "blowfish.h"
#include "mex.h"
#include "math.h"
#include "string.h"
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <cstdlib>
 
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  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 = "";
  
  mxArray *new_number, *str;
  double out;
 
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;*/
  
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");
 
mexPrintf("Please enter a valid Decryption key:\n");
 
scanf_s("%s",inputDkey);
 
mexPrintf("Your key:%s",inputDkey);
 
const char* Dkey = inputDkey.c_str();
 
int Dkeylen = strlen(Dkey);
Blowfish_Init(&ctx, (unsigned char*)Dkey, keylen);
 
     printf("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);
 
        printf("%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\n");
   }



Can anyone here willing to spend some time in helping me to convert this C code to Matlab code? I have trouble with the syntax of Matlab as I am new to Matlab. Thanks for your help. Much appreciated!
 
Last edited by a moderator:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top