ldes56.c
3.22 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdlib.h>
#include <string.h>
#include "des56.h"
#include "lua.h"
#include "lauxlib.h"
#include "ldes56.h"
static int des56_decrypt( lua_State *L )
{
char* decypheredText;
keysched KS;
int rel_index, abs_index;
size_t cypherlen;
const char *cypheredText =
luaL_checklstring( L, 1, &cypherlen );
const char *key = luaL_optstring( L, 2, NULL );
int padinfo;
padinfo = cypheredText[cypherlen-1];
cypherlen--;
/* Aloca array */
decypheredText =
(char *) malloc( (cypherlen+1) * sizeof(char));
if(decypheredText == NULL) {
lua_pushstring(L, "Error decrypting file. Not enough memory.");
lua_error(L);
}
/* Inicia decifragem */
if (key && strlen(key) >= 8)
{
char k[8];
int i;
for (i=0; i<8; i++)
k[i] = (unsigned char)key[i];
fsetkey(k, &KS);
} else {
lua_pushstring(L, "Error decrypting file. Invalid key.");
lua_error(L);
}
rel_index = 0;
abs_index = 0;
while (abs_index < (int) cypherlen)
{
decypheredText[abs_index] = cypheredText[abs_index];
abs_index++;
rel_index++;
if( rel_index == 8 )
{
rel_index = 0;
fencrypt(&(decypheredText[abs_index - 8]), 1, &KS);
}
}
decypheredText[abs_index] = 0;
lua_pushlstring(L, decypheredText, (abs_index-padinfo));
free( decypheredText );
return 1;
}
static int des56_crypt( lua_State *L )
{
char *cypheredText;
keysched KS;
int rel_index, pad, abs_index;
size_t plainlen;
const char *plainText = luaL_checklstring( L, 1, &plainlen );
const char *key = luaL_optstring( L, 2, NULL );
cypheredText = (char *) malloc( (plainlen+8) * sizeof(char));
if(cypheredText == NULL) {
lua_pushstring(L, "Error encrypting file. Not enough memory.");
lua_error(L);
}
if (key && strlen(key) >= 8)
{
char k[8];
int i;
for (i=0; i<8; i++)
k[i] = (unsigned char)key[i];
fsetkey(k, &KS);
} else {
lua_pushstring(L, "Error encrypting file. Invalid key.");
lua_error(L);
}
rel_index = 0;
abs_index = 0;
while (abs_index < (int) plainlen) {
cypheredText[abs_index] = plainText[abs_index];
abs_index++;
rel_index++;
if( rel_index == 8 ) {
rel_index = 0;
fencrypt(&(cypheredText[abs_index - 8]), 0, &KS);
}
}
pad = 0;
if(rel_index != 0) { /* Pads remaining bytes with zeroes */
while(rel_index < 8)
{
pad++;
cypheredText[abs_index++] = 0;
rel_index++;
}
fencrypt(&(cypheredText[abs_index - 8]), 0, &KS);
}
cypheredText[abs_index] = pad;
lua_pushlstring( L, cypheredText, abs_index+1 );
free( cypheredText );
return 1;
}
/*
** Assumes the table is on top of the stack.
*/
static void set_info (lua_State *L) {
lua_pushliteral (L, "_COPYRIGHT");
lua_pushliteral (L, "Copyright (C) 2007 PUC-Rio");
lua_settable (L, -3);
lua_pushliteral (L, "_DESCRIPTION");
lua_pushliteral (L, "DES 56 cryptographic facilities for Lua");
lua_settable (L, -3);
lua_pushliteral (L, "_VERSION");
lua_pushliteral (L, "DES56 1.1.2");
lua_settable (L, -3);
}
static const struct luaL_Reg des56lib[] = {
{"crypt", des56_crypt},
{"decrypt", des56_decrypt},
{NULL, NULL},
};
int luaopen_des56 (lua_State *L) {
luaL_openlib (L, "des56", des56lib, 0);
set_info (L);
return 1;
}