Last active
December 14, 2015 01:09
-
-
Save kyle-go/5003954 to your computer and use it in GitHub Desktop.
jgg encrypt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Jgg.h" | |
#define REAL_SHIFT(i) (i%8+1) | |
#define KEY_BYTE 0xE9 | |
JGG_KEY jggSetup(unsigned char *key, int len) | |
{ | |
JGG_KEY jk; | |
int i=0, j=0, k=0; | |
for( i = 0; i < 256; i++ ) | |
{ | |
jk.m[i] = i; | |
} | |
for( i = 0; i < 256; i++ ) | |
{ | |
j = (unsigned char) ( j + jk.m[i] + key[k] + j%7 + i%11); | |
if( i != j) | |
{ | |
char tmp = jk.m[i]; | |
jk.m[i] = jk.m[j]; | |
jk.m[j] = tmp; | |
} | |
if( ++k >= len ) k = 0; | |
} | |
return jk; | |
} | |
void encrypt(JGG_KEY &jk, const unsigned char *in, unsigned char *out, int len) | |
{ | |
int i=0; | |
int x=0; | |
int y=0; | |
for (i=0; i<len; i++) | |
{ | |
unsigned char a = in[i] ^ KEY_BYTE ^ (unsigned char)i; | |
unsigned char b = REAL_SHIFT(i); | |
__asm | |
{ | |
s1: | |
rol a, 1 | |
dec b | |
cmp b,0 | |
jg s1 | |
} | |
x = (unsigned char)(x + 1); | |
y = (unsigned char)(y + jk.m[x]); | |
if (x != y) | |
{ | |
char tmp = jk.m[x]; | |
jk.m[x] = jk.m[y]; | |
jk.m[y] = tmp; | |
} | |
out[i] = a^(unsigned char)jk.m[(unsigned char)(jk.m[x]+jk.m[y])]; | |
} | |
} | |
void decrypt(JGG_KEY &jk, const unsigned char *in, unsigned char *out, int len) | |
{ | |
int i=0; | |
int x=0; | |
int y=0; | |
for (i=0; i<len; i++) | |
{ | |
unsigned char a = 0; | |
unsigned char b = 0; | |
x = (unsigned char)(x + 1); | |
y = (unsigned char)(y + jk.m[x]); | |
if (x != y) | |
{ | |
char tmp = jk.m[x]; | |
jk.m[x] = jk.m[y]; | |
jk.m[y] = tmp; | |
} | |
a = in[i]^(unsigned char)jk.m[(unsigned char)(jk.m[x]+jk.m[y])]; | |
b = REAL_SHIFT(i); | |
__asm | |
{ | |
s1: | |
ror a, 1 | |
dec b | |
cmp b,0 | |
jg s1 | |
} | |
out[i] = a ^ KEY_BYTE ^ (unsigned char)i; | |
} | |
} | |
void jggEncrypt(JGG_KEY &jk, const unsigned char *in, unsigned char *out, int len, bool enc) | |
{ | |
enc? encrypt(jk, in, out, len):decrypt(jk, in, out, len); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _JGG_H | |
#define _JGG_H | |
struct JGG_KEY | |
{ | |
char m[256]; | |
}; | |
JGG_KEY jggSetup(unsigned char *key, int len); | |
void jggEncrypt(JGG_KEY &jk, const unsigned char *in, unsigned char *out, int len, bool enc); | |
#endif //_JGG_H | |
/*! | |
example: | |
#include "jgg.h" | |
#include <string> | |
int main() | |
{ | |
unsigned char buf[256] = {0}; | |
unsigned char buf2[256] = {0}; | |
std::string src = "哈哈 ni mei 啊。。--¥%&*!!!"; | |
printf("src:%s\n", src.c_str()); | |
std::string key = "i am a key..."; | |
jggEncrypt(jggSetup((unsigned char*)key.c_str(), key.length()), (unsigned char*)src.c_str(), buf, src.length(), true); | |
jggEncrypt(jggSetup((unsigned char*)key.c_str(), key.length()), buf, buf2, src.length(), false); | |
printf("dst:%s\n", buf2); | |
return 0; | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment