Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
X
xxtea-c
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
xxtea-c
Commits
6ba543bb
Commit
6ba543bb
authored
Mar 03, 2015
by
andot
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed key and added base64 in example.
parent
72ce9f7e
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
229 additions
and
53 deletions
+229
-53
.gitignore
.gitignore
+3
-0
README.md
README.md
+1
-1
README_zh_CN.md
README_zh_CN.md
+1
-1
base64.c
example/base64.c
+126
-0
base64.h
example/base64.h
+46
-0
test.c
example/test.c
+5
-1
xxtea.c
xxtea.c
+44
-48
xxtea.h
xxtea.h
+3
-2
No files found.
.gitignore
View file @
6ba543bb
CMakeFiles/*
example/CMakeFiles/*
*.cmake
...
...
@@ -12,3 +13,5 @@ CMakeCache.txt
*.a
Makefile
example/main
README.md
View file @
6ba543bb
...
...
@@ -26,7 +26,7 @@ int main() {
const
char
*
text
=
"Hello World! 你好,中国!"
;
const
char
*
key
=
"1234567890"
;
size_t
len
;
char
*
encrypt_data
=
xxtea_encrypt
(
text
,
strlen
(
text
),
key
,
&
len
);
unsigned
char
*
encrypt_data
=
xxtea_encrypt
(
text
,
strlen
(
text
),
key
,
&
len
);
char
*
decrypt_data
=
xxtea_decrypt
(
encrypt_data
,
len
,
key
,
&
len
);
if
(
strncmp
(
text
,
decrypt_data
,
len
)
==
0
)
{
printf
(
"success!
\n
"
);
...
...
README_zh_CN.md
View file @
6ba543bb
...
...
@@ -26,7 +26,7 @@ int main() {
const
char
*
text
=
"Hello World! 你好,中国!"
;
const
char
*
key
=
"1234567890"
;
size_t
len
;
char
*
encrypt_data
=
xxtea_encrypt
(
text
,
strlen
(
text
),
key
,
&
len
);
unsigned
char
*
encrypt_data
=
xxtea_encrypt
(
text
,
strlen
(
text
),
key
,
&
len
);
char
*
decrypt_data
=
xxtea_decrypt
(
encrypt_data
,
len
,
key
,
&
len
);
if
(
strncmp
(
text
,
decrypt_data
,
len
)
==
0
)
{
printf
(
"success!
\n
"
);
...
...
example/base64.c
0 → 100644
View file @
6ba543bb
/**********************************************************\
| |
| base64.c |
| |
| Base64 library for C. |
| |
| Code Authors: Chen fei <cf850118@163.com> |
| Ma Bingyao <mabingyao@gmail.com> |
| LastModified: Mar 3, 2015 |
| |
\**********************************************************/
#include <string.h>
#include "base64.h"
static
const
char
Base64EncodeChars
[]
=
{
'A'
,
'B'
,
'C'
,
'D'
,
'E'
,
'F'
,
'G'
,
'H'
,
'I'
,
'J'
,
'K'
,
'L'
,
'M'
,
'N'
,
'O'
,
'P'
,
'Q'
,
'R'
,
'S'
,
'T'
,
'U'
,
'V'
,
'W'
,
'X'
,
'Y'
,
'Z'
,
'a'
,
'b'
,
'c'
,
'd'
,
'e'
,
'f'
,
'g'
,
'h'
,
'i'
,
'j'
,
'k'
,
'l'
,
'm'
,
'n'
,
'o'
,
'p'
,
'q'
,
'r'
,
's'
,
't'
,
'u'
,
'v'
,
'w'
,
'x'
,
'y'
,
'z'
,
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
'8'
,
'9'
,
'+'
,
'/'
};
static
const
char
Base64DecodeChars
[]
=
{
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
62
,
-
1
,
-
1
,
-
1
,
63
,
52
,
53
,
54
,
55
,
56
,
57
,
58
,
59
,
60
,
61
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
0
,
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
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
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
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
};
char
*
base64_encode
(
const
unsigned
char
*
data
,
size_t
len
)
{
char
*
out
,
*
pos
;
const
unsigned
char
*
in
=
data
;
size_t
i
,
quot
,
rem
;
int
c
;
if
(
!
len
)
return
NULL
;
quot
=
len
/
3
;
rem
=
len
%
3
;
out
=
(
char
*
)
malloc
((
quot
+
(
rem
?
1
:
0
))
*
4
+
1
);
if
(
!
out
)
return
NULL
;
pos
=
out
;
for
(
i
=
0
;
i
<
quot
;
i
++
)
{
c
=
(
0x000000ff
&
*
in
++
)
<<
16
;
c
|=
(
0x000000ff
&
*
in
++
)
<<
8
;
c
|=
0x000000ff
&
*
in
++
;
*
pos
++
=
Base64EncodeChars
[
c
>>
18
];
*
pos
++
=
Base64EncodeChars
[
c
>>
12
&
0x3f
];
*
pos
++
=
Base64EncodeChars
[
c
>>
6
&
0x3f
];
*
pos
++
=
Base64EncodeChars
[
c
&
0x3f
];
}
if
(
rem
==
1
)
{
c
=
0x000000ff
&
*
in
++
;
*
pos
++
=
Base64EncodeChars
[
c
>>
2
];
*
pos
++
=
Base64EncodeChars
[(
c
&
0x03
)
<<
4
];
*
pos
++
=
'='
;
*
pos
++
=
'='
;
}
else
if
(
rem
==
2
)
{
c
=
(
0x000000ff
&
*
in
++
)
<<
8
;
c
|=
0x000000ff
&
*
in
++
;
*
pos
++
=
Base64EncodeChars
[
c
>>
10
];
*
pos
++
=
Base64EncodeChars
[
c
>>
4
&
0x3f
];
*
pos
++
=
Base64EncodeChars
[(
c
&
0x0f
)
<<
2
];
*
pos
++
=
'='
;
}
*
pos
=
'\0'
;
return
out
;
}
unsigned
char
*
base64_decode
(
const
char
*
data
,
size_t
*
out_len
)
{
unsigned
char
*
out
,
*
pos
;
const
unsigned
char
*
in
=
(
const
unsigned
char
*
)
data
;
size_t
i
,
len
,
quot
,
rem
,
paddings
=
0
;
int
c
;
len
=
strlen
(
data
);
if
(
!
len
)
return
NULL
;
rem
=
len
%
4
;
if
(
rem
)
return
NULL
;
// invalid size
quot
=
len
/
4
;
if
(
data
[
len
-
2
]
==
'='
)
paddings
=
2
;
else
if
(
data
[
len
-
1
]
==
'='
)
paddings
=
1
;
out
=
(
unsigned
char
*
)
malloc
(
quot
*
3
-
paddings
+
1
);
if
(
!
out
)
return
NULL
;
pos
=
out
;
for
(
i
=
0
;
i
<
quot
;
i
++
)
{
c
=
Base64DecodeChars
[(
int
)
*
in
++
]
<<
18
;
c
+=
Base64DecodeChars
[(
int
)
*
in
++
]
<<
12
;
*
pos
++
=
(
c
&
0x00ff0000
)
>>
16
;
if
(
*
in
!=
'='
)
{
c
+=
Base64DecodeChars
[(
int
)
*
in
++
]
<<
6
;
*
pos
++
=
(
c
&
0x0000ff00
)
>>
8
;
if
(
*
in
!=
'='
)
{
c
+=
Base64DecodeChars
[(
int
)
*
in
++
];
*
pos
++
=
c
&
0x000000ff
;
}
}
}
*
pos
=
'\0'
;
*
out_len
=
pos
-
out
;
return
out
;
}
example/base64.h
0 → 100644
View file @
6ba543bb
/**********************************************************\
| |
| base64.h |
| |
| Base64 library for C. |
| |
| Code Authors: Chen fei <cf850118@163.com> |
| Ma Bingyao <mabingyao@gmail.com> |
| LastModified: Mar 3, 2015 |
| |
\**********************************************************/
#ifndef BASE64_INCLUDED
#define BASE64_INCLUDED
#include <stdlib.h>
#ifdef __cplusplus
extern
"C"
{
#endif
/**
* Function: base64_encode
* @data: Data to be encoded
* @len: Length of the data to be encoded
* Returns: Encoded data or %NULL on failure
*
* Caller is responsible for freeing the returned buffer.
*/
char
*
base64_encode
(
const
unsigned
char
*
data
,
size_t
len
);
/**
* Function: base64_decode
* @data: Data to be decoded
* @out_len: Pointer to output length variable
* Returns: Decoded data or %NULL on failure
*
* Caller is responsible for freeing the returned buffer.
*/
unsigned
char
*
base64_decode
(
const
char
*
data
,
size_t
*
out_len
);
#ifdef __cplusplus
}
#endif
#endif
example/test.c
View file @
6ba543bb
#include <stdio.h>
#include <string.h>
#include <xxtea.h>
#include "base64.h"
int
main
()
{
const
char
*
text
=
"Hello World! 你好,中国!"
;
const
char
*
key
=
"1234567890"
;
size_t
len
;
char
*
encrypt_data
=
xxtea_encrypt
(
text
,
strlen
(
text
),
key
,
&
len
);
unsigned
char
*
encrypt_data
=
xxtea_encrypt
(
text
,
strlen
(
text
),
key
,
&
len
);
char
*
base64_data
=
base64_encode
(
encrypt_data
,
len
);
printf
(
"%s
\n
"
,
base64_data
);
char
*
decrypt_data
=
xxtea_decrypt
(
encrypt_data
,
len
,
key
,
&
len
);
if
(
strncmp
(
text
,
decrypt_data
,
len
)
==
0
)
{
printf
(
"success!
\n
"
);
...
...
@@ -16,5 +19,6 @@ int main() {
}
free
(
encrypt_data
);
free
(
decrypt_data
);
free
(
base64_data
);
return
0
;
}
xxtea.c
View file @
6ba543bb
...
...
@@ -2,13 +2,14 @@
| |
| xxtea.c |
| |
| XXTEA encryption algorithm library for
Golang.
|
| XXTEA encryption algorithm library for
C.
|
| |
| Encryption Algorithm Authors: |
| David J. Wheeler |
| Roger M. Needham |
| |
| Code Author: Ma Bingyao <mabingyao@gmail.com> |
| Code Authors: Chen fei <cf850118@163.com> |
| Ma Bingyao <mabingyao@gmail.com> |
| LastModified: Mar 3, 2015 |
| |
\**********************************************************/
...
...
@@ -16,7 +17,7 @@
#include "xxtea.h"
#include <st
ddef
.h>
#include <st
ring
.h>
#if defined(_MSC_VER) && _MSC_VER < 1600
typedef
unsigned
__int8
uint8_t
;
typedef
unsigned
__int32
uint32_t
;
...
...
@@ -27,8 +28,16 @@ typedef unsigned __int32 uint32_t;
#define MX (((z >> 5) ^ (y << 2)) + ((y >> 3) ^ (z << 4))) ^ ((sum ^ y) + (key[(p & 3) ^ e] ^ z))
#define DELTA 0x9e3779b9
#define FIXED_KEY \
size_t
i
,
j
;
\
uint8_t
fixed_key
[
16
];
\
memcpy
(
fixed_key
,
key
,
16
);
\
for
(
i
=
0
;
(
i
<
16
)
&&
(
fixed_key
[
i
]
!=
0
);
++
i
);
\
for
(
j
=
i
+
1
;
j
<
16
;
++
j
)
fixed_key
[
j
]
=
0
;
\
/**
*
Method:
xxtea_to_uint_array
*
Function:
xxtea_to_uint_array
* @data: Data to be converted
* @len: Length of the data to be converted
* @inc_len: Including the length of the information?
...
...
@@ -37,29 +46,25 @@ typedef unsigned __int32 uint32_t;
*
* Caller is responsible for freeing the returned buffer.
*/
uint32_t
*
xxtea_to_uint_array
(
const
uint8_t
*
data
,
size_t
len
,
int
inc_len
,
size_t
*
out_len
)
{
uint32_t
*
xxtea_to_uint_array
(
const
uint8_t
*
data
,
size_t
len
,
int
inc_len
,
size_t
*
out_len
)
{
uint32_t
*
out
;
size_t
i
,
n
;
n
=
(((
len
&
3
)
==
0
)
?
(
len
>>
2
)
:
((
len
>>
2
)
+
1
));
if
(
inc_len
)
{
if
(
inc_len
)
{
out
=
(
uint32_t
*
)
calloc
(
n
+
1
,
sizeof
(
uint32_t
));
if
(
!
out
)
return
NULL
;
out
[
n
]
=
(
uint32_t
)
len
;
*
out_len
=
n
+
1
;
}
else
{
else
{
out
=
(
uint32_t
*
)
calloc
(
n
,
sizeof
(
uint32_t
));
if
(
!
out
)
return
NULL
;
*
out_len
=
n
;
}
for
(
i
=
0
;
i
<
len
;
i
++
)
{
for
(
i
=
0
;
i
<
len
;
++
i
)
{
out
[
i
>>
2
]
|=
(
uint32_t
)
data
[
i
]
<<
((
i
&
3
)
<<
3
);
}
...
...
@@ -67,7 +72,7 @@ uint32_t * xxtea_to_uint_array(const uint8_t * data, size_t len, int inc_len, si
}
/**
*
Method:
xxtea_to_ubyte_array
*
Function:
xxtea_to_ubyte_array
* @data: Data to be converted
* @len: Length of the data to be converted
* @inc_len: Included the length of the information?
...
...
@@ -76,15 +81,13 @@ uint32_t * xxtea_to_uint_array(const uint8_t * data, size_t len, int inc_len, si
*
* Caller is responsible for freeing the returned buffer.
*/
uint8_t
*
xxtea_to_ubyte_array
(
const
uint32_t
*
data
,
size_t
len
,
int
inc_len
,
size_t
*
out_len
)
{
uint8_t
*
xxtea_to_ubyte_array
(
const
uint32_t
*
data
,
size_t
len
,
int
inc_len
,
size_t
*
out_len
)
{
uint8_t
*
out
;
size_t
i
,
m
,
n
;
n
=
len
<<
2
;
if
(
inc_len
)
{
if
(
inc_len
)
{
m
=
data
[
len
-
1
];
if
(
m
>
n
)
return
NULL
;
n
=
m
;
...
...
@@ -92,8 +95,7 @@ uint8_t * xxtea_to_ubyte_array(const uint32_t * data, size_t len, int inc_len, s
out
=
(
uint8_t
*
)
malloc
(
n
+
1
);
for
(
i
=
0
;
i
<
n
;
i
++
)
{
for
(
i
=
0
;
i
<
n
;
++
i
)
{
out
[
i
]
=
(
uint8_t
)(
data
[
i
>>
2
]
>>
((
i
&
3
)
<<
3
));
}
...
...
@@ -104,26 +106,23 @@ uint8_t * xxtea_to_ubyte_array(const uint32_t * data, size_t len, int inc_len, s
}
/**
*
Method:
xxtea_uint_encrypt
*
Function:
xxtea_uint_encrypt
* @data: Data to be encrypted
* @len: Length of the data to be encrypted
* @key: Symmetric key
* Returns: Encrypted data
*/
uint32_t
*
xxtea_uint_encrypt
(
uint32_t
*
data
,
size_t
len
,
uint32_t
*
key
)
{
uint32_t
*
xxtea_uint_encrypt
(
uint32_t
*
data
,
size_t
len
,
uint32_t
*
key
)
{
uint32_t
n
=
(
uint32_t
)
len
-
1
;
uint32_t
z
=
data
[
n
],
y
=
data
[
0
],
p
,
q
=
6
+
52
/
(
n
+
1
),
sum
=
0
,
e
;
if
(
n
<
1
)
return
data
;
while
(
0
<
q
--
)
{
while
(
0
<
q
--
)
{
sum
+=
DELTA
;
e
=
sum
>>
2
&
3
;
for
(
p
=
0
;
p
<
n
;
p
++
)
{
for
(
p
=
0
;
p
<
n
;
p
++
)
{
y
=
data
[
p
+
1
];
z
=
data
[
p
]
+=
MX
;
}
...
...
@@ -136,25 +135,22 @@ uint32_t * xxtea_uint_encrypt(uint32_t * data, size_t len, uint32_t * key)
}
/**
*
Method:
xxtea_uint_decrypt
*
Function:
xxtea_uint_decrypt
* @data: Data to be decrypted
* @len: Length of the data to be decrypted
* @key: Symmetric key
* Returns: Decrypted data
*/
uint32_t
*
xxtea_uint_decrypt
(
uint32_t
*
data
,
size_t
len
,
uint32_t
*
key
)
{
uint32_t
*
xxtea_uint_decrypt
(
uint32_t
*
data
,
size_t
len
,
uint32_t
*
key
)
{
uint32_t
n
=
(
uint32_t
)
len
-
1
;
uint32_t
z
=
data
[
n
],
y
=
data
[
0
],
p
,
q
=
6
+
52
/
(
n
+
1
),
sum
=
q
*
DELTA
,
e
;
if
(
n
<
1
)
return
data
;
while
(
sum
!=
0
)
{
while
(
sum
!=
0
)
{
e
=
sum
>>
2
&
3
;
for
(
p
=
n
;
p
>
0
;
p
--
)
{
for
(
p
=
n
;
p
>
0
;
p
--
)
{
z
=
data
[
p
-
1
];
y
=
data
[
p
]
-=
MX
;
}
...
...
@@ -168,7 +164,7 @@ uint32_t * xxtea_uint_decrypt(uint32_t * data, size_t len, uint32_t * key)
}
/**
*
Method: xxtea_encrypt
*
Function: xxtea_encrypt_ubyte
* @data: Data to be encrypted
* @len: Length of the data to be encrypted
* @key: Symmetric key
...
...
@@ -177,8 +173,7 @@ uint32_t * xxtea_uint_decrypt(uint32_t * data, size_t len, uint32_t * key)
*
* Caller is responsible for freeing the returned buffer.
*/
uint8_t
*
xxtea_encrypt_ubyte
(
const
uint8_t
*
data
,
size_t
len
,
const
uint8_t
*
key
,
size_t
*
out_len
)
{
uint8_t
*
xxtea_encrypt_ubyte
(
const
uint8_t
*
data
,
size_t
len
,
const
uint8_t
*
key
,
size_t
*
out_len
)
{
uint8_t
*
out
;
uint32_t
*
data_array
,
*
key_array
;
size_t
data_len
,
key_len
;
...
...
@@ -189,8 +184,7 @@ uint8_t * xxtea_encrypt_ubyte(const uint8_t * data, size_t len, const uint8_t *
if
(
!
data_array
)
return
NULL
;
key_array
=
xxtea_to_uint_array
(
key
,
16
,
0
,
&
key_len
);
if
(
!
key_array
)
{
if
(
!
key_array
)
{
free
(
data_array
);
return
NULL
;
}
...
...
@@ -203,12 +197,8 @@ uint8_t * xxtea_encrypt_ubyte(const uint8_t * data, size_t len, const uint8_t *
return
out
;
}
void
*
xxtea_encrypt
(
const
void
*
data
,
size_t
len
,
const
void
*
key
,
size_t
*
out_len
)
{
return
xxtea_encrypt_ubyte
(
data
,
len
,
key
,
out_len
);
}
/**
*
Method: xxtea_decrypt
*
Function: xxtea_decrypt_ubyte
* @data: Data to be decrypted
* @len: Length of the data to be decrypted
* @key: Symmetric key
...
...
@@ -217,8 +207,7 @@ void * xxtea_encrypt(const void * data, size_t len, const void * key, size_t * o
*
* Caller is responsible for freeing the returned buffer.
*/
uint8_t
*
xxtea_decrypt_ubyte
(
const
uint8_t
*
data
,
size_t
len
,
const
uint8_t
*
key
,
size_t
*
out_len
)
{
uint8_t
*
xxtea_decrypt_ubyte
(
const
uint8_t
*
data
,
size_t
len
,
const
uint8_t
*
key
,
size_t
*
out_len
)
{
uint8_t
*
out
;
uint32_t
*
data_array
,
*
key_array
;
size_t
data_len
,
key_len
;
...
...
@@ -229,8 +218,7 @@ uint8_t * xxtea_decrypt_ubyte(const uint8_t * data, size_t len, const uint8_t *
if
(
!
data_array
)
return
NULL
;
key_array
=
xxtea_to_uint_array
(
key
,
16
,
0
,
&
key_len
);
if
(
!
key_array
)
{
if
(
!
key_array
)
{
free
(
data_array
);
return
NULL
;
}
...
...
@@ -243,6 +231,14 @@ uint8_t * xxtea_decrypt_ubyte(const uint8_t * data, size_t len, const uint8_t *
return
out
;
}
// public functions
void
*
xxtea_encrypt
(
const
void
*
data
,
size_t
len
,
const
void
*
key
,
size_t
*
out_len
)
{
FIXED_KEY
return
xxtea_encrypt_ubyte
(
data
,
len
,
fixed_key
,
out_len
);
}
void
*
xxtea_decrypt
(
const
void
*
data
,
size_t
len
,
const
void
*
key
,
size_t
*
out_len
)
{
return
xxtea_decrypt_ubyte
(
data
,
len
,
key
,
out_len
);
FIXED_KEY
return
xxtea_decrypt_ubyte
(
data
,
len
,
fixed_key
,
out_len
);
}
xxtea.h
View file @
6ba543bb
...
...
@@ -2,13 +2,14 @@
| |
| xxtea.h |
| |
| XXTEA encryption algorithm library for
Golang.
|
| XXTEA encryption algorithm library for
C.
|
| |
| Encryption Algorithm Authors: |
| David J. Wheeler |
| Roger M. Needham |
| |
| Code Author: Ma Bingyao <mabingyao@gmail.com> |
| Code Authors: Chen fei <cf850118@163.com> |
| Ma Bingyao <mabingyao@gmail.com> |
| LastModified: Mar 3, 2015 |
| |
\**********************************************************/
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment