Skip to content

Commit

Permalink
refactor the encode function to make it safer
Browse files Browse the repository at this point in the history
  • Loading branch information
lihancong committed Mar 18, 2017
1 parent 8fece1a commit 65f96af
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions core.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,15 @@ int tonyenc_ext_fopen(FILE *fp, struct stat *stat_buf, TONYENC_RES *res)

void tonyenc_encode(char *data, size_t len)
{
tonyenc_decode(data, len);
size_t i, p = 0;
for (i = 0; i < len; ++i) {
if (i & 1) {
p += tonyenc_key[p] + i;
p %= sizeof(tonyenc_key);
u_char t = tonyenc_key[p];
data[i] = ~(data[i] ^ t);
}
}
}


Expand All @@ -156,7 +164,7 @@ void tonyenc_decode(char *data, size_t len)
p += tonyenc_key[p] + i;
p %= sizeof(tonyenc_key);
u_char t = tonyenc_key[p];
data[i] ^= t;
data[i] = ~data[i] ^ t;
}
}
}
2 changes: 1 addition & 1 deletion tonyenc.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
if (!extension_loaded('tonyenc'))
die("The extension: 'tonyenc' not loaded\n");
if ($argc <= 1)
die("usage: php tonyenc.php file.php ... encrypt the php file(s) or directory(s)\n");
die("\nusage: php tonyenc.php file.php ... encrypt the php file(s) or directory(s)\n\n");



Expand Down

0 comments on commit 65f96af

Please sign in to comment.